Advertisement
Guest User

Hello World! example

a guest
Apr 21st, 2020
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. global _start
  2.  
  3. section .text
  4.     ; function to print reversed text by dword
  5.     ; text string should end in null
  6.     ; ie 'e','l','p','m','a','x','e',0
  7.     ; would print 'example'
  8.     print:
  9.         ; stores esp pointer for later
  10.         push ebp
  11.         mov ebp, esp
  12.        
  13.         ; sets eax to first char
  14.         mov eax, esp
  15.         add eax, 8
  16.        
  17.         ; sets ebx to end-of-string identifier, being 0/null/''
  18.         mov ebx, ''
  19.        
  20.         ; loop through string, reversing it into the correct order
  21.         mainloop:
  22.         cmp [eax], ebx ; check if end-of-string
  23.         je endloop ; if end of string, quit loop to endloop:
  24.        
  25.         ; push char and continue to next byte
  26.         push dword [eax]
  27.         add eax, 4
  28.         jmp mainloop
  29.        
  30.         endloop:
  31.        
  32.         ; reset start of string
  33.         mov edx, eax
  34.         sub edx, esp
  35.         sar edx, 1
  36.        
  37.         ; display string
  38.         mov ecx, esp
  39.         mov ebx, 1
  40.         mov eax, 4
  41.         int 0x80
  42.        
  43.         ; return to program
  44.         mov esp, ebp
  45.         pop ebp
  46.         ret
  47.        
  48.     _start:
  49.    
  50.         push dword '' ; set end-of-string, being 0/null/''
  51.        
  52.         push dword 'H'
  53.         push dword 'e'
  54.         push dword 'l'
  55.         push dword 'l'
  56.         push dword 'o'
  57.        
  58.         call print ; call to print 'Hello'
  59.        
  60.         push dword '' ; set end-of-string, being 0/null/''
  61.        
  62.         push dword 10 ; new line
  63.        
  64.         push dword 'W'
  65.         push dword 'o'
  66.         push dword 'r'
  67.         push dword 'l'
  68.         push dword 'd'
  69.         push dword '!'
  70.        
  71.         call print ; call to print newline + 'World!'
  72.        
  73.         mov eax, 1      ;system call number (sys_exit)
  74.         int 0x80        ;call kernel
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement