3

I'm having trouble with changing the value in %rax, which is 1 into a char* for it to be printed in a syscall write in x86 64 Assembly GAS. It produces a segmentation fault. No stdlib.

# Write Implementation
mov %rax,rsi    #Number to print
mov     $1, %rax                #Write
mov     $1, %rdi                #File Handle 1
mov     $4, %rdx
syscall
  • `rsi` is supposed to point to the buffer to print. A pointer of `1` is not going to be a valid buffer. You need to have some memory (perhaps on the stack?) that you write the character to. Also, there is a difference between `rax` containing the value `1`, and it containing the ascii value for `1` (aka 49). If you want to show a 1 on the screen, you want the ascii value. – David Wohlferd Nov 07 '17 at 04:32
  • My answer on the linked duplicate has a nice and commented implementation of the function you're trying to write. (Using a tmp buffer on the stack). – Peter Cordes Nov 07 '17 at 07:33
  • 1
    Also, the segfault you're getting is after `sys_write` returns and you fall off the end of the function. Passing a bogus pointer to `sys_write` will make it return `-EFAULT`. i.e. you have a separate segfault-causing bug, passing the integer as a pointer doesn't explain that. – Peter Cordes Nov 07 '17 at 07:36

0 Answers0