Menu
CoddyTech

Bytes and ASCII

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

A character is stored as one byte: its ASCII code. 'A' is 65, 'a' is 97, the digit '0' is 48 and a space is 32. A text in memory is a row of these bytes, one per character.

In this chapter the locked main.asm reads one line of text and passes its address to solve in rdi. The text ends with a zero byte, the number 0, which no character uses. Code that walks the text stops when it reads that zero. C stores its strings the same way.

movzx eax, byte [rdi] reads one byte and fills the rest of rax with zeros. A plain mov al, [rdi] changes only the lowest byte and keeps the old upper bytes of rax, so comparing all of rax afterwards can go wrong.

movzx eax, byte [rdi]       ; rax = the first byte, for 'Hi' 72
movzx ecx, byte [rdi + 1]   ; rcx = the second byte, 105

Characters are one byte each, so the next character is at the next address: inc rdi, or an index register in [rdi + rcx]. This loop stops at the zero byte:

    xor ecx, ecx
.next:
    movzx eax, byte [rdi + rcx]
    test eax, eax
    jz .end
    inc rcx
    jmp .next
.end:

In NASM a character in quotes is its code: cmp al, 'a' is the same instruction as cmp al, 97. Quotes make the code easier to read than the number.

challenge icon

Challenge

Easy

solve receives the address of a text in rdi; the text ends with a zero byte. Return in rax the number of bytes before the zero byte. An empty text returns 0.

Example: for the input hello the program prints 5.

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

All lessons in Functions, Stack and Strings

Practice on your own: Assembly playground