I have been learning x64 recently and I am able to get a working print call, but I have not been able to successfully print the return value of open (a file descriptor in rax). Given that there are no errors in the read call, I'm assuming that I'm just making some error in reading the value. I was also unsure of what access to use so I assumed that 4 was read only
section .data
msg1 db 'Hello, world!', 0xa ; set msg as text with trailing newline
len1 equ $ - msg1 ; store length of msg
outfile db 'test_file.txt', 0 ; set outfile as text without trailing newline
msg2 db 'My message!', 0xa ; set msg1 as text with trailing newline
len2 equ $ - msg2
infile db 'token_testlist.txt', 0 ; set input file
section .text
global _start ; define symbol for entry point
global exit ; define symbol for exit
global print ; define symbol for print
global open ; define symbol for open
open:
mov rax, 2
mov rdi, infile
mov rsi, 4
mov rdx, 4
syscall
ret
print:
mov rax, 1 ; set instruction to write
mov rdi, 1 ; set file descriptor to stdout
add rsp, 8 ; move stack pointer to access data
mov rsi, rbx ; set buffer to text
mov rdx, rcx ; set the size to the length of text
sub rsp, 8 ; return stack pointer back
syscall ; system call
ret ; return to call location
_start:
mov rbp, rsp ; for correct debugging
mov rbx, msg2 ; store text in register
mov rcx, len2 ; store len in rcx
call print ; call print function
call open
mov rsi, rax ; -----------------------------------------
mov rdx, 4 ; - attempt to read file descriptor value -
mov rax, 1 ; -----------------------------------------
mov rdi, 1 ;
syscall ;
;call print
jmp exit ; jump to program exit code
exit:
mov eax, 1 ; set instruction
mov ebx, 0 ; program exit code
int 0x80 ; system call