Advertisement
Guest User

File: create and write - linux assembly syscalls

a guest
May 2nd, 2018
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. # Create the file /tmp/foo.txt and put a text inside it.
  2. #
  3. # 1. print a message using write syscall
  4. # 2. open/create the file using open syscall
  5. # 3. write a text inside the file using write syscall
  6. # 4. close the file using close syscall
  7. # 5. finish execution using exit syscall
  8. #
  9. # Linux syscall references
  10. # http://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/
  11.  
  12. .section .data
  13. string1: .string "About to create a file \n"
  14. len1: .long .-string1
  15.  
  16. string2: .string "It goes inside the created file\n"
  17. len2: .long .-string2
  18.  
  19. file: .string "/tmp/foo.txt"
  20.  
  21. .section .text
  22. .globl _start
  23.  
  24. _start:
  25.  
  26. # write syscall
  27. mov $1, %rax # sys_write
  28. mov $1, %rdi # unsigned int fd: stdout
  29. mov $string1, %rsi # const char *buf
  30. mov len1, %rdx # size_t count: length of string1
  31. syscall
  32.  
  33. # open syscall
  34. mov $2, %rax # sys_open
  35. mov $file, %rdi # const char * pathname
  36. mov $0x42, %rsi # int flags: O_RDWR | O_CREAT
  37. mov $0x1a4, %rdx # mode_t mode: 0644
  38. syscall
  39.  
  40. xor %r9, %r9
  41. mov %eax, %r9d # save the returned FD in %r9
  42.  
  43. # write syscall
  44. mov $1, %rax # sys_write
  45. mov %r9, %rdi # fd: opened file
  46. mov $string2, %rsi # buf: string2
  47. mov len2, %rdx # count: length of string2
  48. syscall
  49.  
  50. # close syscall
  51. mov $3, %rax # sys_close
  52. mov %r9, %rdi # unsigned int fd: value stored in r9
  53. syscall
  54.  
  55. # exit syscall
  56. mov $60, %rax # sys_exit
  57. mov $0, %rdi # int error_code
  58. syscall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement