0

I'm learning assembly language via Youtube now. In the penultimate fifth line of the code segment below, I believe it aims to clear the 13 and "HelloWorld" in the stack. However, instead of poping these two out of the stack, it add the value in esp by 8. If that is the case, when a new element is pushed into the stack, would esp point to wrong data as 13 and "HelloWorld" are not popped out. Or the addition of esp will automatically change the stack? Please help.|ू・ω・` )

.data HelloWorld: acsiz. "HelloWorld!/n" .text

.global start_
.type PrintFunction, @function

PrintFunction:
    #prepare state
    pushl %ebp
    movl %esp, ebp

    #write
    movl $4, %eax
    movl $1, %ebx
    movl 8(%ebp), %ecx
    movl12(%ebp), %edx
    int 0x80

    #return state
    movl $ebp, $esp #clear esp from potential data in the function
    pop $ebp
    ret 

_start:

nop

push $13

push $HelloWorld

call PrintFunction

addl $8, %esp   #issue there


ExistCall:
    movl $1, %eax
    int 0x80`

I have tried to disassemble the code through gdb, and find that the stack go back to the point before the _start.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
LoucO
  • 1
  • 1
  • 1
    You can remove things from the stack by adding to the stack pointer, by popping, or by putting a value directly into the stack pointer — the latter is commonly done when there's a frame pointer, so the stack pointer is reset to where it was when the frame pointer is established. – Erik Eidt Apr 06 '23 at 12:15
  • 1
    Popping internally keeps track of the current top of the stack by adding to the stack pointer as well. The difference is then whether to pop values into available registers or memory variables (which can be unused after the popping), or to simply discard the values by directly adding to the stack pointer. – ecm Apr 06 '23 at 12:43
  • Adding to `esp` is useful when you don't need the values on the stack anymore and you would rather keep your registers the way they are. Just remember that when adding/subtracting to `esp`, the addition/subtraction is measured in bytes, so every `push e__` (fill in the blank with any register name) your code does, you have to add back 4 bytes to maintain stack balance. – puppydrum64 Jun 30 '23 at 12:39

0 Answers0