mirror of
https://gitlab.os-k.eu/os-k-team/kvisc.git
synced 2023-08-25 14:05:46 +02:00
113 lines
1.6 KiB
Plaintext
113 lines
1.6 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.
|
|
|
|
;
|
|
; char *_itoa(char *buf, int num, int base, int unsi)
|
|
;
|
|
; Acts as itoa if unsi == 0, as utoa otherwise
|
|
;
|
|
|
|
_itoa:
|
|
mov rax, ax0
|
|
xor rdx, rdx
|
|
|
|
; make sure base is in [2, 32]
|
|
|
|
cmp ax2, 2
|
|
cjmpb .bad
|
|
|
|
cmp ax2, 36
|
|
cjmpa .bad
|
|
|
|
; deal with zero
|
|
test ax1, ax1
|
|
cjmpz .zero
|
|
|
|
; deal with base 10 signedness
|
|
|
|
test ax3, ax3 ; unsigned mode
|
|
cjmpnz .conv
|
|
|
|
cmp ax2, 10 ; base 10
|
|
cjmpnz .conv
|
|
|
|
cmp ax1, -9223372036854775808 ; LONG_MIN
|
|
cjmpz .min
|
|
|
|
sgn rdx, ax1 ; extract ax1 sign
|
|
|
|
cmp rdx, -1 ; negative?
|
|
cnotz ax1 ; -x = ~x+1
|
|
cincz ax1 ; need "neg" inst...
|
|
|
|
; main loop
|
|
.conv:
|
|
test ax1, ax1
|
|
cjmpz .fini
|
|
|
|
mov rsx, ax1
|
|
mod rsx, ax2 ; ax1 % base
|
|
|
|
cmp rsx, 9 ; rsx > 9 ?
|
|
cjmpa .nondec
|
|
|
|
add rsx, 48 ; '0'
|
|
jmp .next
|
|
|
|
.nondec:
|
|
add rsx, 87 ; 'a' - 10
|
|
|
|
.next:
|
|
mov b[ax0], rsx
|
|
inc ax0
|
|
|
|
div ax1, ax2
|
|
jmp .conv
|
|
|
|
; add minus flag, null-terminate and reverse
|
|
.fini:
|
|
cmp rdx, -1
|
|
cmovz b[ax0], 45 ; '-'
|
|
cincz ax0
|
|
|
|
mov b[ax0], 0
|
|
|
|
mov ax0, rax
|
|
call strrev2
|
|
|
|
ret
|
|
|
|
;
|
|
; exceptional cases
|
|
;
|
|
|
|
.bad:
|
|
mov q[errno], EINVAL
|
|
mov b[rax], 0
|
|
ret
|
|
|
|
.zero:
|
|
mov b[ax0], 48 ; '0'
|
|
mov b[ax0+1], 0
|
|
ret
|
|
|
|
.min:
|
|
mov ax1, .min10
|
|
call strcpy
|
|
ret
|
|
|
|
.min10 = "-9223372036854775808"
|
|
|
|
;
|
|
; wrappers
|
|
;
|
|
|
|
itoa:
|
|
mov ax3, 0
|
|
jmp _itoa
|
|
|
|
utoa:
|
|
mov ax3, 1
|
|
jmp _itoa
|
|
|