生成的可执行文件

时间:2018-02-01 06:28:01

标签: macos assembly nasm x86-64 system-calls

这是我来自wiki的NASM代码。我在MacOS 10.12.6中使用以下命令编译它。

nasm -f macho64 -o echo.o echo.asm
ld echo.o -e _start -o echo

我用:

运行程序
./echo

我发现out字符串不打印。我使用lldb来调试它,发现传递给syscall的地址不是out字符串的实际地址。(实际地址是0x2013,传递的地址是0x2026)所以它打印一个空字符串。 NASM的版本是2.13.02。有人可以解释为什么会发生这种情况以及如何解决这个问题吗?

global _start

section .data

query_string:       db  "Enter a character:  "
query_string_len:   equ $ - query_string
out_string:         db  "You have input:  "
out_string_len:     equ $ - out_string

section .bss

in_char:            resw 4

section .text

_start:

mov rax, 0x2000004      ; put the write-system-call-code into register rax
mov rdi, 1              ; tell kernel to use stdout
mov rsi, query_string   ; rsi is where the kernel expects to find the address of the message
mov rdx, query_string_len   ; and rdx is where the kernel expects to find the length of the message 
syscall

; read in the character
mov rax, 0x2000003      ; read system call
mov rdi, 0              ; stdin
mov rsi, in_char        ; address for storage, declared in section .bss
mov rdx, 2              ; get 2 bytes from the kernel's buffer (one for the carriage return)
syscall

; show user the output
mov rax, 0x2000004      ; write system call
mov rdi, 1              ; stdout
mov rsi, out_string
mov rdx, out_string_len
syscall

mov rax, 0x2000004      ; write system call
mov rdi, 1              ; stdout
mov rsi, in_char
mov rdx, 2              ; the second byte is to apply the carriage return expected in the string
syscall

; exit system call
mov rax, 0x2000001      ; exit system call
xor     rdi, rdi
syscall

0 个答案:

没有答案