0

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
P O
  • 13
  • 4
  • You're using the file descriptor as a pointer to a string to write, but it's not. The file descriptor will be a small integer, so the write will return EFAULT (invalid address) – Chris Dodd Apr 19 '22 at 21:48
  • Your code after the call to `open` has the comment "attempt to read" but you are putting 1 into `rax` which is for writing. What exactly is this this program trying to do? – Jim Rhodes Apr 19 '22 at 21:54
  • I was trying to print the integer that is the file descriptor – P O Apr 19 '22 at 21:55
  • 1
    When writing, `rsi` is a memory address so you can't do that. You would have to convert the value to text. – Jim Rhodes Apr 19 '22 at 22:01
  • In `print:`, `add rsp, 8` / `sub rsp, 8` is pointless if you don't actually do anything with RSP in between. You're just copying register values, and neither of them are RSP. (And if it was, you could have done `lea rsi, [rsp+8]` or something, instead of temporarily having the return address in the red-zone instead of above RSP. (Which is safe because the x86-64 ABI guarantees a red-zone, otherwise anything below RSP could get overwritten.) It's weird that your `print` function doesn't just take args in the right regs in the first place, instead using RBX as an arg for no reason. – Peter Cordes Apr 20 '22 at 03:33
  • If you're doing this as a debug-print, just use `strace ./myprog` instead. – Peter Cordes Apr 20 '22 at 03:37

0 Answers0