所以我在程序所在的目录中有文件file1
。我用
nasm -f elf64 -o prog.o prog.asm
ld --fatal-warnings -o prog prog.o
并与./prog file1
一起运行,但这给了我SegFault
global _start
exit_error:
mov rax, 60
mov rdi, 1
syscall
exit_ok:
mov rax, 60
syscall
_start:
cmp qword [rsp], 2
jne exit_error
mov rdi, [rsp + 16]
call open_file
call close_file
mov rdi, 0
jmp exit_ok
open_file:
mov rax, 2
mov rsi, 0
syscall
mov rdi, [rax]
ret
close_file:
mov rax, 0
syscall
ret
我还尝试使用strace ./prog file1
运行它,我得到了:
execve("./prog", ["./prog", "file1"], 0x7ffc8edac5e8 /* 63 vars */) = 0
open("file1", O_RDONLY) = 3
--- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=0x3} ---
+++ killed by SIGSEGV (core dumped) +++
Segmentation fault (core dumped)
有人可以解释一下为什么t不起作用吗?