1

我正在阅读Richard Detmer 的《汇编语言》一书

第一章说:

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.

进位标志是EFL寄存器的第 0 位。

假设我们要执行 195D - 618D = -423D 作为减法运算。有借位,因此不应设置进位标志。

以下 asm 代码编译并运行,但在 之后sub rax, 618,确实设置了进位标志。

相应的加法将是 00C3h + FD96h,这不涉及进位,因为最终的成对加法是 0 + F,没有进位,因此最终成对加法没有进位。

 .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

我不清楚这怎么可能。

任何帮助将不胜感激。

4

1 回答 1

4

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.

于 2017-11-16T17:07:13.187 回答