装配打印在大数字之前
该程序打印小数字,但不是大数字,我不知道为什么。
例如打印1346269
将以“ C1346269”打印 打印40000
将以“ 40000”的形式打印出来。
这是宏:
%macro print 1
mov rax, %1
%%printInt:
mov rcx, digit ; set rcx to digit memory address
mov rbx, 10 ; moving a newline into rbx
mov [rcx], rbx ; setting digit to rbx
inc rcx ; increment rcx position by one byte
%%storeLoop:
xor rdx, rdx ; zero out rdx
mov rbx, 10
div rbx ; rax / rbx (10)
; rdx holds the remainder of the divison
add rdx, 48 ; add 48 to rdx to make in ascii character
mov [rcx], dl ; get the character part of rdx
inc rcx ; increment digit position again
cmp rax, 0
jnz %%storeLoop ; continue looping until rax is 0
%%printLoop:
push rcx
;perform sys write
mov rax, SYSWRITE
mov rdi, 1
mov rsi, rcx
mov rdx, 1
syscall
pop rcx
dec rcx
cmp rcx, digit ; first byte of digit (10)
jge %%printLoop
%endmacro
这就是我的使用方式:
_start:
print 40000
exit
This program prints small numbers fine but not big numbers and I don't know why.
For example print 1346269
will print out as "c1346269"
and print 40000
will print out as "40000" just like it's supposed to.
Here's the macro:
%macro print 1
mov rax, %1
%%printInt:
mov rcx, digit ; set rcx to digit memory address
mov rbx, 10 ; moving a newline into rbx
mov [rcx], rbx ; setting digit to rbx
inc rcx ; increment rcx position by one byte
%%storeLoop:
xor rdx, rdx ; zero out rdx
mov rbx, 10
div rbx ; rax / rbx (10)
; rdx holds the remainder of the divison
add rdx, 48 ; add 48 to rdx to make in ascii character
mov [rcx], dl ; get the character part of rdx
inc rcx ; increment digit position again
cmp rax, 0
jnz %%storeLoop ; continue looping until rax is 0
%%printLoop:
push rcx
;perform sys write
mov rax, SYSWRITE
mov rdi, 1
mov rsi, rcx
mov rdx, 1
syscall
pop rcx
dec rcx
cmp rcx, digit ; first byte of digit (10)
jge %%printLoop
%endmacro
And here is how I use it:
_start:
print 40000
exit
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
storeloop 以
inc rcx
结尾,将rcx 留在后面。因此,在调用syscall
之前, printloop 必须 Predecrement 指针。“长”和“短”数字之间的差异纯粹是因为您在(垃圾)打印的第一个地址中发生了内存中的差异。
The storeLoop ends with an
inc rcx
, leaving RCX behind the last character. Therefore, the printLoop must pre-decrement the pointer before invoking thesyscall
.The difference between 'long' and 'short' numbers is purely because of what happens to be in memory at the first address that you print from (garbage).