push-pop.txt 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. Example of PUSH and POP
  2. examen de la stack sous gdb :
  3. x/10x $sp
  4. Here’s a simple example of pushing and popping a value in x86-64 assembly:
  5. [BITS 64]
  6. section .text
  7. global _start ; Entry point for the program
  8. _start:
  9. mov rax, 42 ; Moves the value 42 into the rax register
  10. push rax ; Pushes the value of rax onto the stack
  11. pop rbx ; Pops the top value from the stack into rbx
  12. ; Exit the program
  13. mov eax, 60 ; syscall number for exit
  14. xor edi, edi ; Exit code 0
  15. syscall ; Invoke syscall to exit the program
  16. In this example, we first moved the value 42 into the rax register. Then we pushed the value of rax onto the stack. We then popped the top value from the stack into the rbx register.
  17. Finally, we use the syscall instruction to exit the program.
  18. Autre programme :
  19. [BITS 64]
  20. section .bss
  21. ; nada
  22. section .data
  23. prompt: db 'What is your name? '
  24. prompt_length: equ $-prompt
  25. name: times 30 db 0
  26. name_length: equ $-name
  27. greeting: db 'Greetings, '
  28. greeting_length: equ $-greeting
  29. section .text
  30. global _start
  31. _start:
  32. ; Prompt the user for their name
  33. mov rax, 1
  34. ; '1' is the ID for the sys_write call
  35. mov rdi, 1
  36. ; '1' is the file descriptor for stdout and our
  37. first argument
  38. mov rsi, prompt
  39. ; prompt string is the second argument
  40. mov rdx, prompt_length
  41. ; prompt string length is the third argument
  42. syscall
  43. ; make the call with all arguments passed
  44. ; Get the user's name
  45. mov rax, 0
  46. ; 0 = sys_read
  47. mov rdi, 0
  48. ; 0 = stdin
  49. mov rsi, name
  50. mov rdx, name_length
  51. syscall
  52. push rax
  53. ; store return value (size of name) on the
  54. stack... we'll need this for later
  55. ; Print our greeting string
  56. mov rax, 1
  57. ; 1 = sys_write
  58. mov rdi, 1
  59. ; 1 = stdout
  60. mov rsi, greeting
  61. mov rdx, greeting_length
  62. syscall
  63. ; Print the user's name
  64. mov rax, 1
  65. mov rdi, 1
  66. mov rsi, name
  67. pop rdx
  68. stored on the stack
  69. syscall
  70. ; 1 = sys_write
  71. ; 1 = stdout
  72. ; length previously returned by sys_read and
  73. ; Exit the program normally
  74. mov rax, 60
  75. ; 60 = sys_exit
  76. xor rdi, rdi
  77. ; return code 0
  78. syscall