adventofcode2023/18/utils64.s

118 lines
1.5 KiB
ArmAsm
Raw Normal View History

2023-12-18 14:04:47 -06:00
[bits 64]
[section .text]
; string input in RSI
; value in RAX
; CF set if none, clear if some
; RSI set past checked area
dec_parse:
sub rsp, 24
mov [rsp+16], rbx
mov [rsp+8], rdx
mov [rsp+0], rdi
xor rax, rax
xor rdi, rdi
mov rbx, 10 ; base
lodsb
sub al, '0'
js .no_input
cmp al, 9
jle .got_char
.no_input:
stc ; set CF
jmp .done
.loop:
xor rax,rax
lodsb
sub al, '0'
js .dec_done
cmp al, 9
jg .dec_done
.got_char:
xchg rdi,rax
mul rbx
add rdi,rax
jmp .loop
.dec_done:
clc ; clear CF
.done:
mov rax, rdi
mov rdi, [rsp+0]
mov rdx, [rsp+8]
mov rbx, [rsp+16]
add rsp, 24
ret
; string input in RSI
; value in RAX
; CF set if none, cear if some
; RSI set past checked area
hex_parse:
sub rsp, 24
mov [rsp+16], rbx
mov [rsp+8], rdx
mov [rsp+0], rdi
xor rax, rax
xor rdi, rdi
lodsb
cmp al, '0'
jb .no_input
cmp al, '9'
jbe .got_dec_char
cmp al, 'A'
jb .no_input
cmp al, 'F'
jbe .got_ualpha_char
cmp al, 'a'
jb .no_input
cmp al, 'f'
jbe .got_ualpha_char
.no_input:
stc ; set CF
jmp .done
.loop:
xor rax,rax
lodsb
cmp al, '0'
jb .hex_done
cmp al, '9'
jbe .got_dec_char
cmp al, 'A'
jb .hex_done
cmp al, 'F'
jbe .got_ualpha_char
cmp al, 'a'
jb .hex_done
cmp al, 'f'
jbe .got_ualpha_char
jmp .hex_done
.got_dec_char:
sub al, '0'
xchg rdi,rax
shl rax, 4
add rdi,rax
jmp .loop
.got_alpha_char:
sub al, 'A'-10
xchg rdi,rax
shl rax, 4
add rdi,rax
jmp .loop
.got_ualpha_char:
sub al, 'a'-10
xchg rdi,rax
shl rax, 4
add rdi,rax
jmp .loop
.hex_done:
clc ; clear CF
.done:
mov rax, rdi
mov rdi, [rsp+0]
mov rdx, [rsp+8]
mov rbx, [rsp+16]
add rsp, 24
ret
; --- MACROS ---
%define len(x) x %+ .over - x