汇编 x86-64 设置子指令的进位标志 [英] Assembly x86-64 setting carry flag for sub instruction

查看:39
本文介绍了汇编 x86-64 设置子指令的进位标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 Richard Detmer 的汇编语言书籍.

第一章指出:

当 b 作为无符号数大于 a 时,在减法 a - b 中出现借位.计算机硬件可以通过查看相应加法中是否发生进位来检测减法中的借位.如果加法没有进位,那么减法就有借位.如果加法有进位,那么减法就没有借位.

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

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

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

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.

下面的汇编代码编译并运行,但在sub rax, 618之后,确实设置了进位标志.

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

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

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.

任何帮助将不胜感激.

推荐答案

首先了解有无符号整数运算(溢出由进位标志表示)和有符号整数运算(溢出由溢出标志表示).

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).

相同的加法和减法指令用于无符号和有符号整数算术.唯一的区别是您之后测试的标志以及您如何解释结果(例如 -0x0000423D 或 0xFFFFBDC3).

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).

借位也由进位标志指示.这意味着只要发生​​无符号整数溢出,就会发生借用.对于 0x0000195D - 0x0000618D 存在无符号整数溢出(无符号整数不能为负),因此将设置进位标志(但没有有符号整数溢出,因此溢出标志获胜未设置).结果将是 0xFFFFBDC3 或 -0x0000423D,具体取决于结果应该是有符号还是无符号.

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.

这篇关于汇编 x86-64 设置子指令的进位标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆