kvisc/ka/crt/fmt/strtol.k

168 lines
2.5 KiB
Plaintext
Raw Normal View History

2019-06-23 20:56:04 +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.
;
; int strtol(const char *str, int base)
;
; rax = integer extracted from str
; rdx = pointer to first invalid byte
;
strtol:
mov ax2, 1
jmp strtoq
;
; int strtoul(const char *str, int base)
;
; rax = integer extracted from str
; rdx = pointer to first invalid byte
;
strtoul:
2019-08-14 20:23:05 +02:00
nul ax2
2019-06-23 20:56:04 +02:00
jmp strtoq
;
; int strtoq(const char *str, int base, bool signed)
;
; guesses base when 'base'=0
;
strtoq:
2019-08-14 20:23:05 +02:00
nul rax, rsi
2019-06-23 20:56:04 +02:00
mov rdx, ax0
; make sure base is in [2, 32]
2019-08-14 09:52:39 +02:00
beq ax1, 1, .bad
bltu 36, ax1, .bad
2019-06-23 20:56:04 +02:00
; empty string?
2019-08-14 09:52:39 +02:00
bzr b[rdx], .done
2019-06-23 20:56:04 +02:00
.skip_spc:
2019-08-14 09:52:39 +02:00
bne b[rdx], ' ', .no_spc
2019-08-21 16:57:32 +02:00
inc rdx
2019-08-14 09:52:39 +02:00
jmp .skip_spc
2019-06-23 20:56:04 +02:00
2019-08-14 09:52:39 +02:00
.no_spc:
2019-06-23 20:56:04 +02:00
; skip +
2019-08-14 09:52:39 +02:00
bne b[rdx], '+', .no_plus
2019-08-21 16:57:32 +02:00
inc rdx
2019-06-23 20:56:04 +02:00
2019-08-14 09:52:39 +02:00
.no_plus:
; unsigned?
bzr ax2, .unsigned
2019-06-23 20:56:04 +02:00
; parse '-'
2019-08-14 09:52:39 +02:00
bne b[rdx], '-', .unsigned
2019-08-21 16:57:32 +02:00
inc rdx
2019-08-14 09:52:39 +02:00
mov rsi, 1
2019-06-23 20:56:04 +02:00
.unsigned:
; base 0
2019-08-14 09:52:39 +02:00
bzr ax1, .base_0
2019-06-23 20:56:04 +02:00
; base prefix?
2019-08-14 09:52:39 +02:00
bne b[rdx], '0', .main_loop
2019-06-23 20:56:04 +02:00
2019-08-21 16:57:32 +02:00
inc rdx
2019-07-02 20:13:05 +02:00
movzx rcx, b[rdx]
2019-06-23 20:56:04 +02:00
2019-07-02 20:13:05 +02:00
; "0x"/"0b" prefix
2019-08-14 09:52:39 +02:00
jrcxz .done ; "0"
beq rcx, 'x', .parsed_0x
beq rcx, 'b', .parsed_0b
2019-06-23 20:56:04 +02:00
; may be octal, but we don't care
2019-07-02 20:13:05 +02:00
; we accept "0110101010" (despite base=2) for instance
2019-06-23 20:56:04 +02:00
jmp .main_loop
.parsed_0x:
; are we in base 16?
; if not, leave rax = 0 and *rdx = 'x'
2019-08-14 09:52:39 +02:00
bne ax1, 16, .done
2019-06-23 20:56:04 +02:00
; else
2019-08-03 17:41:44 +02:00
inc rdx, 1
2019-06-23 20:56:04 +02:00
jmp .main_loop
.parsed_0b:
; are we in base 2?
; if not, leave rax = 0 and *rdx = 'b'
2019-08-14 09:52:39 +02:00
bne ax1, 2, .done
2019-06-23 20:56:04 +02:00
; else
2019-08-21 16:57:32 +02:00
inc rdx
2019-06-23 20:56:04 +02:00
jmp .main_loop
.base_0:
; guess base
2019-08-14 09:52:39 +02:00
beq b[rdx], '0', .b0_not10
; must be base 10
mov ax1, 10
jmp .main_loop
.b0_not10:
2019-08-21 16:57:32 +02:00
inc rdx
2019-06-23 20:56:04 +02:00
2019-08-14 09:52:39 +02:00
bne b[rdx], 'x', .b0_not16
2019-08-21 16:57:32 +02:00
inc rdx
2019-08-14 09:52:39 +02:00
mov ax1, 16
jmp .main_loop
2019-06-23 20:56:04 +02:00
2019-08-14 09:52:39 +02:00
.b0_not16:
bne b[rdx], 'b', .b0_not2
2019-08-21 16:57:32 +02:00
inc rdx
2019-08-14 09:52:39 +02:00
mov ax1, 2
jmp .main_loop
2019-06-23 20:56:04 +02:00
2019-08-14 09:52:39 +02:00
.b0_not2:
; take octal by default
2019-06-23 20:56:04 +02:00
mov ax1, 8
.main_loop:
2019-07-22 13:18:13 +02:00
movzx rcx, b[rdx]
2019-08-21 16:57:32 +02:00
inc rdx
2019-06-23 20:56:04 +02:00
2019-08-14 09:52:39 +02:00
; between 0 and 9?
bltu rcx, '0', .done
bltu '9', rcx, .not_digit10
; yes
2019-08-21 16:57:32 +02:00
sub rcx, '0'
2019-08-14 09:52:39 +02:00
jmp .next
2019-06-23 20:56:04 +02:00
2019-08-14 09:52:39 +02:00
.not_digit10:
bltu rcx, 'A', .done
bltu 'Z', rcx, .not_digitAZ
2019-06-23 20:56:04 +02:00
2019-08-21 16:57:32 +02:00
sub rcx, 55 ; 'A' - 10
2019-08-14 09:52:39 +02:00
jmp .next
.not_digitAZ:
bltu rcx, 'a', .done
bltu 'z', rcx, .done
2019-08-21 16:57:32 +02:00
sub rcx, 87 ; 'a' - 10
2019-08-14 09:52:39 +02:00
jmp .next
2019-06-23 20:56:04 +02:00
.next:
; too large for base?
2019-08-14 09:52:39 +02:00
blteu ax1, rcx, .done
2019-06-23 20:56:04 +02:00
2019-08-08 18:39:12 +02:00
mul rax, ax1
2019-08-03 17:41:44 +02:00
inc rax, rcx
2019-06-23 20:56:04 +02:00
jmp .main_loop
.done:
; negative?
2019-08-14 09:52:39 +02:00
bzr rsi, .r
2019-06-23 20:56:04 +02:00
; yes
2019-08-14 20:23:05 +02:00
neg rax
2019-08-14 09:52:39 +02:00
.r:
2019-06-23 20:56:04 +02:00
ret
.bad:
ret