fox32rom: Add string_to_int

main
Ry 2022-05-01 18:01:10 -07:00
parent 77ee645f7d
commit 6516128ba3
3 changed files with 72 additions and 0 deletions

View File

@ -64,6 +64,9 @@ compare_memory_words: jmp [0xF0046010]
compare_string: jmp [0xF0046014]
string_length: jmp [0xF0046018]
; integer jump table
string_to_int: jmp [0xF0047000]
; event types
const EVENT_TYPE_MOUSE_CLICK: 0x00000000
const EVENT_TYPE_MOUSE_RELEASE: 0x00000001

63
integer.asm 100644
View File

@ -0,0 +1,63 @@
; integer routines
; convert ASCII string into an integer
; thanks to lua for helping with this :3
; inputs:
; r0: pointer to null-terminated string
; r1: radix (i.e. 10 for decimal, 16 for hexadecimal)
; outputs:
; r0: integer
string_to_int:
push r2
push r3
mov r3, 0
string_to_int_loop:
movz.8 r2, [r0]
inc r0
cmp r2, 0
ifz jmp string_to_int_end
; if (digit >= '0' && digit <= '9') {
; digit -= '0';
; } else if (digit >= 'A' && digit <= 'Z') {
; digit -= 'A' - 10
; } else if (digit >= 'a' && digit <= 'z') {
; digit -= 'a' - 10
; } else {
; continue;
; }
cmp r2, '0'
iflt jmp string_to_int_loop_2
cmp r2, '9'
ifgt jmp string_to_int_loop_2
sub r2, '0'
jmp string_to_int_loop_end
string_to_int_loop_2:
cmp r2, 'A'
iflt jmp string_to_int_loop_3
cmp r2, 'Z'
ifgt jmp string_to_int_loop_3
sub r2, 0x37 ; 'A' - 10
jmp string_to_int_loop_end
string_to_int_loop_3:
cmp r2, 'a'
iflt jmp string_to_int_loop
cmp r2, 'z'
ifgt jmp string_to_int_loop
sub r2, 0x57 ; 'a' - 10
string_to_int_loop_end:
mul r3, r1
add r3, r2
jmp string_to_int_loop
string_to_int_end:
mov r0, r3
pop r3
pop r2
ret

View File

@ -124,6 +124,7 @@ get_rom_version:
#include "draw_rectangle.asm"
#include "draw_text.asm"
#include "event.asm"
#include "integer.asm"
#include "keyboard.asm"
#include "memory.asm"
#include "menu.asm"
@ -210,6 +211,11 @@ get_rom_version:
data.32 compare_memory_bytes
data.32 compare_memory_words
data.32 compare_string
data.32 string_length
; integer jump table
org.pad 0xF0047000
data.32 string_to_int
org.pad 0xF004F000
standard_font_width: