Menu
CoddyTech

Reversing Text

Part of the Functions, Stack and Strings section of Coddy's Assembly journey. Lesson 25 of 33.

To reverse a text, find its length first. The last byte of the text then goes to position 0 of the buffer, the byte before it to position 1, and so on: byte i of the result is byte length - 1 - i of the text.

Two index registers do it: rcx walks the text backwards from the end, and r8 walks the buffer forwards from 0.

.copy:
    test rcx, rcx
    jz .done
    dec rcx                     ; the byte before
    movzx edx, byte [rdi + rcx]
    mov [rsi + r8], dl
    inc r8                      ; the next free byte
    jmp .copy
.done:

The stack reverses too: push every byte, then pop them back into the buffer. Last in, first out returns the last byte first.

.push_bytes:
    movzx eax, byte [rdi + rcx]
    test eax, eax
    jz .pop_bytes
    push rax
    inc rcx
    jmp .push_bytes

Each push takes 8 bytes, even for one character, so a 255-byte text uses about 2 KB of stack. Pop as many times as you pushed, or ret finds a byte instead of its return address.

challenge icon

Challenge

Easy

solve receives the address of a text in rdi, ending with a zero byte, and a buffer in rsi. Write the text into the buffer backwards, last byte first, and return its length in rax.

Example: for the input hello the program prints olleh.

Write your code in solution.asm. The locked main.asm reads one line of text, calls solve and prints the first rax bytes of the buffer.

Try it yourself

bits 64
section .data
    changed_rbx db 'solve changed rbx', 10
    changed_rbp db 'solve changed rbp', 10
    changed_r12 db 'solve changed r12', 10
    changed_r13 db 'solve changed r13', 10
    changed_r14 db 'solve changed r14', 10
    changed_r15 db 'solve changed r15', 10
section .bss
    input_buffer resb 257
    text_buffer resb 257
section .text
    global _start
    extern solve
_start:
    xor eax, eax
    xor edi, edi
    mov rsi, input_buffer
    mov edx, 255
    syscall
    test rax, rax
    jg .trim
    xor eax, eax
.trim:
    test rax, rax
    jz .ended
    movzx edx, byte [input_buffer + rax - 1]
    cmp dl, 10
    je .drop
    cmp dl, 13
    jne .ended
.drop:
    dec rax
    jmp .trim
.ended:
    mov byte [input_buffer + rax], 0
    mov rbx, 0x1111111111111111
    mov rbp, 0x2222222222222222
    mov r12, 0x3333333333333333
    mov r13, 0x4444444444444444
    mov r14, 0x5555555555555555
    mov r15, 0x6666666666666666
    lea rdi, [input_buffer]
    lea rsi, [text_buffer]
    xor eax, eax
    call solve
    mov rdx, 0x1111111111111111
    lea rsi, [changed_rbx]
    cmp rbx, rdx
    jne .changed
    mov rdx, 0x2222222222222222
    lea rsi, [changed_rbp]
    cmp rbp, rdx
    jne .changed
    mov rdx, 0x3333333333333333
    lea rsi, [changed_r12]
    cmp r12, rdx
    jne .changed
    mov rdx, 0x4444444444444444
    lea rsi, [changed_r13]
    cmp r13, rdx
    jne .changed
    mov rdx, 0x5555555555555555
    lea rsi, [changed_r14]
    cmp r14, rdx
    jne .changed
    mov rdx, 0x6666666666666666
    lea rsi, [changed_r15]
    cmp r15, rdx
    jne .changed
    cmp rax, 256
    jbe .length_ok
    mov eax, 256
.length_ok:
    mov byte [text_buffer + rax], 10
    lea rdx, [rax + 1]
    lea rsi, [text_buffer]
    mov eax, 1
    mov edi, 1
    syscall
.exit:
    mov eax, 60
    xor edi, edi
    syscall
.changed:
    mov eax, 1
    mov edi, 1
    mov edx, 18
    syscall
    jmp .exit

All lessons in Functions, Stack and Strings

Practice on your own: Assembly playground