2019-05-30 18:31:50 +02:00
|
|
|
; The OS/K Team licenses this file to you under the MIT license.
|
|
|
|
; See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
;
|
2019-06-20 12:31:36 +02:00
|
|
|
; void itoa(char *buf, int num, int base)
|
2019-06-17 20:59:30 +02:00
|
|
|
;
|
|
|
|
itoa:
|
2019-06-23 20:56:04 +02:00
|
|
|
mov ax3, 1
|
|
|
|
jmp ltostr
|
|
|
|
|
|
|
|
;
|
|
|
|
; void utoa(char *buf, int num, int base)
|
|
|
|
;
|
2019-06-26 21:25:59 +02:00
|
|
|
utoa:
|
2019-08-14 20:23:05 +02:00
|
|
|
nul ax3
|
2019-06-23 20:56:04 +02:00
|
|
|
jmp ltostr
|
|
|
|
|
|
|
|
;
|
|
|
|
; void ltostr(char *buf, int num, int base, bool signed)
|
|
|
|
;
|
|
|
|
ltostr:
|
2019-06-05 22:11:45 +02:00
|
|
|
mov rax, ax0
|
2019-08-14 20:23:05 +02:00
|
|
|
nul rcx
|
2019-06-05 22:11:45 +02:00
|
|
|
|
|
|
|
; make sure base is in [2, 32]
|
2019-08-14 09:52:39 +02:00
|
|
|
bltu ax2, 2, .bad
|
|
|
|
bltu 36, ax2, .bad
|
2019-06-05 22:11:45 +02:00
|
|
|
|
|
|
|
; deal with zero
|
2019-08-14 09:52:39 +02:00
|
|
|
bzr ax1, .is_zero
|
2019-06-05 22:11:45 +02:00
|
|
|
|
|
|
|
; deal with base 10 signedness
|
|
|
|
|
2019-08-14 09:52:39 +02:00
|
|
|
bzr ax3, .conv
|
|
|
|
bne ax2, 10, .conv ; base 10
|
2019-06-05 22:11:45 +02:00
|
|
|
|
2019-07-22 13:18:13 +02:00
|
|
|
shr rcx, ax1, 63 ; extract ax1 sign
|
2019-08-14 09:52:39 +02:00
|
|
|
jrcxz .conv
|
|
|
|
|
2019-08-14 20:23:05 +02:00
|
|
|
neg ax1 ; NEG if negative
|
2019-06-05 22:11:45 +02:00
|
|
|
|
|
|
|
; main loop
|
|
|
|
.conv:
|
2019-08-14 09:52:39 +02:00
|
|
|
bzr ax1, .fini
|
2019-06-05 22:11:45 +02:00
|
|
|
|
2019-08-14 09:52:39 +02:00
|
|
|
rem rdx, ax1, ax2 ; ax1 % base
|
2019-06-05 22:11:45 +02:00
|
|
|
|
2019-08-14 09:52:39 +02:00
|
|
|
blt 9, rdx, .nondec ; rdx > 9 ?
|
2019-06-05 22:11:45 +02:00
|
|
|
|
2019-08-21 19:10:26 +02:00
|
|
|
add rdx, '0'
|
2019-06-05 22:59:32 +02:00
|
|
|
jmp .next
|
|
|
|
|
|
|
|
.nondec:
|
2019-08-21 19:10:26 +02:00
|
|
|
add rdx, 55 ; 'A' - 10
|
2019-06-05 22:11:45 +02:00
|
|
|
|
|
|
|
.next:
|
2019-07-22 13:18:13 +02:00
|
|
|
mov b[ax0], rdx
|
2019-08-08 18:39:12 +02:00
|
|
|
|
2019-08-21 16:57:32 +02:00
|
|
|
inc ax0
|
2019-06-05 22:11:45 +02:00
|
|
|
|
2019-08-08 18:39:12 +02:00
|
|
|
div ax1, ax2
|
2019-06-05 22:11:45 +02:00
|
|
|
jmp .conv
|
|
|
|
|
2019-06-05 22:59:32 +02:00
|
|
|
; add minus flag, null-terminate and reverse
|
2019-06-05 22:11:45 +02:00
|
|
|
.fini:
|
2019-08-14 09:52:39 +02:00
|
|
|
jrcxz .cxz
|
|
|
|
mov b[ax0], '-'
|
2019-08-21 16:57:32 +02:00
|
|
|
inc ax0
|
2019-06-05 22:59:32 +02:00
|
|
|
|
2019-08-14 09:52:39 +02:00
|
|
|
.cxz:
|
2019-08-14 20:23:05 +02:00
|
|
|
nul b[ax0]
|
2019-06-05 22:59:32 +02:00
|
|
|
|
2019-07-18 22:49:31 +02:00
|
|
|
call strrev2, rax
|
2019-06-05 22:11:45 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
;
|
|
|
|
; exceptional cases
|
|
|
|
;
|
|
|
|
.bad:
|
2019-06-15 20:21:38 +02:00
|
|
|
mov b[ax0], 0
|
2019-06-05 22:11:45 +02:00
|
|
|
ret
|
|
|
|
|
2019-07-17 22:25:50 +02:00
|
|
|
.is_zero:
|
2019-06-05 22:11:45 +02:00
|
|
|
mov b[ax0], 48 ; '0'
|
|
|
|
mov b[ax0+1], 0
|
|
|
|
ret
|
|
|
|
|