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/strrev.k
julianb0 ce8411be8e
vm
2019-07-22 13:18:13 +02:00

74 lines
1.1 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.
;
; void strrev(char *buf, const char *str)
;
; buf and src must NOT overlap
;
strrev:
cmp b[ax1], zero
mov.z b[ax0], zero
ret.z
; save str's location
mov rsi, ax1
; go to str's end, just before
; the null terminator
mov rcx, STRLEN_MAX
scasb.rep.nz ax1, zero
sub ax1, ax1, 1
.2:
; copy, going backward though str
; and forward through buf
mov rax, b[ax1]
mov b[ax0], rax
cmp ax1, rsi
mov.z b[ax0+1], zero
ret.z
add ax0, ax0, 1
sub ax1, ax1, 1
jmp .2
;
; void strrev2(char *str)
;
; Inverses str
;
strrev2:
cmp b[ax0], zero
ret.z
mov ax1, ax0
; go to str's end, just before
; the null terminator
mov rcx, STRLEN_MAX
scasb.rep.nz ax1, zero
sub ax1, ax1, 1
; increase ax0 while decreasing ax1, performing exchanges
.2:
b.ae ax0, ax1, .3
mov rax, b[ax1]
xchg rax, b[ax0]
mov b[ax1], rax
add ax0, ax0, 1
sub ax1, ax1, 1
jmp .2
.3:
ret