Implement atoi

This commit is contained in:
Bill Rossi 2023-12-21 11:45:43 -05:00
parent a3d01d5873
commit a9ef0dce69
2 changed files with 46 additions and 13 deletions

View File

@ -2,27 +2,32 @@ format ELF64 executable 3
include "lib/linux_syscall.inc" include "lib/linux_syscall.inc"
include "lib/print.inc" include "lib/print.inc"
include "lib/itoa.inc" include "lib/itoa.inc"
include "lib/file.inc"
segment readable executable segment readable executable
entry main entry main
main: main:
mov rax, 42069 mov rdi, input_filename
mov rbx, part_1 call load_file
call itoa
mov rdi, part_1_verbiage call part_1
call print_c_string
exit 0 exit 0
part_1:
mov rdi, number
call atoi
mov rbx, part_1_answer
call itoa
mov rdi, part_1_verbiage
call print_c_string
ret
segment readable writable segment readable writable
input_filename db '../data/2015/1/input.txt', 0, 'more text', 0 input_filename db '../data/2015/2/input.txt', 0
input_fd dq -1
input dq -1
part_1_verbiage db 'Part 1: ' part_1_verbiage db 'Part 1: '
part_1 db ' ', 0 part_1_answer db ' ', 0
part_2_verbiage db 'Part 2: ', 0 part_2_verbiage db 'Part 2: '
part_2 db ' ', 0 part_2_answer db ' ', 0
newline db 10 number db '42069', 0
file_stat s_stat

View File

@ -87,3 +87,31 @@ itoa:
.done: .done:
ret ret
;;; rdi should contain the address of a c-string
atoi:
mov r8, rdi
mov rax, 0
mov rcx, 10
.lp:
cmp byte [r8], 0
jz .done
mov r9, [r8]
and r9, 127
sub r9, '0'
add rax, r9
mov rcx, rax
add rax, rax
add rax, rax
add rax, rax
add rax, rcx
add rax, rcx
inc r8
jmp .lp
.done:
mov rcx, 10
mov rdx, 0
div rcx
ret