So if I wanted to add two unsigned ints I would simply write
dat1 db ff
dat2 db ff
res dw ?
and in the code segment:
mov ax, dat1
add ax, dat2
mov dl,0
adc dl,0 ;initialize the contents of dl and use it to store the carry flag
mov byte ptr res, ax
mov byte ptr res+1, dl
BUT, what would the code look like if I wanted to add two SIGNED integers? Can I somehow access the sign flag?
First the errors:
mov byte ptr addr, axshould failmov word ptr res, axAnswer to the first question is: nothing special.
add ax, bxdoesn’t care if they are signed or not. The fun part comes when inspecting the status register. There you have the carry flag, sign flag and overflow flag. (and 25+ unrelated flags). As a side note: overflow flag is set, if carry_flag != sign_flag for addends of the same sign. It means that addition/subtraction of two negative numbers can be interpreted positive or adding two positive numbers can be interpreted as negative. For addends of different sign (3) + (-2) overflow doesn’t happen.Sign flag can be accessed by comparing and branching:
Or if you are concerned to transfer the sign bit of a register somewhere else, you can
–
mov cl, 16; sar ax, cl;// shift the sign bit over the word ax or–
add ax, ax(orshl ax,1) // shift the sign bit out of the “left” side of AX to Carry BitAdditionally one can
pushfstatus flag,pop axit to a general purpose register, and inspect any individual bits.