Advertisement
drankinatty

Nasm x86 Use Stack For Temp Storage

May 5th, 2022
1,636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; strn (string const) macro is overloaded to take 1 or 2 args
  2. ; the address and optionally the number of chars to output
  3. ; (default 1)
  4. %macro  strn  1
  5.   mov     eax,  4
  6.   mov     ebx,  1
  7.   mov     ecx, %1
  8.   mov     edx,  1
  9.   int     0x80
  10. %endmacro
  11.  
  12. %macro  strn  2
  13.   mov     eax,  4
  14.   mov     ebx,  1
  15.   mov     ecx, %1
  16.   mov     edx, %2
  17.   int     0x80
  18. %endmacro
  19.  
  20. section .data
  21.  
  22.   nwln db 0xa                             ; newline
  23.   abet db "abcdefghijklmnopqrstuvwxyz"    ; alphabet
  24.   ablen equ $ - abet                      ; alphabet len
  25.  
  26. section .bss
  27.  
  28. section .text
  29.       global _start
  30.  
  31.   _start:
  32.       sub     esp, 32             ; grow stack by 32-bytes
  33.      
  34.       xor     ecx, ecx            ; zero ecx
  35.       mov     cl, ablen           ; set count register to ablen
  36.      
  37.       mov     esi, abet           ; address of source in esi
  38.       mov     edi, esp            ; address of dest in edi
  39.       rep     movsb               ; copy string to stack at esp
  40.      
  41.       strn    abet, ablen         ; output string from abet
  42.       strn    nwln                ; output newline
  43.      
  44.       strn    esp, ablen          ; output string from copy
  45.       strn    nwln                ; output newline
  46.      
  47.       lea     esi, [abet + 14]    ; address of 15th, 'o' from abet
  48.       strn    esi                 ; output char
  49.       strn    nwln                ; output newline
  50.      
  51.       lea     esi, [esp + 14]     ; address of 15th, 'o' from copy
  52.       strn    esi                 ; output char
  53.       strn    nwln                ; output newline
  54.      
  55.       lea     esi, [esp + ablen - 1]  ; load addr of 'z' in esi
  56.      
  57.   prnchar:                        ; print from copy in reverse
  58.       strn    esi                 ; print char at esi
  59.       dec     esi                 ; decrement address
  60.       cmp     esi, esp            ; compare to esp
  61.       jge prnchar                 ; loop through esi == esp
  62.       strn    nwln                ; output newline
  63.      
  64.       mov     esi, esp            ; load copy to esi
  65.      
  66.   eachchar:                       ; print every other char
  67.       strn    esi                 ; print char at esi
  68.       inc     esi                 ; increment address at esi
  69.       mov     al, 0               ; load 0 in al
  70.       cmp     al, byte[esi]       ; compare byte
  71.       je      exit32              ; if end, jump to exit32
  72.       inc     esi                 ; skip to next char
  73.       cmp     al, byte[esi]       ; compare byte
  74.       jne     eachchar            ; repeat until nul-char reached
  75.      
  76.   exit32:
  77.       strn    nwln                ; output newline
  78.      
  79.       add     esp, 32             ; restore stack pointer address
  80.      
  81.       xor     ebx, ebx            ; set zero exit code
  82.       mov     eax, 1              ; syscall __NR_exit 1 (0x1 hex)
  83.       int     0x80                ; call kernel
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement