forked from fox32-arch/fox32rom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.asm
82 lines (74 loc) · 1.59 KB
/
string.asm
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
73
74
75
76
77
78
79
80
81
; string copy/compare routines
; copy string from source pointer to destination pointer
; if the source and destination overlap, the behavior is undefined
; inputs:
; r0: pointer to source
; r1: pointer to destination
; outputs:
; none
copy_string:
push r0
push r1
push r2
copy_string_loop:
mov.8 r2, [r0]
mov.8 [r1], r2
inc r0
inc r1
cmp.8 r2, 0
ifnz jmp copy_string_loop
pop r2
pop r1
pop r0
ret
; compare string from source pointer with destination pointer
; inputs:
; r0: pointer to source
; r1: pointer to destination
; outputs:
; Z flag
compare_string:
push r0
push r1
compare_string_loop:
; check if the strings match
cmp.8 [r0], [r1]
ifnz jmp compare_string_not_equal
; if this is the end of string 1, then this must also be the end of string 2
; the cmp above already ensured that both strings have a null-terminator here
cmp.8 [r0], 0
ifz jmp compare_string_equal
inc r0
inc r1
jmp compare_string_loop
compare_string_not_equal:
; Z flag is already cleared at this point
pop r1
pop r0
ret
compare_string_equal:
; set Z flag
mov r0, 0
cmp r0, 0
pop r1
pop r0
ret
; get the length of a string
; inputs:
; r0: pointer to null-terminated string
; outputs:
; r0: length of the string, not including the null-terminator
string_length:
push r1
mov r1, 0
string_length_loop:
; check if this is the end of the string
cmp.8 [r0], 0
ifz jmp string_length_end
inc r0
inc r1
jmp string_length_loop
string_length_end:
mov r0, r1
pop r1
ret