This commit is contained in:
PleegWat
2026-09-10 21:03:45 +02:00
parent 431e00ca7e
commit ae5f90456c
2 changed files with 55 additions and 3 deletions
+46
View File
@@ -15,6 +15,8 @@ cmp r4, 2
je bin
cmp r4, 8
je oct
cmp r4, 16
je hex
mov flags, errno.INTTOSTR_BAD_BASE
jmp r13
@@ -110,3 +112,47 @@ lsr r5, r1, r4 ; Select bit and advance counter
and r5, r5, 7
sub r4, r4, 3
jmp oct_print
; Convert integer to hexadecimal string
; Arguments:
; r1 - Integer
; r2 - Start of buffer
; r3 - Buffer length
; Result:
; none
; Clobbers:
; flags
; r4 - input counter
; r5 - character
pub hex:
mov r4, 28
hex_find_one:
lsr r5, r1, r4 ; Select bit and advance counter
and r5, r5, 0xF
sub r4, r4, 4
cmp r4, 0 ; Start printing if last bit ...
jl hex_print
cmp r5, zr ; .. or if nonzero
je hex_find_one
lsr flags, r5, 2 ; Check buffer size
add flags, flags, 3
cmp flags, r3
ja badbuffer
hex_print:
add r5, r5, 0x30 ; '0' ; Add digit to buffer
cmp r5, 0x39 ; '9'
jle hex_no_letter
add r5, r5, 0x07 ; 'A' - '0' - 10
hex_no_letter:
store_8 [r2], r5
add r2, r2, 1
cmp r4, 0 ; Done if last bit
jl done
lsr r5, r1, r4 ; Select bit and advance counter
and r5, r5, 0xF
sub r4, r4, 4
jmp hex_print