TExcelTAD
Home › x86-64 Assembly › Assembly Exercises

Assembly Exercises

Time to practise! Each exercise has a starter playground that assembles and runs as it is, printing a placeholder result so you can see the format. Replace the TODO parts, press ▶ Run, and compare your output with the "Expected" lines. The helper routines at the bottom (print_uint, print_array, …) are the same as in the lessons; you don't need to change them. Try for a few minutes before opening a solution.

# Exercise Topics
1 Sum of even numbers loops, test
2 Power function procedures, imul
3 FizzBuzz division, conditionals, strings
4 Vowel counter strings, comparisons
5 Reverse a string in place two pointers, bytes
6 Largest element and its index arrays, cmov
7 Primes below 40 nested loops, div
8 Binary digits shifts, bit tests

1. Sum of even numbers

Add up all even numbers from 1 to 100 and print the total.

Expected: 2550

nasm · try it yourself
section .text
global _start
_start:
    xor eax, eax                ; total = 0
    ; TODO: loop over 1..100 and add only the even numbers
    mov rdi, rax
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
0
Show solution
nasm
section .text
global _start
_start:
    xor eax, eax                ; total = 0
    mov ecx, 1
.loop:
    test ecx, 1
    jnz .odd                    ; skip odd numbers
    add rax, rcx
.odd:
    inc ecx
    cmp ecx, 100
    jbe .loop
    mov rdi, rax
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret

Even simpler: start at 2 and add 2 each time, so there is nothing to test.

2. Power function

Write power(base, exp) that returns base raised to exp (both unsigned, exp may be 0) following the calling convention: rdi = base, rsi = exponent, result in rax.

Expected: 1594323 (3^13), 1 (7^0), 1024 (2^10).

nasm · try it yourself
section .text
global _start
_start:
    mov edi, 3
    mov esi, 13
    call power
    mov rdi, rax
    call print_uint
    mov edi, 7
    xor esi, esi
    call power
    mov rdi, rax
    call print_uint
    mov edi, 2
    mov esi, 10
    call power
    mov rdi, rax
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; power(base, exp) -> base ** exp
power:
    xor eax, eax                ; TODO: compute the real result
    ret

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
0
0
0
Show solution
nasm
section .text
global _start
_start:
    mov edi, 3
    mov esi, 13
    call power
    mov rdi, rax
    call print_uint
    mov edi, 7
    xor esi, esi
    call power
    mov rdi, rax
    call print_uint
    mov edi, 2
    mov esi, 10
    call power
    mov rdi, rax
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; power(base, exp) -> base ** exp
power:
    mov eax, 1                  ; result = 1 (correct for exp = 0)
.loop:
    test rsi, rsi
    jz .done
    imul rax, rdi
    dec rsi
    jmp .loop
.done:
    ret

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret

A faster version uses exponentiation by squaring: square base and halve exp (shr rsi, 1) each step, multiplying into the result whenever the bit shifted out is 1. That needs only about log2(exp) multiplications.

3. FizzBuzz

For the numbers 1 to 15, print Fizz for multiples of 3, Buzz for multiples of 5, FizzBuzz for multiples of both, and the number otherwise — one per line.

Expected (last lines): 13, 14, FizzBuzz.

nasm · try it yourself
section .text
global _start
_start:
    mov ebx, 1                  ; n (callee-saved: survives the calls)
.loop:
    ; TODO: test n % 15, n % 3 and n % 5 and print the right word
    mov edi, ebx
    call print_uint
    inc ebx
    cmp ebx, 15
    jbe .loop

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Show solution
nasm
section .data
fizzbuzz:   db "FizzBuzz", 10   ; "Fizz" and "Buzz" are parts of this string

section .text
global _start
_start:
    mov ebx, 1
.loop:
    mov eax, ebx
    xor edx, edx
    mov ecx, 15
    div rcx
    test edx, edx
    jz .print_fizzbuzz
    mov eax, ebx
    xor edx, edx
    mov ecx, 3
    div rcx
    test edx, edx
    jz .print_fizz
    mov eax, ebx
    xor edx, edx
    mov ecx, 5
    div rcx
    test edx, edx
    jz .print_buzz
    mov edi, ebx
    call print_uint
    jmp .next
.print_fizzbuzz:
    mov rsi, fizzbuzz
    mov edx, 9
    jmp .write
.print_fizz:
    mov byte [fizzbuzz + 4], 10 ; temporarily end the string after "Fizz"
    mov rsi, fizzbuzz
    mov edx, 5
    call write_out
    mov byte [fizzbuzz + 4], 'B'
    jmp .next
.print_buzz:
    mov rsi, fizzbuzz + 4       ; "Buzz\n" is the second half
    mov edx, 5
.write:
    call write_out
.next:
    inc ebx
    cmp ebx, 15
    jbe .loop

    mov eax, 60
    xor edi, edi
    syscall

; write_out(rsi = text, rdx = length)
write_out:
    mov eax, 1
    mov edi, 1
    syscall
    ret

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret

Testing n % 15 first matters, otherwise 15 would be caught by the % 3 test. Separate "Fizz", "Buzz" and "FizzBuzz" strings are clearer; overlapping them in one buffer is just a space-saving trick.

4. Vowel counter

Count the vowels (a e i o u, upper or lower case) in the text.

Expected: 14

nasm · try it yourself
section .data
text:       db "Assembly Language Is Our Favourite"
TEXT_LEN    equ $ - text

section .text
global _start
_start:
    xor edi, edi                ; count = 0
    ; TODO: loop over the TEXT_LEN bytes and count vowels
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
0
Show solution
nasm
section .data
text:       db "Assembly Language Is Our Favourite"
TEXT_LEN    equ $ - text
vowels:     db "aeiou"

section .text
global _start
_start:
    xor edi, edi                ; count = 0
    xor ecx, ecx                ; i = 0
.next_char:
    movzx eax, byte [text + rcx]
    or al, 0x20                 ; lowercase (harmless for non-letters here)
    xor edx, edx                ; j = 0
.next_vowel:
    cmp al, [vowels + rdx]
    je .is_vowel
    inc edx
    cmp edx, 5
    jb .next_vowel
    jmp .advance
.is_vowel:
    inc edi
.advance:
    inc ecx
    cmp ecx, TEXT_LEN
    jb .next_char
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret

5. Reverse a string in place

Reverse the bytes of word_text in memory (without a second buffer) and print it.

Expected: racecar si ylbmessa

nasm · try it yourself
section .data
word_text:  db "assembly is racecar"
WORD_LEN    equ $ - word_text
newline:    db 10

section .text
global _start
_start:
    ; TODO: swap bytes from both ends until the pointers meet

    mov eax, 1
    mov edi, 1
    mov rsi, word_text
    mov edx, WORD_LEN + 1       ; the text and the newline after it
    syscall

    mov eax, 60
    xor edi, edi
    syscall
assembly is racecar
Show solution
nasm
section .data
word_text:  db "assembly is racecar"
WORD_LEN    equ $ - word_text
newline:    db 10

section .text
global _start
_start:
    mov rsi, word_text                  ; left pointer
    lea rdi, [word_text + WORD_LEN - 1] ; right pointer
.swap:
    cmp rsi, rdi
    jae .done
    mov al, [rsi]
    mov dl, [rdi]
    mov [rsi], dl
    mov [rdi], al
    inc rsi
    dec rdi
    jmp .swap
.done:
    mov eax, 1
    mov edi, 1
    mov rsi, word_text
    mov edx, WORD_LEN + 1
    syscall

    mov eax, 60
    xor edi, edi
    syscall

6. Largest element and its index

Find the largest value in the array and the index of its first occurrence. Print the value, then the index.

Expected: 91, then 4.

nasm · try it yourself
section .data
temps:      dq 64, 72, 58, 85, 91, 77, 91, 60
COUNT       equ ($ - temps) / 8

section .text
global _start
_start:
    xor ebx, ebx                ; TODO: the largest value
    xor r12d, r12d              ; TODO: its index
    mov rdi, rbx
    call print_uint
    mov rdi, r12
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
0
0
Show solution
nasm
section .data
temps:      dq 64, 72, 58, 85, 91, 77, 91, 60
COUNT       equ ($ - temps) / 8

section .text
global _start
_start:
    mov rbx, [temps]            ; best value = element 0
    xor r12d, r12d              ; best index = 0
    mov ecx, 1
.loop:
    mov rax, [temps + rcx*8]
    cmp rax, rbx
    cmova rbx, rax              ; strictly greater: keeps the FIRST maximum
    cmova r12, rcx
    inc ecx
    cmp ecx, COUNT
    jb .loop
    mov rdi, rbx
    call print_uint
    mov rdi, r12
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret

Both cmova instructions use the flags of the same cmp, because cmov doesn't change the flags. With cmovae you would get the index of the last maximum (6).

7. Primes below 40

Collect all prime numbers below 40 in the primes array and print them with print_array. Use trial division: n is prime if no d with 2 ≤ d and d × d ≤ n divides it.

Expected: 2 3 5 7 11 13 17 19 23 29 31 37

nasm · try it yourself
section .bss
primes:     resq 20

section .text
global _start
_start:
    xor r12d, r12d              ; number of primes found
    ; TODO: for n = 2..39, test divisors and store primes in [primes + r12*8]
    mov qword [primes], 2       ; placeholder: just one prime
    inc r12

    mov rdi, primes
    mov rsi, r12
    call print_array

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_array: print rsi unsigned quadwords starting at rdi on one line ----
print_array:
    push rbx
    push r12
    mov rbx, rdi            ; rbx = address of the next element
    mov r12, rsi            ; r12 = elements left
    sub rsp, 32             ; digit buffer, as in print_uint
.element:
    test r12, r12
    jz .finish
    mov rax, [rbx]          ; the number to convert
    lea rsi, [rsp + 31]
    mov byte [rsi], ' '     ; a space after each number ...
    cmp r12, 1
    jne .convert
    mov byte [rsi], 10      ; ... except the last one, which ends the line
.convert:
    mov ecx, 10
.digit:
    xor edx, edx
    div rcx
    add dl, '0'
    dec rsi
    mov [rsi], dl
    test rax, rax
    jnz .digit
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; length of digits + separator
    mov eax, 1
    mov edi, 1
    syscall
    add rbx, 8              ; next quadword
    dec r12
    jmp .element
.finish:
    add rsp, 32
    pop r12
    pop rbx
    ret
2
Show solution
nasm
section .bss
primes:     resq 20

section .text
global _start
_start:
    xor r12d, r12d              ; count = 0
    mov ebx, 2                  ; n = 2
.next_n:
    mov ecx, 2                  ; d = 2
.try_divisor:
    mov eax, ecx
    imul eax, ecx
    cmp eax, ebx
    ja .is_prime                ; d * d > n: no divisor found
    mov eax, ebx
    xor edx, edx
    div ecx                     ; edx = n % d
    test edx, edx
    jz .not_prime
    inc ecx
    jmp .try_divisor
.is_prime:
    mov [primes + r12*8], rbx
    inc r12
.not_prime:
    inc ebx
    cmp ebx, 40
    jb .next_n

    mov rdi, primes
    mov rsi, r12
    call print_array

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_array: print rsi unsigned quadwords starting at rdi on one line ----
print_array:
    push rbx
    push r12
    mov rbx, rdi            ; rbx = address of the next element
    mov r12, rsi            ; r12 = elements left
    sub rsp, 32             ; digit buffer, as in print_uint
.element:
    test r12, r12
    jz .finish
    mov rax, [rbx]          ; the number to convert
    lea rsi, [rsp + 31]
    mov byte [rsi], ' '     ; a space after each number ...
    cmp r12, 1
    jne .convert
    mov byte [rsi], 10      ; ... except the last one, which ends the line
.convert:
    mov ecx, 10
.digit:
    xor edx, edx
    div rcx
    add dl, '0'
    dec rsi
    mov [rsi], dl
    test rax, rax
    jnz .digit
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; length of digits + separator
    mov eax, 1
    mov edi, 1
    syscall
    add rbx, 8              ; next quadword
    dec r12
    jmp .element
.finish:
    add rsp, 32
    pop r12
    pop rbx
    ret

div ecx is the 32-bit division (edx:eax divided by ecx), which is enough here and faster than the 64-bit form.

8. Binary digits

Print the 8-bit binary representation of the byte in value, most significant bit first, always 8 digits.

Expected: 00101101 (45)

nasm · try it yourself
section .data
value:      db 45

section .bss
digits:     resb 9

section .text
global _start
_start:
    ; TODO: fill digits[0..7] with '0'/'1' characters
    mov rdi, digits
    mov al, '?'
    mov ecx, 8
    rep stosb
    mov byte [digits + 8], 10

    mov eax, 1
    mov edi, 1
    mov rsi, digits
    mov edx, 9
    syscall

    mov eax, 60
    xor edi, edi
    syscall
????????
Show solution
nasm
section .data
value:      db 45

section .bss
digits:     resb 9

section .text
global _start
_start:
    movzx eax, byte [value]
    xor ecx, ecx                ; position 0..7
.bit:
    shl al, 1                   ; the top bit falls into CF
    mov dl, '0'
    adc dl, 0                   ; '0' + CF -> '0' or '1'
    mov [digits + rcx], dl
    inc ecx
    cmp ecx, 8
    jb .bit
    mov byte [digits + 8], 10

    mov eax, 1
    mov edi, 1
    mov rsi, digits
    mov edx, 9
    syscall

    mov eax, 60
    xor edi, edi
    syscall

Shifting left moves each bit, from the top down, into the carry flag, and adc turns it into a digit without a branch.

Test yourself

In the FizzBuzz solution, why is the counter kept in ebx?

  1. div needs it there
  2. rbx is callee-saved, so print_uint and the syscalls don't destroy it
  3. ebx is faster
  4. Any register would do

In exercise 6, why does cmova (not cmovae) find the first maximum?

  1. cmova is faster
  2. It only replaces the best value when the new one is strictly greater, so equal later values are ignored
  3. cmovae doesn't exist
  4. Because the array is sorted

What must you do before a 64-bit unsigned div rcx?

  1. cqo
  2. xor edx, edx
  3. neg rax
  4. Nothing