【发布时间】:2014-10-12 21:20:34
【问题描述】:
这是我第一次尝试在 Linux 下进行 64 位汇编。我正在使用 FASM。
我正在将 64 位寄存器十六进制值转换为字符串。它工作正常,直到达到最后一个数字。我无法弄清楚我的代码到底有什么问题。也许有一些关于 64 位编程的东西我不知道或与系统调用有关(我也是一个 linux 菜鸟)
format ELF64 executable 3
entry start
segment readable executable
start:
mov rax,3c5677h ;final '7' is not displayed
push rax
call REG
;call line
xor edi,edi ;exit
mov eax,60
syscall
;----------------------------------
REG:push rbp ;stack frame setup
mov rbp,rsp
sub rsp,8 ;space for local char
mov rax,[rbp+16];arg
lea r9,[rsp-8] ;local char
mov rcx,16 ;divisor
mov rsi,16 ;16 hex digits for register
.begin: ;get the digit
xor rdx,rdx ;by division
div rcx ;of 16
push rdx ;from back to front
dec rsi
test rsi,rsi
jz .disp
jmp .begin
.disp: ;convert and display digit
inc rsi
pop rax ;In reverse order
add rax,30h ;convert digit to string
cmp rax,39h ;if alpha
jbe .normal
add rax,7 ;add 7
.normal:
mov [r9],rax ;copy the value
push rsi ;save RSI for syscall
mov rsi,r9 ;address of char
mov edx,1 ;size
mov edi,1 ;stdout
mov eax,1 ;sys_write
syscall
pop rsi ;restore RSI for index
cmp rsi,16
je .done
jmp .disp
.done:
add rsp,8 ;stack balancing
pop rbp
ret
提前感谢您的帮助。
【问题讨论】:
-
看起来很适合调试器。
-
我也是一个 linux 菜鸟。可能需要一些时间来学习 gdb 之类的东西。