通常,您只需使用调试器来查看标志,而不是编写所有代码以将它们放入 C 变量中以进行调试打印。特别是因为体面的调试器会象征性地为您解码条件标志,而不是显示十六进制值。
您不必知道或关心 FLAGS 中的哪个位是 CF,哪个是 ZF。(此信息也与编写实际程序无关。我没有记住它,我只知道哪些标志是通过不同条件测试的jae
或jl
。当然,最好了解 FLAGS 只是您可以复制,保存/恢复,甚至根据需要进行修改)
您的函数 args 和返回值是int
,在您使用的 System V 32 位 x86 ABI 中是 32 位的。( x86标签 wiki中的 ABI 文档链接)。编写一个只查看其输入的低 16 位并在输出的高 16 位留下高垃圾的函数是一个错误。原型中的int
返回值告诉编译器 EAX 的所有 32 位都是返回值的一部分。
正如迈克尔指出的那样,您似乎是在说您的作业需要使用 16 位 ADD。与查看完整的 32 位相比,这将产生具有不同输入的进位、溢出和其他条件。(顺便说一句,这篇文章很好地解释了进位与溢出。)
这就是我要做的。注意 ADD 的 32 位操作数大小。
global add
section .text
add:
push ebp
mov ebp,esp ; stack frames are optional, you can address things relative to ESP
mov eax, [ebp+8] ; first arg: No need to avoid loading the full 32 bits; the next insn doesn't care about the high garbage.
add ax, [ebp+12] ; low 16 bits of second arg. Operand-size implied by AX
cwde ; sign-extend AX into EAX
mov ecx, [ebp+16] ; the pointer arg
pushf ; the simple straightforward way
pop edx
mov [ecx], dx ; Store the low 16 of what we popped. Writing word [ecx] is optional, because dx implies 16-bit operand-size
; be careful not to do a 32-bit store here, because that would write outside the caller's object.
; mov esp,ebp ; redundant: ESP is still pointing at the place we pushed EBP, since the push is balanced by an equal-size pop
pop ebp
ret
CWDE(8086 CBW 指令的 16->32 形式)不要与CWD(AX -> DX:AX 8086 指令)混淆。如果您不使用 AX,那么 MOVSX / MOVZX 是一个很好的方法。
有趣的方法:我们可以直接将 16 位弹出到目标内存地址,而不是使用默认操作数大小并进行 32 位推送和弹出。这会使堆栈不平衡,因此我们可以取消注释mov esp, ebp
,或者使用 16 位 pushf(带有操作数大小的前缀,根据文档,它只推送低 16 位 FLAGS,而不是 32 位 EFLAGS .)
; What I'd *really* do: maximum efficiency if I had to use the 32-bit ABI with args on the stack, instead of args in registers
global add
section .text
add:
mov eax, [esp+4] ; first arg, first thing above the return address
add ax, [esp+8] ; second arg
cwde ; sign-extend AX into EAX
mov ecx, [esp+12] ; the pointer
pushfw ; push the low 16 of FLAGS
pop word [ecx] ; pop into memory pointed to by unsigned short* flags
ret
PUSHFW 和 POP WORD 都将使用操作数大小的前缀进行组合。的输出objdump -Mintel
,它使用与 NASM 略有不同的语法:
4000c0: 66 9c pushfw
4000c2: 66 8f 01 pop WORD PTR [ecx]
PUSHFW 与o16 PUSHF
. 在 NASM 中,o16
应用操作数大小前缀。
如果您只需要低 8 个标志(不包括 OF),您可以使用LAHF将 FLAGS 加载到 AH 中并存储它。
我不建议直接推入目的地。暂时将堆栈指向某个随机地址通常是不安全的。带有信号处理程序的程序将异步使用堆栈下方的空间。这就是为什么你必须在使用它之前保留堆栈空间sub esp, 32
或其他任何东西,即使你不打算通过将更多东西压入堆栈来进行覆盖它的函数调用。唯一的例外是当您有一个red-zone时。
你 C 来电者:
您正在传递一个 NULL 指针,因此您的 asm 函数当然会出现段错误。传递本地地址以将函数提供给某个地方进行存储。
int add(int a, int b, unsigned short* flags);
int main(void) {
unsigned short flags;
int result = add(30000, 36000, &flags);
printf("%d %#hx\n", result, flags);
return 0;
}