【发布时间】:2018-11-10 19:57:36
【问题描述】:
今天我开始在 Linux 上使用 NASM 学习 x86_64 汇编。 我成功编写了一个 hello world 程序。 现在我想编写另一个简单的程序。 程序应该询问用户他的名字,然后打印“hi [name]”。 我的问题是该程序不要求名称。如果我启动程序,它不会打印任何内容并停止而不会出现错误。 这是我的代码:
section .data
msg1 db "Type in ur Name? ", 10
len1 equ $ - msg1 ; Get the Size of msg1
msg2 db "Hi, "
len2 equ $ - msg2 ;Get the Size of msg2
section .bss
name resb 16 ;16 Bytes for name
section .text
global _start
_start:
;Call Functions
call _printMsg1
call _getName
call _printMsg2
call _printName
mov eax, 60
mov ebx, 0
int 0x80
_printMsg1:
mov eax, 1
mov ebx, 1
mov ecx, msg1
mov edx, len1
int 0x80
ret
_printMsg2:
mov eax, 1
mov ebx, 1
mov ecx, msg2
mov edx, len2
int 0x80
ret
_printName:
mov eax, 1
mov ebx, 1
mov ecx, name
mov edx, 16 ; reserve 16 Bytes for the name
int 0x80
ret
_getName:
mov eax, 0 ;Syscall 0 = User Input
mov ebx, 0
mov ecx, name
mov edx, 16 ;16 Bytes for the name
int 0x80
ret
感谢您的帮助!
编辑:我发现了问题。 如果我将以下寄存器替换为: eax 到 rax ebx 转 rdi ecx 到 rsi edx 转 rdx
好像我用的是假寄存器。
【问题讨论】:
-
根据asm.sourceforge.net/intro/hello.html应该是
mov eax, 4调用write系统调用。 -
您好,感谢您的回答!我添加了我的解决方案。我只是更改寄存器。我从这里使用系统调用表:filippo.io/linux-syscall-table
-
您确定在 64 位模式下编程吗?你可能会遇到this problem。
-
如果您仍然使用invoking the 32-bit
int 0x80ABI 而不是syscall,则使用RDI、RSI 和RDX 将完全不起作用。您使用的是 64 位的索书号,但绝对其他一切都是 32 位的。
标签: linux assembly nasm x86-64