numbers - Nasm 中的打印数字批评
问题描述
我写的“方法”是Nasm 64bit 模式。我只是想要一些建议,因为我刚刚开始在 Nasm 中编码,所以我可以做得更好。例如,有没有一种方法可以使用更少的寄存器,或者我的命名约定很糟糕(它们可能是!),有没有更好的方法来打印负值等。
bits 64
section .text
global _start
_start:
;debugging
mov rbp, rsp
push -666 ;number I want to print
call _printVal ;calling the print "method"
;exit
mov rax, 1
mov rbx, 0
int 80h
_printVal:
;prolog
push rbp
mov rbp, rsp
;save registers
push rbx
push rdi
push rsi
;actual code
mov rax, [rbp + 16];get value user wants to print
mov rbx, 10 ;we will be dividing by the to get the last digit
xor rcx, rcx ; clear the rcx register as this will be out counter
cqo; extend the flag
cmp rdx, -1 ;check to see if negative
jne _divisionLoop;if not negative jump
;print negative sign
push 45;negative sign value
mov rax, 1; sys_write
mov rdi, 1; stdout
mov rsi, rsp ; address to what to print
mov rdx, 1 ; how many bytes is it
syscall
pop rax ;pop negative sign value from stack
mov rax, [rbp + 16]; get user value again
converting negative value to positive
dec rax
not rax
xor rcx, rcx; clear rcx because of syscall again
_divisionLoop:
xor rdx, rdx
div rbx ;divides number by 10 to move over last digit into rdx reg
push rdx ;pushes the remainder onto the stack
inc rcx ; count for how many digits added to stack
cmp rax, 0
jnz _divisionLoop ;jump if the division did not result in a zero
mov rax, 1; sys_write
mov rdi, 1; stdout
mov rdx, 1; 1 byte long
_printToScreenVal:
mov rsi, rsp; pop value from stack (most significant digit)
push rcx; (save rcx)
mov rcx, 48
add [rsi], rcx; add 48 to make it into ASCII value
syscall
pop rcx; get rcx register back
pop rbx; pop most siginificant digit
dec rcx; decrement how many more digits i have to print
jnz _printToScreenVal
;restore register
pop rsi
pop rdi
pop rbx
;epilog
pop rbp
ret
解决方案
推荐阅读
- python - 尝试/例外,不返回例外
- javascript - 为什么在浏览器中缓存的脚本比 iframe 源更多
- ios - completionHandler / userCompletionHandler 问题 - Swift iOS
- php - 错误 403 访问被禁止!您无权访问请求的对象。它要么是读保护的,要么是服务器不可读的
- javascript - 在单选按钮选项上选择不同的 div
- python - 我可以抓取两个 reddit 页面直到某个点,然后我得到一个错误,我不明白为什么
- javascript - 使用 Firebase 和 AngularFire 获取、更新和删除文档时的竞争条件
- python - Pandas 按功能分组未正确分组?
- python - 没有模块名称 Pythonwin
- xcode - 极慢的模拟器(输入不注册,滚动很慢/滞后/生涩)