Menu
CoddyTech

The Full Report

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

challenge icon

Challenge

Easy

Print the three counts on one line, like wc: the lines, the words and the characters, with one space between them and a newline at the end. Print each number with print_number and each space with print_char (rdi = 32). Exit with code 0.

Example: for the input hello world the program prints 1 2 11.

Write the whole program in main.asm. The locked lib.asm holds print_number and print_char.

Try it yourself

bits 64
extern print_number
extern print_char
section .bss
    buffer resb 4096
section .text
    global _start
_start:
    mov rax, 0
    mov rdi, 0
    mov rsi, buffer
    mov rdx, 4096
    syscall
    mov rbx, rax
    xor r12, r12
    xor r13, r13
    xor r14, r14
    xor r15, r15
    mov r8, 1
.next:
    cmp r12, rbx
    jge .done
    movzx eax, byte [buffer + r12]
    cmp al, 10
    je .newline
    inc r13
    cmp al, 32
    je .space
    test r8, r8
    jz .after
    inc r15
    xor r8, r8
    jmp .after
.space:
    mov r8, 1
    jmp .after
.newline:
    inc r14
    mov r8, 1
.after:
    inc r12
    jmp .next
.done:
    test rbx, rbx
    jz .report
    cmp byte [buffer + rbx - 1], 10
    je .report
    inc r14
.report:
    mov rdi, r15
    call print_number
    mov rdi, 10
    call print_char
    mov rax, 60
    mov rdi, 0
    syscall

All lessons in Functions, Stack and Strings

Practice on your own: Assembly playground