Hello World Walkthrough (line-by-line)

Hello World Walkthrough

Line-by-Line Explanation

USNA IWG

About these slides

  • This presentation goes through a sample Hello World program, line by line
  • For the actual code, there will be a stack diagram to the right
  • For stack diagrams, higher addresses are highest on the diagram
  • Similarly, the top of the stack is at the bottom of the diagram
  • The first slide has all the code on it - copy that into a text editor for easy reference

First, all the code:


[BITS 32]

section .data:
    msg:    db `Hello, World!\n\0`  ; use backticks for the string
                                    ; note that we need to manually add the \0

section .text:
    extern printf           ; have to declare what functions we use
    global main             ; main is a global symbol (accessible from other files)

main:
    push ebp                ; standard prologue
    mov ebp, esp            ;
    push msg                ; push msg onto the stack (to use as an arg)
    call printf             ; printf(msg)
    add esp, 4              ; clean up the arg we pushed
    mov eax, 0              ; put return code in eax
    mov esp, ebp            ; standard epilogue
    pop ebp                 ;
    ret                     ;

[BITS 32]

section .data:

msg: db `Hello, World!\n\0`

section .text:

extern printf

global main

main:

push ebp

  • This pushes the last function's block pointer onto the stack
  • This is part of the standard function prologue
  • Need to save ebp because we will overwrite ebp, and the caller expects ebp to be preserved

mov ebp, esp

  • This sets ebp to point to the top of the stack
  • Provides us with a fixed location to reference local memory from (esp changes)
  • Notice how ebp now points to the saved ebp
  • In this way, ebp forms a sort of linked-list

push msg

  • This pushes the value of the msg label onto the stack
  • Labels are pointers
  • So, the variable at the top of the stack points to our message
  • We are pushing this onto the stack to use as an argument

call printf

  • This calls printf() in the c library (libc)
  • Recall: arguments are passed on the stack
  • So, this is equivalent to printf(msg);
  • printf() will return to the next instruction
  • Recall: return value will be in eax

add esp, 4

  • printf() has returned
  • Its return code is in eax (but we don't care)
  • Now, we need to clean up the stack
  • Recall: in x86 it is the caller's responsibility to remove args from the stack
  • Recall: adding 4 to esp effectively removes one item from the stack

mov eax, 0

  • Putting the return code for main() into eax
  • Necessary because we don't know what printf() will put there

mov esp, ebp

  • Part of the standard epilogue
  • Clears any local variable space we may have allocated
  • Since we have not allocated any stack space - doesn't do anything

pop ebp

  • Restores the saved base pointer (ebp)
  • Removes saved base pointer from the stack
  • Sets up the stack frame for a return

ret

  • Return to the caller
  • Return code in eax: 0
  • main(): returns to startup code that calls exit()