kvisc/ka/crt/fmt/strtol.k

171 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:
sub ax2, ax2
jmp strtoq
;
; int strtoq(const char *str, int base, bool signed)
;
; guesses base when 'base'=0
;
strtoq:
xor rax, rax
mov rdx, ax0
; make sure base is in [2, 32]
cmp ax1, 1
j.z .bad
cmp ax1, 36
j.a .bad
; empty string?
cmp b[rdx], 0
jmp.z .done
.skip_spc:
cmp b[rdx], ' '
inc.z rdx
jmp.z .skip_spc
; skip +
cmp b[rdx], '+'
inc.z rdx
; signed?
test ax2, ax2
2019-07-01 00:45:08 +02:00
sub.z r10, r10
2019-06-23 20:56:04 +02:00
jmp.z .unsigned
; parse '-'
cmp b[rdx], '-'
inc.z rdx
2019-07-01 00:45:08 +02:00
mov.z r10, 1
sub.nz r10, r10
2019-06-23 20:56:04 +02:00
.unsigned:
; base 0
test ax1, ax1
jmp.z .base_0
; base prefix?
cmp b[rdx], '0'
jmp.nz .main_loop
inc rdx
; string is "0"
cmp b[rdx], 0
jmp.z .done
; "0x" prefix
cmp b[rdx], 'x'
jmp.z .parsed_0x
; "0b" prefix
cmp b[rdx], 'b'
jmp.z .parsed_0b
; may be octal, but we don't care
; we accept "0110101010" (binary) for instance
jmp .main_loop
.parsed_0x:
; are we in base 16?
; if not, leave rax = 0 and *rdx = 'x'
cmp ax1, 16
jmp.nz .done
; else
inc rdx
jmp .main_loop
.parsed_0b:
; are we in base 2?
; if not, leave rax = 0 and *rdx = 'b'
cmp ax1, 2
jmp.nz .done
; else
inc rdx
jmp .main_loop
.base_0:
; guess base
cmp b[rdx], '0'
mov.nz ax1, 10
jmp.nz .main_loop
inc rdx
cmp b[rdx], 'x'
inc.z rdx
mov.z ax1, 16
jmp.z .main_loop
cmp b[rdx], 'b'
inc.z rdx
mov.z ax1, 2
jmp.z .main_loop
mov ax1, 8
.main_loop:
2019-07-01 00:45:08 +02:00
mov rx8, b[rdx]
2019-06-23 20:56:04 +02:00
inc rdx
2019-07-01 00:45:08 +02:00
cmp rx8, '0'
2019-06-23 20:56:04 +02:00
jmp.b .done
2019-07-01 00:45:08 +02:00
cmp.ae rx8, '9'
sub.be rx8, '0'
2019-06-23 20:56:04 +02:00
jmp.be .next
2019-07-01 00:45:08 +02:00
cmp rx8, 'A'
cmp.ae rx8, 'Z'
sub.be rx8, 55 ; 'A' - 10
2019-06-23 20:56:04 +02:00
jmp.be .next
2019-07-01 00:45:08 +02:00
cmp rx8, 'a'
2019-06-23 20:56:04 +02:00
jmp.b .next
2019-07-01 00:45:08 +02:00
cmp.ae rx8, 'z'
sub.be rx8, 87 ; 'a' - 10
2019-06-23 20:56:04 +02:00
jmp.be .next
.next:
; too large for base?
2019-07-01 00:45:08 +02:00
cmp rx8, ax1
2019-06-23 20:56:04 +02:00
jmp.ae .done
mul rax, ax1
2019-07-01 00:45:08 +02:00
add rax, rx8
2019-06-23 20:56:04 +02:00
jmp .main_loop
.done:
; negative?
2019-07-01 00:45:08 +02:00
test r10, r10
2019-06-23 20:56:04 +02:00
ret.z
; yes
neg rax
ret
.bad:
mov q[errno], EINVAL
ret