TExcelTAD
Home › x86-64 Assembly › Mini Project: Score Report

Mini Project: Score Report

In this project you will build a complete program, step by step: a score report for a class test. Given a list of scores, it prints the statistics a teacher wants to see — count, total, average, minimum, maximum and median — and a histogram of letter grades. It brings together most of the course: data directives, loops, arrays, procedures following the calling convention, sorting, macros and number printing.

The final output will look like this:

text
=== Class test report ===
Scores:  88 72 95 61 79 84 55 90 67 73 98 81
Sorted:  55 61 67 72 73 79 81 84 88 90 95 98
Count:   12
Average: 78.58
Range:   55 - 98
Median:  80.00
A | ***
B | ***
C | ***
D | **
F | *

Every step is a complete program you can run and modify. The helpers at the bottom (print_array, print_uint, print_fixed2) are exactly the ones from the lessons.

Step 1: The data

Start with the scores as an array of quadwords and print them. The count is computed by NASM from the size of the data, so adding a score needs no other change:

nasm · try it yourself
section .data
scores:     dq 88, 72, 95, 61, 79, 84, 55, 90, 67, 73, 98, 81
COUNT       equ ($ - scores) / 8

section .text
global _start
_start:
    mov rdi, scores
    mov esi, COUNT
    call print_array

    mov edi, COUNT
    call print_uint

    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

; ---- 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
88 72 95 61 79 84 55 90 67 73 98 81
12

Step 2: Statistics in one pass

A function stats(array, count) walks the array once and stores the total, the minimum and the maximum in a small result structure. Returning several values through a pointer is a common pattern when two registers aren't enough. A print_text macro prints labels like Total: without a newline, so that the number follows on the same line.

nasm · try it yourself
%macro print_text 1             ; print a string literal (no newline)
    section .rodata
    %%text: db %1
    %%len   equ $ - %%text
    section .text
    mov eax, 1
    mov edi, 1
    mov rsi, %%text
    mov edx, %%len
    syscall
%endmacro

struc stats_result
    .total:     resq 1
    .min:       resq 1
    .max:       resq 1
endstruc

section .data
scores:     dq 88, 72, 95, 61, 79, 84, 55, 90, 67, 73, 98, 81
COUNT       equ ($ - scores) / 8

section .bss
result:     resb stats_result_size

section .text
global _start
_start:
    mov rdi, scores
    mov esi, COUNT
    mov rdx, result
    call stats

    print_text "Total:   "
    mov rdi, [result + stats_result.total]
    call print_uint
    print_text "Minimum: "
    mov rdi, [result + stats_result.min]
    call print_uint
    print_text "Maximum: "
    mov rdi, [result + stats_result.max]
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; stats(rdi = array, rsi = count >= 1, rdx = pointer to a stats_result)
stats:
    xor eax, eax                ; total
    mov r8, [rdi]               ; min = first element
    mov r9, r8                  ; max = first element
    xor ecx, ecx
.loop:
    mov r10, [rdi + rcx*8]
    add rax, r10
    cmp r10, r8
    cmovb r8, r10               ; new minimum
    cmp r10, r9
    cmova r9, r10               ; new maximum
    inc rcx
    cmp rcx, rsi
    jb .loop
    mov [rdx + stats_result.total], rax
    mov [rdx + stats_result.min], r8
    mov [rdx + stats_result.max], r9
    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
Total:   943
Minimum: 55
Maximum: 98

Step 3: The average with two decimals

The average is 943 / 12 = 78.5833…. To print it with two decimals, compute it in hundredths with integer arithmetic: (total × 100 + count / 2) / count. Adding half the divisor before dividing rounds to the nearest hundredth instead of truncating. No floating point needed:

nasm · try it yourself
section .data
scores:     dq 88, 72, 95, 61, 79, 84, 55, 90, 67, 73, 98, 81
COUNT       equ ($ - scores) / 8

section .text
global _start
_start:
    xor eax, eax                ; total
    xor ecx, ecx
.sum:
    add rax, [scores + rcx*8]
    inc ecx
    cmp ecx, COUNT
    jb .sum

    mov rdi, rax
    mov esi, COUNT
    call average_x100
    mov rdi, rax
    call print_fixed2

    mov eax, 60
    xor edi, edi
    syscall

; average_x100(total, count) -> round(total * 100 / count)
average_x100:
    imul rax, rdi, 100
    mov rdx, rsi
    shr rdx, 1
    add rax, rdx                ; + count / 2: round to nearest
    xor edx, edx
    div rsi
    ret

; ---- print_fixed2: print rdi hundredths as a decimal number with 2 places ----
print_fixed2:
    mov rax, rdi            ; e.g. 31415 -> "314.15"
    sub rsp, 32
    lea rsi, [rsp + 31]
    mov byte [rsi], 10
    mov ecx, 10
    xor r8d, r8d            ; digits written so far
.digit:
    cmp r8d, 2
    jne .no_point
    dec rsi
    mov byte [rsi], '.'     ; the point goes after two digits
.no_point:
    xor edx, edx
    div rcx
    add dl, '0'
    dec rsi
    mov [rsi], dl
    inc r8d
    cmp r8d, 3
    jb .digit               ; always at least three digits: "0.05"
    test rax, rax
    jnz .digit
    lea rdx, [rsp + 32]
    sub rdx, rsi
    mov eax, 1
    mov edi, 1
    syscall
    add rsp, 32
    ret
78.58

Step 4: Sorting and the median

The median is the middle value of the sorted list; for an even count it is the average of the two middle values. Sort a copy of the scores with insertion sort (from the Sorting lesson, now as a function), then compute the median in hundredths — (a + b) × 50 for the two middle values a and b:

nasm · try it yourself
section .data
scores:     dq 88, 72, 95, 61, 79, 84, 55, 90, 67, 73, 98, 81
COUNT       equ ($ - scores) / 8

section .bss
sorted:     resq COUNT

section .text
global _start
_start:
    mov rsi, scores             ; copy the scores: keep the original order
    mov rdi, sorted
    mov ecx, COUNT
    rep movsq

    mov rdi, sorted
    mov esi, COUNT
    call insertion_sort
    mov rdi, sorted
    mov esi, COUNT
    call print_array

    mov rdi, sorted
    mov esi, COUNT
    call median_x100
    mov rdi, rax
    call print_fixed2

    mov eax, 60
    xor edi, edi
    syscall

; insertion_sort(rdi = array of unsigned quadwords, rsi = count)
insertion_sort:
    mov ecx, 1                  ; i = 1
.outer:
    cmp rcx, rsi
    jae .done
    mov rax, [rdi + rcx*8]      ; key
    mov rdx, rcx                ; j = i
.shift:
    test rdx, rdx
    jz .insert
    mov r8, [rdi + rdx*8 - 8]
    cmp r8, rax
    jbe .insert
    mov [rdi + rdx*8], r8
    dec rdx
    jmp .shift
.insert:
    mov [rdi + rdx*8], rax
    inc rcx
    jmp .outer
.done:
    ret

; median_x100(rdi = sorted array, rsi = count >= 1) -> median * 100
median_x100:
    mov rcx, rsi
    shr rcx, 1                  ; middle index (upper middle for even counts)
    mov rax, [rdi + rcx*8]
    test esi, 1
    jz .even
    imul rax, rax, 100          ; odd count: the middle value
    ret
.even:
    add rax, [rdi + rcx*8 - 8]  ; the two middle values
    imul rax, rax, 50           ; (a + b) / 2 * 100
    ret

; ---- 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

; ---- print_fixed2: print rdi hundredths as a decimal number with 2 places ----
print_fixed2:
    mov rax, rdi            ; e.g. 31415 -> "314.15"
    sub rsp, 32
    lea rsi, [rsp + 31]
    mov byte [rsi], 10
    mov ecx, 10
    xor r8d, r8d            ; digits written so far
.digit:
    cmp r8d, 2
    jne .no_point
    dec rsi
    mov byte [rsi], '.'     ; the point goes after two digits
.no_point:
    xor edx, edx
    div rcx
    add dl, '0'
    dec rsi
    mov [rsi], dl
    inc r8d
    cmp r8d, 3
    jb .digit               ; always at least three digits: "0.05"
    test rax, rax
    jnz .digit
    lea rdx, [rsp + 32]
    sub rdx, rsi
    mov eax, 1
    mov edi, 1
    syscall
    add rsp, 32
    ret
55 61 67 72 73 79 81 84 88 90 95 98
80.00

rep movsq copies rcx quadwords from [rsi] to [rdi] — the 8-byte sibling of rep movsb.

Step 5: The grade histogram

Each score maps to a grade: A (90+), B (80–89), C (70–79), D (60–69), F (below 60). A table of thresholds turns the if/else chain into a loop, and a counts array collects the number of scores per grade. Then every grade gets a bar of stars:

nasm · try it yourself
section .data
scores:     dq 88, 72, 95, 61, 79, 84, 55, 90, 67, 73, 98, 81
COUNT       equ ($ - scores) / 8
thresholds: dq 90, 80, 70, 60, 0        ; lower bound of A, B, C, D, F
letters:    db "ABCDF"
GRADES      equ 5

section .bss
counts:     resq GRADES
bar:        resb 64

section .text
global _start
_start:
    xor ecx, ecx                ; score index
.next_score:
    mov rax, [scores + rcx*8]
    xor edx, edx                ; grade index
.find_grade:
    cmp rax, [thresholds + rdx*8]
    jae .found                  ; score >= lower bound
    inc edx
    jmp .find_grade             ; the last bound is 0, so this always ends
.found:
    inc qword [counts + rdx*8]
    inc ecx
    cmp ecx, COUNT
    jb .next_score

    xor ebx, ebx                ; grade index (callee-saved: survives syscall)
.print_bar:
    mov al, [letters + rbx]
    mov [bar], al
    mov dword [bar + 1], ' | '  ; three characters (the 4th byte is 0)
    mov rcx, [counts + rbx*8]
    lea rdi, [bar + 4]
    mov al, '*'
    rep stosb                   ; one star per score
    mov byte [rdi], 10
    lea rdx, [rdi + 1]
    sub rdx, bar                ; length of the line
    mov eax, 1
    mov edi, 1
    mov rsi, bar
    syscall
    inc ebx
    cmp ebx, GRADES
    jb .print_bar

    mov eax, 60
    xor edi, edi
    syscall
A | ***
B | ***
C | ***
D | **
F | *

Step 6: The complete report

Finally, put the pieces together as functions and call them from a short _start. Notice how readable the main program has become: it is mostly a sequence of calls, like a C main:

nasm · try it yourself
%macro print_text 1             ; print a string literal (no newline)
    section .rodata
    %%text: db %1
    %%len   equ $ - %%text
    section .text
    mov eax, 1
    mov edi, 1
    mov rsi, %%text
    mov edx, %%len
    syscall
%endmacro

section .data
scores:     dq 88, 72, 95, 61, 79, 84, 55, 90, 67, 73, 98, 81
COUNT       equ ($ - scores) / 8
thresholds: dq 90, 80, 70, 60, 0
letters:    db "ABCDF"
GRADES      equ 5

section .bss
sorted:     resq COUNT
counts:     resq GRADES
bar:        resb 64

section .text
global _start
_start:
    print_text `=== Class test report ===\n`
    print_text "Scores:  "
    mov rdi, scores
    mov esi, COUNT
    call print_array

    mov rsi, scores             ; sorted copy
    mov rdi, sorted
    mov ecx, COUNT
    rep movsq
    mov rdi, sorted
    mov esi, COUNT
    call insertion_sort
    print_text "Sorted:  "
    mov rdi, sorted
    mov esi, COUNT
    call print_array

    print_text "Count:   "
    mov edi, COUNT
    call print_uint

    print_text "Average: "
    mov rdi, sorted
    mov esi, COUNT
    call sum
    mov rdi, rax
    mov esi, COUNT
    call average_x100
    mov rdi, rax
    call print_fixed2

    print_text "Range:   "              ; the sorted array makes min/max trivial
    mov rdi, [sorted]
    call print_uint_no_newline
    print_text " - "
    mov rdi, [sorted + (COUNT - 1) * 8]
    call print_uint

    print_text "Median:  "
    mov rdi, sorted
    mov esi, COUNT
    call median_x100
    mov rdi, rax
    call print_fixed2

    call histogram

    mov eax, 60
    xor edi, edi
    syscall

; sum(rdi = array, rsi = count) -> total
sum:
    xor eax, eax
    xor ecx, ecx
.loop:
    cmp rcx, rsi
    jae .done
    add rax, [rdi + rcx*8]
    inc rcx
    jmp .loop
.done:
    ret

; average_x100(total, count) -> round(total * 100 / count)
average_x100:
    imul rax, rdi, 100
    mov rdx, rsi
    shr rdx, 1
    add rax, rdx
    xor edx, edx
    div rsi
    ret

; insertion_sort(rdi = array of unsigned quadwords, rsi = count)
insertion_sort:
    mov ecx, 1
.outer:
    cmp rcx, rsi
    jae .done
    mov rax, [rdi + rcx*8]
    mov rdx, rcx
.shift:
    test rdx, rdx
    jz .insert
    mov r8, [rdi + rdx*8 - 8]
    cmp r8, rax
    jbe .insert
    mov [rdi + rdx*8], r8
    dec rdx
    jmp .shift
.insert:
    mov [rdi + rdx*8], rax
    inc rcx
    jmp .outer
.done:
    ret

; median_x100(rdi = sorted array, rsi = count >= 1) -> median * 100
median_x100:
    mov rcx, rsi
    shr rcx, 1
    mov rax, [rdi + rcx*8]
    test esi, 1
    jz .even
    imul rax, rax, 100
    ret
.even:
    add rax, [rdi + rcx*8 - 8]
    imul rax, rax, 50
    ret

; histogram: count grades of scores[] and print one bar per grade
histogram:
    push rbx
    xor ecx, ecx
.next_score:
    mov rax, [scores + rcx*8]
    xor edx, edx
.find_grade:
    cmp rax, [thresholds + rdx*8]
    jae .found
    inc edx
    jmp .find_grade
.found:
    inc qword [counts + rdx*8]
    inc ecx
    cmp ecx, COUNT
    jb .next_score
    xor ebx, ebx
.print_bar:
    mov al, [letters + rbx]
    mov [bar], al
    mov dword [bar + 1], ' | '
    mov rcx, [counts + rbx*8]
    lea rdi, [bar + 4]
    mov al, '*'
    rep stosb
    mov byte [rdi], 10
    lea rdx, [rdi + 1]
    sub rdx, bar
    mov eax, 1
    mov edi, 1
    mov rsi, bar
    syscall
    inc ebx
    cmp ebx, GRADES
    jb .print_bar
    pop rbx
    ret

; print_uint_no_newline: like print_uint, but without the final newline
print_uint_no_newline:
    mov rax, rdi
    sub rsp, 32
    lea rsi, [rsp + 32]         ; start right at the end: no newline
    mov ecx, 10
.next_digit:
    xor edx, edx
    div rcx
    add dl, '0'
    dec rsi
    mov [rsi], dl
    test rax, rax
    jnz .next_digit
    lea rdx, [rsp + 32]
    sub rdx, rsi
    mov eax, 1
    mov edi, 1
    syscall
    add rsp, 32
    ret

; ---- 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

; ---- print_fixed2: print rdi hundredths as a decimal number with 2 places ----
print_fixed2:
    mov rax, rdi            ; e.g. 31415 -> "314.15"
    sub rsp, 32
    lea rsi, [rsp + 31]
    mov byte [rsi], 10
    mov ecx, 10
    xor r8d, r8d            ; digits written so far
.digit:
    cmp r8d, 2
    jne .no_point
    dec rsi
    mov byte [rsi], '.'     ; the point goes after two digits
.no_point:
    xor edx, edx
    div rcx
    add dl, '0'
    dec rsi
    mov [rsi], dl
    inc r8d
    cmp r8d, 3
    jb .digit               ; always at least three digits: "0.05"
    test rax, rax
    jnz .digit
    lea rdx, [rsp + 32]
    sub rdx, rsi
    mov eax, 1
    mov edi, 1
    syscall
    add rsp, 32
    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
=== Class test report ===
Scores:  88 72 95 61 79 84 55 90 67 73 98 81
Sorted:  55 61 67 72 73 79 81 84 88 90 95 98
Count:   12
Average: 78.58
Range:   55 - 98
Median:  80.00
A | ***
B | ***
C | ***
D | **
F | *

The backquoted string `=== Class test report ===\n` uses NASM's C-style escapes, so \n becomes a newline byte inside the macro argument.

Ideas for extending the project

  1. Signed data. Allow negative values (for example temperature changes): switch the comparisons to signed jumps and cmovl/cmovg, and print with print_int.
  2. Standard deviation. Compute the variance with integer arithmetic in hundredths, then take the square root with sqrtsd (see Floating Point with SSE).
  3. Input. Read scores as text from standard input with the read system call and parse the digits (multiply by 10 and add each digit) — the reverse of print_uint.
  4. Exit status. Return a non-zero exit status if any student failed, so a shell script can react to it.
  5. Faster sorting. Replace insertion sort with a recursive quicksort, and test it against the simple version on the same data.

Test yourself

Why does the report compute the average in hundredths with integers?

  1. Floating point is impossible in assembly
  2. So it can be printed with an integer routine and rounded exactly with integer division
  3. Integers are always more accurate
  4. To save memory

What does rep movsq with rcx = 12 do?

  1. Copies 12 bytes
  2. Copies 12 quadwords (96 bytes) from [rsi] to [rdi]
  3. Moves rsi into rdi 12 times
  4. Sorts 12 values

Why does the histogram function push rbx?

  1. To align the stack
  2. rbx is callee-saved: the function uses it, so it must restore the caller's value
  3. syscall needs it
  4. It doesn't need to