在x86汇编中减去两个64位数的正确方法

use*_*298 1 x86 assembly subtraction

我正在使用32位系统并在EDX中保存了64位数:EAX.我正在尝试减去ESI中保存的数字:EDI是正确的吗?我很确定它不是因为经过3次迭代后结果不正确.

sub %esi, %edx          #Subtract two 64 bit numbers
sub %edi, %eax
Run Code Online (Sandbox Code Playgroud)

amd*_*mdn 9

您需要进行两项更改:

  1. 首先减去低位32位,而不是高位
  2. 如果减去32位的低位生成a borrow,则需要从高位开始减去一位.幸运的是,CPU会记住是否存在借用(在carry flagCF中)并且有一条指令要借借减去,SBB

这是最终的代码

sub %edi, %eax          # Subtract low order 32-bits, borrow reflected in CF
sbb %esi, %edx          # Subtract high order 32-bits, and the borrow if there was one
Run Code Online (Sandbox Code Playgroud)