mirror of
https://gitlab.os-k.eu/os-k-team/kvisc.git
synced 2023-08-25 14:05:46 +02:00
84 lines
1.3 KiB
Plaintext
84 lines
1.3 KiB
Plaintext
; The OS/K Team licenses this file to you under the MIT license.
|
|
; See the LICENSE file in the project root for more information.
|
|
|
|
;
|
|
; void itoa(char *buf, int num, int base)
|
|
;
|
|
itoa:
|
|
mov ax3, 1
|
|
jmp ltostr
|
|
|
|
;
|
|
; void utoa(char *buf, int num, int base)
|
|
;
|
|
utoa:
|
|
mov ax3, zero
|
|
jmp ltostr
|
|
|
|
;
|
|
; void ltostr(char *buf, int num, int base, bool signed)
|
|
;
|
|
ltostr:
|
|
mov rax, ax0
|
|
|
|
; make sure base is in [2, 32]
|
|
b.b ax2, 2, .bad
|
|
b.a ax2, 36, .bad
|
|
|
|
; deal with zero
|
|
b.z ax1, zero, .is_zero
|
|
|
|
; deal with base 10 signedness
|
|
|
|
b.z ax3, zero, .conv
|
|
b.nz ax2, 10, .conv ; base 10
|
|
|
|
shr rcx, ax1, 63 ; extract ax1 sign
|
|
sub.cxnz ax1, zero, ax1 ; NEG if negative
|
|
|
|
; main loop
|
|
.conv:
|
|
b.z ax1, zero, .fini
|
|
|
|
rem rdx, ax1, ax2 ; ax1 % base
|
|
|
|
b.a rdx, 9, .nondec ; rdx > 9 ?
|
|
|
|
inc rdx, '0'
|
|
jmp .next
|
|
|
|
.nondec:
|
|
inc rdx, 55 ; 'A' - 10
|
|
|
|
.next:
|
|
mov b[ax0], rdx
|
|
inc ax0, 1
|
|
|
|
div ax1, ax1, ax2
|
|
jmp .conv
|
|
|
|
; add minus flag, null-terminate and reverse
|
|
.fini:
|
|
mov.cxnz b[ax0], '-'
|
|
add.cxnz ax0, ax0, 1
|
|
|
|
mov b[ax0], zero
|
|
|
|
call strrev2, rax
|
|
|
|
ret
|
|
|
|
;
|
|
; exceptional cases
|
|
;
|
|
.bad:
|
|
mov q[errno], EINVAL
|
|
mov b[ax0], 0
|
|
ret
|
|
|
|
.is_zero:
|
|
mov b[ax0], 48 ; '0'
|
|
mov b[ax0+1], 0
|
|
ret
|
|
|