我有这个用NASM程序集编写的代码。我想打开一个通过命令行参数提供的文件。然后我想阅读内容。不幸的是sys_open失败(rax寄存器中的file_descriptor是有线0xffffffffffffffffe)。当我在.data部分硬编码文件名时 - 程序运行正常。当我通过命令行参数读取文件名时失败。感谢您的提示
代码:
SYS_READ equ 0
SYS_OPEN equ 2
SYS_CLOSE equ 3
SYS_EXIT equ 60
O_RDONLY equ 0
section .bss
argc resb 1
filename resb 10
array resb 256
number resb 1
fd resb 8
section .text
global _start
_start:
pop rax ; pop argc value - should be 2
cmp rax, 0x2
jne _sys_exit_1
pop rax ; pop addres pointing to "./prog"
pop rax ; pop addres pointing to filename
mov [filename], rax
call _sys_open
mov [fd], rax ; store file descriptor
_read_seq_loop:
call _sys_read
xor rbx, rbx
mov bl, byte [number] ; store read number in bl register
cmp bl, 0
mov byte [array + rbx], 1 ;save it to array
jne _read_seq_loop
jmp _sys_exit_0
_sys_open:
mov rax, SYS_OPEN
mov rdi, filename
mov rsi, O_RDONLY
mov rdx, 0
syscall
ret
_sys_read:
mov rax, SYS_READ
mov rdi, [fd]
mov rsi, number
mov rdx, 1
syscall
ret
_sys_exit_1:
mov rax, SYS_EXIT
mov rdi, 1
syscall
_sys_exit_0:
mov rax, SYS_EXIT
mov rdi, 0
syscall
答案 0 :(得分:2)
进行这两项更改,我想你会马上看到,不是将指针传递给字符串,而是将指针传递给指向字符串的指针。
pop rax ; pop argc value - should be 2
cmp rax, 0x2
jne _sys_exit_1
pop rax ; pop addres pointing to "./prog"
pop rdi ; pop address of ARG0
call _sys_open
mov [fd], rax ; store file descriptor
由于已经设置了RDI,因此可以将其从常规
中取出_sys_open:
mov rax, SYS_OPEN
mov rsi, O_RDONLY
mov rdx, 0
syscall
ret