简体   繁体   English

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

[英]How to print a 64 bit number in NASM?

I'm writing a subroutine that's supposed to print the decimal value of whatever gets passed to it in rdi. 我正在编写一个子程序,该子程序应该打印在rdi中传递给它的所有内容的十进制值。 It works great for every number that can be represented with 32 bits. 它适用于每个可以用32位表示的数字。 As soon as 64 bit values get involved things break down. 一旦涉及到64位值,事情就会崩溃。

if I pass 4294967295 or 0000000000000000000000000000000011111111111111111111111111111111b as an argument it prints as expected. 如果我将4294967295或0000000000000000000000000000000011111111111111111111111111111111b作为参数传递,则会按预期打印。 But if I do 但是如果我这样做

mov rdi, 4294967295
inc rdi
call Print_Unsinged

I get the wrong result (X96 to be exact). 我得到了错误的结果(准确地说是X96)。

I'm checking the size of the argument this way: 我正在以这种方式检查参数的大小:

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

What ends up happening is that a value that should have bits beyond ebx set and should be jumping to isQword jumps to isDword instead. 最终发生的是,一个应该具有超出ebx设置的位并且应该跳转到isQword的值改为跳转到isDword。 So the first character printed ends up being garbage while the rest of the number prints fine. 因此,打印的第一个字符最终将成为垃圾,而其余的数字则可以正常打印。 Look at the first code snippet: I would expect the argument value to be 0000000000000000000000000000000100000000000000000000000000000000b, and then that would trigger a jump to isQword because rbx would have a bit set after ebx was cleared. 看一下第一个代码段:我希望参数值是0000000000000000000000000000000100000000000000000000000000000000b,然后将触发到isQword的跳转,因为在清除ebx之后rbx会设置一些位。 But no, this value filters all the way down to isByte and prints "X96". 但是,不,此值一直向下过滤到isByte并显示“ X96”。

I can't figure this out, can anyone help? 我想不通,有人可以帮忙吗?

This has been resolved, thank you! 这个已经解决了,谢谢!

the reason my code could not detect a 64 bit value (as someone pointed out) is that doing a 32 bit operation on a register clears the upper 32 bits of that register. 我的代码无法检测到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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM