Menu
CoddyTech

Count the Characters

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

challenge icon

Challenge

Easy

Keep the read from the previous step and remove the code that writes the input back. Count the characters: every byte read that is not a newline (byte 10). Print the count with print_number (the number goes in rdi), then a newline with print_char (rdi = 10), and exit with code 0.

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

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

Try it yourself

bits 64
section .data
    newline db 10
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
    mov rdx, rax
    mov rax, 1
    mov rdi, 1
    mov rsi, buffer
    syscall
    test rbx, rbx
    jz .exit
    cmp byte [buffer + rbx - 1], 10
    je .exit
    mov rax, 1
    mov rdi, 1
    mov rsi, newline
    mov rdx, 1
    syscall
.exit:
    mov rax, 60
    mov rdi, 0
    syscall

All lessons in Functions, Stack and Strings

Practice on your own: Assembly playground