WRITE

PROCEDURE

1.identify the corresponding number for the syscall (write)
   - cat /usr/include/x86_64-linux-gnu/asm/unistd_64.h
      #ifndef _ASM_X86_UNISTD_64_H
      #define _ASM_X86_UNISTD_64_H 1

      #define __NR_read 0
      #define __NR_write 1
      #define __NR_open 2
      #define __NR_close 3
      #define __NR_stat 4
      #define __NR_fstat 5

2.research how to use the required syscall (write)
   - root@dev:~$ man -s 2 write
      ...SNIP...
      ssize_t write(int fd, const void *buf, size_t count);
       - File Descriptor fd to be printed to (usually 1 for stdout)
       - The address pointer to the string to be printed
       - The length to be printed
 
3.Save registers to stack
   - push ...
   
4.Set its syscall number in rax
   - mov rax, 1
      - rax is also used for storing the return value of a syscall or a function. So, 
        if we were expecting to get a value back from a syscall/function, it will be 
        in rax.
        
5.Set its arguments in the registers
   - rdi -> 1 (for stdout)
     rsi -> 'Fibonacci Sequence:\n' (pointer to our string)
     rdx -> 20 (length of our string)

6.Use the syscall assembly instruction to call it

7.gracefully exit syscalls
   - root@dev:~$ grep exit /usr/include/x86_64-linux-gnu/asm/unistd_64.h
      #define __NR_exit 60
      #define __NR_exit_group 231
   - root@dev:~$ man -s 2 exit
      ...SNIP...
      void _exit(int status);
   
   - mov rax, 60
     mov rdi, 0
     syscall

Last updated