如何在 NASM 中打印 64 位数字?



我正在编写一个子例程,该子例程应该打印在 rdi 中传递给它的任何内容的十进制值。它适用于可以用 32 位表示的每个数字。一旦涉及 64 位值,事情就会崩溃。

如果我传递4294967295或 000000000000000000000000000000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111但如果我这样做

mov rdi, 4294967295
inc rdi
call Print_Unsinged

我得到了错误的结果(确切地说是 X96(。

我以这种方式检查参数的大小:

mov rbx, rax ; rax has the orginal arg at this point
xor ebx, eax
cmp rbx, 0
jne isQword
mov ebx, eax
xor bx, ax
cmp ebx, 0
jne isDword
cmp ah, 0
jne isWord
jmp isByte

最终发生的事情是,一个应该具有超出 ebx 设置的位并且应该跳转到 isQword 的值跳转到 isDword。因此,打印的第一个字符最终成为垃圾,而其余的数字打印正常。查看第一个代码片段:我希望参数值为 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000但是不,此值一直过滤到 isByte 并打印"X96"。

我想不通,谁能帮忙?

此问题已解决,谢谢!

我的代码无法检测到 64 位值(正如有人指出的那样(的原因是,对寄存器执行 32 位操作会清除该寄存器的高 32 位。

; rax has the orginal rdi argument at this point in the code
mov rbx, rax 
xor ebx, eax ; this clears the upper 32 bits of rbx
cmp rbx, 0 ; these are equal
jne isQword ; so we don't get to isQword when we should

最新更新