Munashedov

string_reversal.asm

May 15th, 2023 (edited)
418
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 ax, 48
  6.     mov si, ax      ; Move our si to start of string
  7.     push 0          ; Push 0 as a marker for stack rewind
  8. read:              
  9.     mov ax, [si]    ; Get a letter into ax
  10.     shl ax, 8
  11.     shr ax, 8
  12.     jnz collect     ; Check if we hit '\0'
  13.     jmp rewind      
  14. collect:            
  15.     push ax         ; Push current char onto the stack
  16.     mov ax, si
  17.     add ax, 1
  18.     mov si, ax      ; Advance our si pointer
  19.     jmp read        
  20.  
  21. rewind:
  22.     mov ax, 48
  23.     mov si, ax      ; Reload si to start of string
  24. write:              
  25.     pop ax          ; Pop off last letter
  26.     shr ax, 8
  27.     mov [si], ax    ; Write it into the string
  28.     push ax         ; Store it so we can jnz
  29.     mov ax, si
  30.     add ax, 1
  31.     mov si, ax      ; Advance si
  32.     pop ax
  33.     jnz write       ; Repeat if ax isn't zero
  34.     jmp exit        ;
  35. exit:                
  36.                     ; We're done
  37.  
  38. .data 48
  39. "Hello world."
  40.  
Add Comment
Please, Sign In to add comment