how does the CPU know when to perform signed or unsigned SUBtraction

Started by Rainstorm, March 16, 2008, 07:34:26 AM

Previous topic - Next topic

Rainstorm

hi

I've used the sub instruction & stuff.. but how does the CPU know when to treat the numbers as signed or unsigned & when to perform signed subtraction.

mov ebx, -50
sub ebx, 25


mov ebx, 50
sub ebx, 25


THanks

zooba

Two's complement notation is designed so that addition and subtraction can be performed using the same electronics regardless of the sign. The only difference is where an overflow will occur (for unsigned, 255->0 when adding 1, for signed, 127->128 (-128) when adding 1) which is specified using two flags (OF and CF).

Convert some numbers to binary (Windows Calculator will do) and add them up by hand. You'll see that when you restrict the number of available bits the results come out the same. Also look up tutorials on two's complement notation (there are probably some forum posts that explain it as well).

Cheers,

Zooba :U

Rainstorm

hi zooba,

yes i know about 2's complement. but in the examples I gave above(same numbers different signs), the bit markings are different for the results. that's what i meant to ask..probably my question wasn't framed right.- i even wrote a small snippet to show the results in binary.

-75   is         1011 0101
25    is         0001 1001


Rainstorm

still confused about something though.. Can you explain in terms of these examples

mov ebx, -50
sub ebx, 25

Result  = 25   is  0001 1001

mov ebx, 50
sub ebx, 25

result..
-75 (181 treated as unsigned) - 1011 0101

the raw results(& bits) there are different for the two, with same numbers just different signs...that's what i mean to ask....so how does the CPU know it has to treat 50 as signed. am aware that the highest bit (sign bit) denotes the sign.. but the CPU would have to know how to treat it as such in the first place right ?  because otherwise it would be some other unsigned value.

thx

[edit] Maybe i should change the topic title to something else.. dunno what though ; )

MichaelW

If you look at the results for this code:

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
      b1 db 16 dup(0)
      b2 db 16 dup(0)
      b3 db 16 dup(0)
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    mov bl, 50
    invoke byt2bin_ex, bl, ADDR b1
    print ADDR b1,13,10
    invoke byt2bin_ex, 25, ADDR b2
    print ADDR b2,13,10
    print "--------",13,10
    sub bl, 25
    invoke byt2bin_ex, bl, ADDR b3
    print ADDR b3,13,10,13,10

    mov bl, -50
    invoke byt2bin_ex, bl, ADDR b1
    print ADDR b1,13,10
    invoke byt2bin_ex, 25, ADDR b2
    print ADDR b2,13,10
    print "--------",13,10
    sub bl, 25
    invoke byt2bin_ex, bl, ADDR b3
    print ADDR b3,13,10,13,10


    inkey "Press any key to exit..."
    exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start


00110010
00011001
--------
00011001

11001110
00011001
--------
10110101


And consider that the rules for binary subtraction are:

0 - 0 = 0
1 - 0 = 1
1 - 1 = 0
0 - 1 = 1, borrow 1


You should be able to see that the processor is performing a fixed set of operations on the bits, independent of the signed/unsigned status of the operands.

eschew obfuscation

Rainstorm

Michael, that kinda cleared it all for me..I get it now : ))
the 2's complement arithmetic automatically achieves the purpose.. while the cpu
does the same set of operations on the bits.

11001110   <-- that's -50 in 2's complement form
00011001   
--------
10110101


so the -50 is automatically passed by the assembler to the CPU in 2's complement form.
in my code i tried it with a variable too instead of an immediate.. & I guess the same happens there.

thanks a lot.

Rainstorm