stack.s (1402B)
1 .text 2 .global _start 3 4 _start: 5 # Push some 64-bit integers onto the stack. 6 push $3 7 push $2 8 push $1 9 10 # Let's print "Hey\n", using the stack. 11 # man ascii 12 # |---------+----+-----+-----+-----| 13 # | ASCII | H | e | y | \n | 14 # |---------+----+-----+-----+-----| 15 # | Decimal | 10 | 171 | 145 | 110 | 16 # |---------+----+-----+-----+-----| 17 # | Hex | 48 | 65 | 79 | 0A | 18 # |---------+----+-----+-----+-----| 19 20 # Remember that the stack is backwards: 21 # it starts at high memory addresses, and "grows" down. 22 # However, the write system call follows a low-to-high address order. 23 # This means we need to reverse the order of our string. 24 # <-- high low --> 25 # \n y e H 26 push $0x000000000A796548 27 28 # %rsp points to the top of the stack. 29 # (Where 'H' is -- a lower address). 30 # We can pass this address to the kernel. 31 # write will start at 'H', and work up to '\n'. 32 mov $1, %rax # write 33 mov $1, %rdi # stdout 34 lea (%rsp), %rsi # address of buffer 35 # Try changing the number of bytes to 9, 17, 25. 36 # Then call the program with strace. 37 # You'll be able to see the previous numbers we pushed onto the stack. 38 mov $4, %rdx # number of bytes 39 syscall 40 41 # Remember, the stack is LIFO. 42 pop %rdi # pop the "Hey\n" string 43 pop %rdi # pop 1 44 pop %rdi # pop 2 45 46 # We'll use the last popped value in %rdi as our exit code. 47 mov $231, %rax 48 syscall