Skip to content

Instantly share code, notes, and snippets.

@exallium
Created March 10, 2012 20:53
Show Gist options
  • Save exallium/2013145 to your computer and use it in GitHub Desktop.
Save exallium/2013145 to your computer and use it in GitHub Desktop.
YASM/NASM Reverse a string w.o. copy for x64 Linux
; ------------------------
; reverse.asm - Reverse a string without copying it
; Max length of string is 64 bytes
; This is for an x86_64 Linux System
; Assemble with:
; yasm -f elf64 reverse.asm
; Link with:
; ld -o reverse reverse.o
; Note: Also perfectly capable of swapping yasm for nasm without changing code
; ------------------------
[bits 64]
section .data
msg times 64 db 0
max equ 64
section .text
global _start
_start:
; Read
mov rdx,max
mov rsi,msg
mov rdi,0
mov rax,0
syscall
mov rcx,rax ; Copy the string for later
mov rdi,msg ; Set RDI and RSI to point at message
mov rsi,msg ;
add rdi,rax ; RDI should point at last character in message
dec rdi ;
shr rax,1 ; Divide length by 2
.loop ; Begin loop:
mov bl,[rsi] ; Swap the characters using 8 bit registers
mov bh,[rdi] ;
mov [rsi],bh ;
mov [rdi],bl ;
inc rsi ; Increment rsi (which is a pointer)
dec rdi ; Decrement rdi (also a pointer)
dec rax ; Decrement our counter
jnz .loop ; If our counter isn't zero, keep looping
; Write
.write
mov rdx,rcx ; nbytes
mov rsi,msg ; *msg
mov rdi,1 ; stream
mov rax,1 ; syscall
syscall
; Exit
mov rdi,0 ; exit code
mov rax,60 ; syscall
syscall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment