Menu
CoddyTech

Changing Case

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

The lower-case letters 'a' to 'z' are 97 to 122, and the capitals 'A' to 'Z' are 65 to 90. Every capital is 32 less than its lower-case letter: 'a' 97 becomes 'A' 65.

To make a byte a capital, check that it is in 'a' to 'z', then subtract 32. Any other byte (a capital, a digit, a space) stays as it is.

    cmp cl, 'a'
    jb .keep
    cmp cl, 'z'
    ja .keep
    sub cl, 32        ; 'h' becomes 'H'
.keep:

mov [rsi], cl writes one byte, the lowest byte of rcx, to the address in rsi. The size comes from the register: cl is one byte, so one byte is written.

From this lesson on, some tasks build a new text. solve then also receives the address of an empty 256-byte buffer in rsi. Write your text there and return its length in rax; the locked main.asm prints that many bytes of the buffer.

One index can walk the text and the buffer together. This loop copies the text unchanged, and rax ends as its length:

    xor eax, eax
.next:
    movzx ecx, byte [rdi + rax]
    test ecx, ecx
    jz .done
    mov [rsi + rax], cl
    inc rax
    jmp .next
.done:
challenge icon

Challenge

Easy

solve receives the address of a text in rdi, ending with a zero byte, and the address of an empty buffer in rsi. Copy the text into the buffer with every lower-case letter a to z made a capital and every other byte unchanged. Return the length of the text in rax.

Example: for the input hello the program prints HELLO.

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

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 257
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
    cmp rax, 256
    jbe .length_ok
    mov eax, 256
.length_ok:
    mov byte [text_buffer + rax], 10
    lea rdx, [rax + 1]
    lea rsi, [text_buffer]
    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

All lessons in Functions, Stack and Strings

Practice on your own: Assembly playground