Text to Number
Part of the Functions, Stack and Strings section of Coddy's Assembly journey. Lesson 24 of 33.
The text 472 is three bytes: 52, 55 and 50, the codes of '4', '7' and '2'. It is not the number 472 yet. Subtracting '0' (48) from a digit byte gives its value: '7' minus '0' is 7.
Build the number from left to right: multiply what you have by 10 and add the next digit. For 472: 0 times 10 plus 4 is 4, 4 times 10 plus 7 is 47, 47 times 10 plus 2 is 472.
The same steps in a loop over the text:
xor eax, eax ; n = 0
.next:
movzx ecx, byte [rdi]
test ecx, ecx
jz .done
sub ecx, '0' ; the digit's value
imul rax, rax, 10
add rax, rcx ; n = n * 10 + digit
inc rdi
jmp .next
.done:The locked main.asm of chapters 1 to 4 runs this loop on your input. It is how the text 3 10 became the numbers 3 and 10 in rdi and rsi; it also skips the spaces and handles a minus sign.
The loop trusts every byte to be a digit. A letter or a space would add a wrong value, so code that reads text from a person checks the range '0' to '9' first.
Challenge
Easysolve receives the address of a text of 1 to 18 digits in rdi, ending with a zero byte. Turn the digits into the number they spell and return that number plus 1 in rax.
Example: for the input 472 the program prints 473.
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 .exitThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Functions, Stack and Strings
4Stack Frames and Recursion
Local VariablesFrames with RBPRecursionTwo Recursive CallsRecap: Digit Sum5Strings and Bytes
Bytes and ASCIICounting CharactersChanging CaseText to NumberReversing TextRecap: Palindrome3The Calling Convention
Arguments in RegistersReturn ValuesCaller-Saved RegistersCallee-Saved RegistersRecap: Scoreboard6Project: Word Counter
The Read SyscallCount the CharactersPractice on your own: Assembly playground