Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. asm-101
  3. reverse-bytes
4 min read

Reverse Bytes

The capstone for this section. Reversing a buffer in place pulls together everything so far: bytes and memory from the Basics, the loop and comparison from Control Flow, and load/store addressing. It is also the shape of real routines you will meet when reversing and exploiting binaries.

Problem

Reverse a byte buffer in place — without a second buffer. Use two pointers that start at opposite ends and walk toward each other, swapping the bytes they point at, until they meet in the middle. Print the result to confirm it worked.

Hints

Open one hint at a time before reading the walk-through.

Hint 1
You need two pointers: one at the first byte, one at the last (buf + len - 1). lea computes those addresses without reading memory.
Hint 2
Swapping two values needs a temporary. Load each byte into an 8-bit register (al, bl), then store each one back at the other pointer's address.
Hint 3
After each swap, inc the left pointer and dec the right. Stop when they meet or cross — cmp the two and jae out of the loop.
Hint 4
"In place" means you write back into the same bytes, so the buffer must live in writable .data, not read-only .rodata. Writing to read-only memory faults.

Walk-through

Keep the left pointer in r8 and the right in r9, set with lea to buf and buf + buflen - 1. Each iteration first checks the stop condition: cmp r8, r9 then jae .done leaves the loop once the left pointer has reached or passed the right one.

The swap itself is four moves through 8-bit registers, since we are moving single bytes: load [r8] into al and [r9] into bl, then store bl into [r8] and al into [r9]. With the two bytes exchanged, inc r8 and dec r9 step the pointers inward and jmp .loop goes back for the next pair. Watch the buffer flip in memory — before the loop, and again once the pointers have met:

gdb
gef➤ break _startBreakpoint 1 at 0x401000gef➤ runBreakpoint 1, 0x0000000000401000 in _start ()gef➤ x/6xb &buf0x402000:       0x41    0x42    0x43    0x44    0x45    0x46gef➤ break *0x401029Breakpoint 2 at 0x401029gef➤ continueBreakpoint 2, 0x0000000000401029 in _start.done ()gef➤ x/6xb &buf0x402000:       0x46    0x45    0x44    0x43    0x42    0x41

Those six bytes are ASCII codes: 0x41–0x46 spell ABCDEF before the loop, and afterward they read 0x46–0x41, which is FEDCBA — reversed. Same addresses, same bytes; nothing was copied anywhere else, each byte was simply swapped with its mirror.

Solution

nasm
1section .data
2    buf db "ABCDEF"
3    buflen equ $ - buf
4    nl  db 10

shell
$ ./revFEDCBA

The buffer came out reversed, and it never used more than the one buffer plus two single-byte temporaries — exactly the in-place algorithm the problem asked for.

5
6section .text
7 global _start
8_start:
9 lea r8, [buf] ; left pointer
10 lea r9, [buf + buflen - 1] ; right pointer
11.loop:
12 cmp r8, r9 ; pointers met or crossed?
13 jae .done
14 mov al, [r8] ; swap the two bytes
15 mov bl, [r9]
16 mov [r8], bl
17 mov [r9], al
18 inc r8 ; step inward
19 dec r9
20 jmp .loop
21.done:
22 mov rax, 1 ; write(1, buf, buflen)
23 mov rdi, 1
24 lea rsi, [buf]
25 mov rdx, buflen
26 syscall
27 mov rax, 1 ; write the newline
28 mov rdi, 1
29 lea rsi, [nl]
30 mov rdx, 1
31 syscall
32 mov rax, 60 ; exit(0)
33 xor rdi, rdi
34 syscall