1
0
mirror of https://gitlab.os-k.eu/os-k-team/kvisc.git synced 2023-08-25 14:05:46 +02:00
kvisc/ka/crt/str.k

223 lines
2.7 KiB
Plaintext
Raw Normal View History

2019-08-14 20:23:05 +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 strnlen(char *, int)
;
strnlen:
mov rcx, ax1
scasb ax0, zero
sub rax, ax1, rcx
ret
;
; int strlen(char *)
;
strlen:
mov rcx, STRLEN_MAX
mov rdx, rcx
scasb ax0, zero
sub rax, rdx, rcx
ret
;
; void strcpy(char *, const char *)
;
strcpy:
.l:
mov rcx, b[ax1]
mov b[ax0], rcx
jrcxz .r
2019-08-21 16:57:32 +02:00
inc ax0
inc ax1
2019-08-14 20:23:05 +02:00
jmp .l
.r:
ret
;
; void strncpy(char *, const char *, int)
;
strncpy:
mov rcx, ax2
jrcxz .r
.l:
mov b[ax0], b[ax1]
2019-08-21 16:57:32 +02:00
inc ax0
inc ax1
2019-08-14 20:23:05 +02:00
loop .l
.r:
ret
;
; void strnzcpy(char *, const char *, int)
;
strnzcpy:
mov rcx, ax2
jrcxz .r
.l:
mov rax, b[ax1]
mov b[ax0], rax
jraxz .r
2019-08-21 16:57:32 +02:00
inc ax0
inc ax1
2019-08-14 20:23:05 +02:00
loop .l
.z:
nul b[ax0]
.r:
ret
;
; int strcmp(const char *str1, const char *str2)
;
; Returns:
; 0 if the contents of both strings are equal
; >0 if the first character that does not match has a greater value in str1 than in str2
; <0 if the first character that does not match has a lower value in str1 than in str2
;
strcmp:
nul rsi
.l:
movzx rax, b[ax0+rsi]
movzx rdx, b[ax1+rsi]
bne rax, rdx, .r
; both zero?
add rcx, rax, rdx
jrcxz .r
2019-08-21 16:57:32 +02:00
inc rsi
2019-08-14 20:23:05 +02:00
jmp .l
.r:
2019-08-21 16:57:32 +02:00
sub rax, rdx
2019-08-14 20:23:05 +02:00
ret
;
; int strncmp(const char *str1, const char *str2, int maxn)
;
strncmp:
mov rcx, ax2
jrcxz .r
.l:
movzx rax, b[ax0]
movzx rdx, b[ax1]
bne rax, rdx, .r
2019-08-21 16:57:32 +02:00
inc ax0
inc ax1
2019-08-14 20:23:05 +02:00
loop .l
.r:
2019-08-21 16:57:32 +02:00
sub rax, rdx
2019-08-14 20:23:05 +02:00
ret
;
; char *strchrnul(const char *str, int ch)
;
strchrnul:
mov rcx, STRLEN_MAX
scasb ax0, ax1
mov rax, ax0
ret
;
; char *strchr(const char *str, int ch)
;
strchr:
mov rcx, STRLEN_MAX
scasb ax0, ax1
bnz b[ax0], .r
nul rax
ret
.r:
mov rax, ax0
ret
;
; void strrev(char *buf, const char *str)
;
; buf and src must NOT overlap
;
strrev:
bzr b[ax1], .z
; save str's location
mov rsi, ax1
; go to str's end, just before
; the null terminator
mov rcx, STRLEN_MAX
scasb ax1, zero
2019-08-21 16:57:32 +02:00
dec ax1
2019-08-14 20:23:05 +02:00
.l:
; copy, going backward though str
; and forward through buf
mov b[ax0], b[ax1]
beq ax1, rsi, .r
2019-08-21 16:57:32 +02:00
inc ax0
dec ax1
2019-08-14 20:23:05 +02:00
jmp .l
.r:
nul b[ax0+1]
ret
.z:
nul b[ax0]
ret
;
; void strrev2(char *str)
;
; Inverses str
;
strrev2:
bzr b[ax0], .r
mov ax1, ax0
; go to str's end, just before
; the null terminator
mov rcx, STRLEN_MAX
scasb ax1, zero
2019-08-21 16:57:32 +02:00
dec ax1
2019-08-14 20:23:05 +02:00
; increase ax0 while decreasing ax1, performing exchanges
.l:
blteu ax1, ax0, .r
xchg b[ax0], b[ax1]
2019-08-21 16:57:32 +02:00
inc ax0
dec ax1
2019-08-14 20:23:05 +02:00
jmp .l
.r:
ret