kvisc/ka/crt/str/strrev.k

73 lines
1.0 KiB
Plaintext
Raw Normal View History

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.
;
; void strrev(char *buf, const char *str)
;
2019-06-05 22:59:32 +02:00
; buf and src must NOT overlap
;
2019-05-30 18:31:50 +02:00
strrev:
2019-08-14 09:52:39 +02:00
bzr b[ax1], .z
2019-05-30 18:31:50 +02:00
2019-05-30 18:36:06 +02:00
; save str's location
2019-07-22 13:18:13 +02:00
mov rsi, ax1
2019-05-30 18:36:06 +02:00
2019-05-30 19:07:04 +02:00
; go to str's end, just before
; the null terminator
2019-06-16 21:17:56 +02:00
mov rcx, STRLEN_MAX
2019-07-17 22:25:50 +02:00
scasb.rep.nz ax1, zero
2019-08-03 17:41:44 +02:00
dec ax1, 1
2019-05-30 18:31:50 +02:00
2019-08-14 09:52:39 +02:00
.l:
2019-05-30 19:07:04 +02:00
; copy, going backward though str
2019-05-30 19:33:22 +02:00
; and forward through buf
2019-08-03 17:41:44 +02:00
mov b[ax0], b[ax1]
2019-05-30 18:31:50 +02:00
2019-08-14 09:52:39 +02:00
beq ax1, rsi, .r
2019-05-30 18:31:50 +02:00
2019-08-03 17:41:44 +02:00
inc ax0, 1
dec ax1, 1
2019-08-14 09:52:39 +02:00
jmp .l
2019-05-30 18:31:50 +02:00
2019-08-14 09:52:39 +02:00
.r:
mov b[ax0+1], zero
ret
.z:
mov b[ax0], zero
ret
2019-06-05 22:59:32 +02:00
;
; void strrev2(char *str)
;
; Inverses str
;
strrev2:
2019-08-14 09:52:39 +02:00
bzr b[ax0], .r
2019-06-05 22:59:32 +02:00
mov ax1, ax0
; go to str's end, just before
; the null terminator
2019-06-16 21:17:56 +02:00
mov rcx, STRLEN_MAX
2019-07-17 22:25:50 +02:00
scasb.rep.nz ax1, zero
2019-08-03 17:41:44 +02:00
dec ax1, 1
2019-06-05 22:59:32 +02:00
; increase ax0 while decreasing ax1, performing exchanges
2019-08-14 09:52:39 +02:00
.l:
blteu ax1, ax0, .r
2019-06-05 22:59:32 +02:00
2019-08-03 17:41:44 +02:00
xchg b[ax0], b[ax1]
2019-06-05 22:59:32 +02:00
2019-08-03 17:41:44 +02:00
inc ax0, 1
dec ax1, 1
2019-08-14 09:52:39 +02:00
jmp .l
2019-06-05 22:59:32 +02:00
2019-08-14 09:52:39 +02:00
.r:
2019-06-05 22:59:32 +02:00
ret