sakau2007
sakau2007

Reputation: 115

Assembly language - Flag setting giving me headaches

I'm trying to determine what various flags should be set to (carry, sign, zero, overflow) after the following operation:

mov ax, 7ff0h;       ax register is now 7ff0h

add al, 10h;         

I'm confused as to how assembly handles this. since we are adding to just the al portion of the register which contains f0h, f0h + 10h gives 100h. Does this set the overflow flag? The carry flag? Neither? I would think the overflow flag would be set, however, another possibility running through my mind is that the program detects this and automatically alters the ah register from 7fh to 80h, making the full ax register 8000h. This would in theory not set the carry flag, but would instead set the sign flag and overflow flag because our overall number 8000h is now negative. could anyone explain how this is handled?

Thanks.

Upvotes: 2

Views: 843

Answers (2)

Maxime Vernier
Maxime Vernier

Reputation: 350

I have played a bit with the debug command (shipped with x86 Windows):

-a 100
1A2F:0100 mov ax, 7ff0
1A2F:0103 add al, 10
1A2F:0105
-t =100 2

AX=7FF0  BX=0000  CX=0000  DX=0000  SP=FFEE  BP=0000  SI=0000  DI=0000
DS=1A2F  ES=1A2F  SS=1A2F  CS=1A2F  IP=0103   NV UP EI PL NZ NA PO NC
1A2F:0103 0410          ADD     AL,10

AX=7F00  BX=0000  CX=0000  DX=0000  SP=FFEE  BP=0000  SI=0000  DI=0000
DS=1A2F  ES=1A2F  SS=1A2F  CS=1A2F  IP=0105   NV UP EI PL ZR NA PE CY
1A2F:0105 0000          ADD     [BX+SI],AL                         DS:0000=CD
-

NV [No Overflow] PL (positive) CY [Carry]

Upvotes: 1

Necrolis
Necrolis

Reputation: 26171

From the Intel instruction manual on the ADD instruction:

The ADD instruction performs integer addition. It evaluates the result for both signed and unsigned integer operands and sets the OF and CF flags to indicate a carry (overflow) in the signed or unsigned result, respectively. The SF flag indicates the sign of the signed result.

Running the a simple test in a debugger, we can see the CF flag is set, and only the bits that form AL are changed:

.386
.MODEL FLAT

.STACK 1024

ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD

.CODE
_start:
    XOR EAX,EAX
    MOV AX,7ff0h
    ADD AL,10h
    PUSH 0  ;EAX is now 0x00007F00
    CALL ExitProcess

PUBLIC _start
END

Upvotes: 1

Related Questions