开发者

the carry flag issue!

开发者 https://www.devze.com 2022-12-31 16:50 出处:网络
Suppose AX =FFFE and BX=1234 now if we write cmp ax,bx (bx will be subtracted from ax and the approprite flages will be updated)

Suppose AX =FFFE and BX=1234

now if we write cmp ax,bx

(bx will be subtracted from ax and the approprite flages will be updated) now the binary representation of the numbers in ax and bx is given by

    AX =    1111    1111    1111    1110
   BX=     0001    0010    0011    0100

As bx will be subtracted from ax so we have to negate bx (as Result= ax+(-bx)) so the negated bx (2's complement of bx ) is given by.

BX=    1110    1101    1100    1100

Now we add both ax and bx (as subtraction is implemented by addition in computer)

    AX=   1111    1111    1111    1110
   BX=   1110    1101    1100    1100
    ------------------------------------
      1  1110    1101 开发者_C百科   1100    1010 

Now as you can see the result is of 17 bits now the 17th bit should go into carry flage, but when i checked it the carry flag is 0 that is CF=0 why?


Found a link here: http://oopweb.com/Assembly/Documents/ArtOfAssembly/Volume/Chapter_6/CH06-2.html

It is as I expected. The carry flag is set only if "borrow" was required. When doing subtraction, you set the carry flag prior to doing the "subtract" and the new carry flag tells if you had to borrow. Your example omitted the addition of 1 for the pre-set carry flag in bit 17 which will cause no carry in the result.


Think of the carry flag as a borrow bit when subtraction is performed. It is initialized to 1 so the operation is a−b−C, i.e., a + not(b) + C instead of a + not(b) + 1 as you described. In other words, the carry is inverted for subtract so it can be used to do multiple-precision subtract.

0

精彩评论

暂无评论...
验证码 换一张
取 消