Munashedov

String Reverse (Not actually NASM)

Apr 24th, 2023
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Reverse a null-terminated string in-place
  2. ; using the stack.
  3.  
  4. .text 0
  5.     mov si, 32      ; Move our si to start of string
  6.     push 0          ; Push 0 as a marker for stack rewind
  7. read:              
  8.     mov acc, [si]   ; Get a letter into acc
  9.     jnz collect     ; Check if we hit '\0'
  10.     jmp rewind      
  11. collect:            
  12.     push acc        ; Push current char onto the stack
  13.     add si, 1       ; Advance our si pointer
  14.     jmp read        
  15.                    
  16. rewind:            
  17.     mov si, 32      ; Reload si to start of string
  18. write:              
  19.     pop acc         ; Pop off last letter
  20.     mov [si], acc   ; Write it into the string
  21.     add si, 1       ; Advance si
  22.     jnz write       ; Repeat if acc isn't zero
  23.     jmp exit        ;
  24. exit:                
  25.                     ; We're done
  26.  
  27. .data 32
  28. "Hello world."
  29.  
Add Comment
Please, Sign In to add comment