Search code examples
assemblyx86twos-complementimmediate-operand

What if an immediate number is out of range before signed number operations


One of my homework exercises is as follows:

mov al,77h  
sub al,80h  
AL=_______  
CF=_______  
OF=_______  

When I first saw it, I thought the result of positive number minus positive number won't overflow. And I just made OF equals to 0.
But my Assembly code shows me that OF=1.

I'm using MASM6.15, 32bit console environment

Here is my code and the output:
Code 1:

; eg000000.asm in Windows Console  
    include io32.inc  
    .data  
    .code  
start:  
        mov al,77h ;119  
        sub al,80h ;128  
        call disprf ;show those 6 flags  
        call dispbd ;binary  
        call dispcrlf  
        call disphd ;hexadecimal  
    exit 0  
    end start  

Output 1:

OF=1, SF=1, ZF=0, AF=0, PF=0, CF=1  
000000000001100111111111 1111 0111  
0019FFF7  

Code 2:

; eg000000.asm in Windows Console  
    include io32.inc  
    .data  
    .code  
start:  
        mov al,77h  
        add al,-80h ;-128  
        call disprf  
        call dispbd  
        call dispcrlf  
        call disphd  
    exit 0  
    end start

Output 2:

OF=0, SF=1, ZF=0, AF=0, PF=0, CF=0  
000000000001100111111111 1111 0111  
0019FFF7  

Code 3:

; eg0000.asm in Windows Console
    include io32.inc
    .data
    .code
start:
        mov al,77h
        sub al,7fh ;127
        call disprf
        call dispbd
        call dispcrlf
        call disphd
    exit 0
    end start

Output 3:

OF=0, SF=1, ZF=0, AF=1, PF=0, CF=1
000000000001100111111111 1111 1000
0019FFF8

In 'Code 1:' I expect OF=0 and CF=1
In 'Code 2:' I expect CF=1
'Code 3:' it's output is correct

Could somebody tell me that why is it and what if the immediate number is out of range like 'Code 1:' before the computer completes it's computing. I know that no matter signed or unsigned number, the computer just doesn't know about it.

BTW: My first ask here. If there's something I did wrong, I'll appreciate it if you could point it out and tell me the correct way to do it.:)


Solution

  • The signed 2's complement interpretation of the 80h bit-pattern is -128. This is what matters for how sub sets OF. See also Understanding Carry vs. Overflow conditions/flags for signed vs. unsigned.

    -80h is the same value and will assemble to the same machine-code.