Menu
CoddyTech

Counting Characters

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

To find a character, compare each byte with it: cmp cl, 'e', then je or jne. Keep the count in a register and add 1 on every match. This loop counts the letter e:

    xor eax, eax          ; the count
.next:
    movzx ecx, byte [rdi]
    test ecx, ecx
    jz .done
    cmp cl, 'e'
    jne .skip
    inc rax
.skip:
    inc rdi
    jmp .next
.done:

To count several characters, compare with each one and send every match to the same counting line:

    cmp cl, 'a'
    je .count
    cmp cl, 'b'
    je .count
    jmp .skip
.count:
    inc rax
.skip:

Some characters have codes in a row: the digits '0' to '9' are 48 to 57, and 'a' to 'z' are 97 to 122. Two comparisons test a whole range:

    cmp cl, '0'
    jb .skip          ; below '0'
    cmp cl, '9'
    ja .skip          ; above '9'
    inc rax           ; a digit

A byte read with movzx is 0 to 255, never negative, so ranges use the unsigned jumps jb and ja from section 2.

Capital and lower-case letters are different bytes: 'A' is 65 and 'a' is 97. A comparison with 'a' does not match 'A'.

challenge icon

Challenge

Easy

solve receives the address of a text in rdi, ending with a zero byte. Return in rax how many of its bytes are the lower-case vowels a, e, i, o or u. Capital letters do not count.

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

Write your code in solution.asm. The locked main.asm reads one line of text, calls solve and prints the number in rax.

Try it yourself

bits 64
section .data
    changed_rbx db 'solve changed rbx', 10
    changed_rbp db 'solve changed rbp', 10
    changed_r12 db 'solve changed r12', 10
    changed_r13 db 'solve changed r13', 10
    changed_r14 db 'solve changed r14', 10
    changed_r15 db 'solve changed r15', 10
section .bss
    input_buffer resb 257
    text_buffer resb 256
    output_buffer resb 32
section .text
    global _start
    extern solve
_start:
    xor eax, eax
    xor edi, edi
    mov rsi, input_buffer
    mov edx, 255
    syscall
    test rax, rax
    jg .trim
    xor eax, eax
.trim:
    test rax, rax
    jz .ended
    movzx edx, byte [input_buffer + rax - 1]
    cmp dl, 10
    je .drop
    cmp dl, 13
    jne .ended
.drop:
    dec rax
    jmp .trim
.ended:
    mov byte [input_buffer + rax], 0
    mov rbx, 0x1111111111111111
    mov rbp, 0x2222222222222222
    mov r12, 0x3333333333333333
    mov r13, 0x4444444444444444
    mov r14, 0x5555555555555555
    mov r15, 0x6666666666666666
    lea rdi, [input_buffer]
    lea rsi, [text_buffer]
    xor eax, eax
    call solve
    mov rdx, 0x1111111111111111
    lea rsi, [changed_rbx]
    cmp rbx, rdx
    jne .changed
    mov rdx, 0x2222222222222222
    lea rsi, [changed_rbp]
    cmp rbp, rdx
    jne .changed
    mov rdx, 0x3333333333333333
    lea rsi, [changed_r12]
    cmp r12, rdx
    jne .changed
    mov rdx, 0x4444444444444444
    lea rsi, [changed_r13]
    cmp r13, rdx
    jne .changed
    mov rdx, 0x5555555555555555
    lea rsi, [changed_r14]
    cmp r14, rdx
    jne .changed
    mov rdx, 0x6666666666666666
    lea rsi, [changed_r15]
    cmp r15, rdx
    jne .changed
    lea rsi, [output_buffer + 31]
    mov byte [rsi], 10
    mov ecx, 1
    xor r8d, r8d
    test rax, rax
    jns .positive
    mov r8d, 1
    neg rax
.positive:
    mov r9d, 10
.convert:
    xor edx, edx
    div r9
    add dl, '0'
    dec rsi
    mov [rsi], dl
    inc ecx
    test rax, rax
    jnz .convert
    test r8d, r8d
    jz .write
    dec rsi
    mov byte [rsi], '-'
    inc ecx
.write:
    mov edx, ecx
    mov eax, 1
    mov edi, 1
    syscall
.exit:
    mov eax, 60
    xor edi, edi
    syscall
.changed:
    mov eax, 1
    mov edi, 1
    mov edx, 18
    syscall
    jmp .exit
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Functions, Stack and Strings

Practice on your own: Assembly playground