-
-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathswab.src
More file actions
72 lines (65 loc) · 1.4 KB
/
swab.src
File metadata and controls
72 lines (65 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
.assume adl=1
.section .text
.global _swab
.type _swab, @function
.if 1
; void swab(const void *__restrict src, void *__restrict dst, ssize_t count)
_swab:
; swab has unspecified behaviour when count is odd:
; - this implementation treats an odd count as if it were count - 1
; - does nothing when count <= 1 or count is negative
ld iy, 0
add iy, sp
sra (iy + 11)
ret m ; do nothing when negative
ld bc, (iy + 9)
rr b
rr c
; BC = count / 2
sbc hl, hl
adc hl, bc
ret z ; count <= 1
ld de, (iy + 6) ; DE = dst
ld hl, (iy + 3) ; HL = src
.L.loop:
ld a, (hl) ; A = src.lo
inc hl
ldi ; dst.lo = src.hi
ld (de), a ; dst.hi = A
inc de
jp pe, .L.loop
ret
.else
; void swab(const void *__restrict src, void *__restrict dst, ssize_t count)
_swab:
; swab has unspecified behaviour when count is odd:
; - this implementation will copy dst[count - 1] to src[count - 1] when count is odd
; - does nothing when count <= 0 or count is negative
ld iy, 0
add iy, sp
sra (iy + 11)
ret m ; do nothing when negative
ld bc, (iy + 9)
rr b
rr c
; BC = count / 2
sbc hl, hl
adc hl, bc
; Carry is set when count is odd
ld de, (iy + 6) ; DE = dst
ld hl, (iy + 3) ; HL = src
jr z, .L.finish
.L.loop:
ld a, (hl) ; A = src.lo
inc hl
ldi ; dst.lo = src.hi
ld (de), a ; dst.hi = A
inc de
jp pe, .L.loop
.L.finish:
ret nc ; even count
; copy one more byte
ld a, (hl)
ld (de), a
ret
.endif