Assembly x86-64 setting carry flag for sub instruction

老子叫甜甜 提交于 2021-01-28 18:00:19

问题


I am working through Richard Detmer's Assembly Language book.

The first chapter states:

A borrow occurs in the subtraction a - b when b is larger than a as unsigned numbers. Computer hardware can detect a borrow in subtraction by looking at whether a carry occurred in the corresponding addition. If there is no carry in the addition, then there is a borrow in the subtraction. If there is a carry in the addition, then there is no borrow in the subtraction.

The carry flag is the 0th bit of the EFL register.

Suppose we want to perform 195D - 618D = -423D as a subtraction operation. There is a borrow and hence the carry flag should not be set.

The following asm code compiles and runs, yet after sub rax, 618, the carry flag is indeed set.

The corresponding addition would be 00C3h + FD96h and this does not involve a carry since the final pairwise addition is 0 + F with no carry into it and hence, there is no carry out of the final pairwise addition.

 .DATA
number  QWORD   195
sum     QWORD   ?

.CODE
main    PROC
        mov     rax, number     ; 195 to RAX
        sub     rax, 618        ; subtract 618
             ;at this point, however, the carry flag is indeed set to 1. Why is this?
        mov     sum, rax        ; sum to memory

        mov     rax, 0          ; return code
        ret                     ; exit to operating system

main    ENDP

END

I am unclear about how this could be.

Any help will be appreciated.


回答1:


First understand that there is unsigned integer arithmetic (where overflow is indicated by the carry flag) and signed integer arithmetic (where overflow is indicated by the overflow flag).

The same addition and subtraction instructions are used for both unsigned and signed integer arithmetic. The only difference is which flags you test afterwards and how you interpret the result (e.g. as -0x0000423D or as 0xFFFFBDC3).

A borrow is also indicated by the carry flag. This means that a borrow happens whenever an unsigned integer overflow happened. For 0x0000195D - 0x0000618D there is an unsigned integer overflow (unsigned integers can't be negative), so the carry flag will be set (but there wasn't a signed integer overflow, so the overflow flag won't be set). The result will be 0xFFFFBDC3 or -0x0000423D depending on whether the result was supposed to be signed or unsigned.



来源:https://stackoverflow.com/questions/47333458/assembly-x86-64-setting-carry-flag-for-sub-instruction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!