aboutsummaryrefslogtreecommitdiff
path: root/lib/string.S
blob: 271e8f30d839ec1a106d8e8e06afdc68db1c6a4c (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Defined in include/fbos/string.h
 *
 *     size_t strlen(const char *str)
 *
 * Returns (a0): string length.
 * Parameter (a0): string to measure.
 * Clobbers: t0, t1.
 */
.globl strlen
.type strlen, @function
strlen:
	mv t1, a0
1:
	lbu t0, 0(t1)
	beqz t0, 2f
	addi t1, t1, 1
	j 1b
2:
	sub a0, t1, a0
	ret

/*
 * Defined in include/fbos/string.h
 *
 *     int strcmp(const char *s1, const char *s2)
 *
 * Returns (a0): comparison result as in stdlib.
 * Parameter (a0, a1): strings to compare.
 * Clobbers: t0, t1.
 */
.globl strcmp
.type strcmp, @function
strcmp:
1:
	lbu t0, 0(a0)
	lbu t1, 0(a1)
	addi a0, a0, 1
	addi a1, a1, 1
	bne t0, t1, 2f
	bnez t0, 1b
	li a0, 0
	ret
2:
	sub a0, t0, t1
	ret

/*
 * Defined in include/fbos/string.h
 *
 *     int memcmp(const void *ptr1, const void *ptr2, size_t n)
 *
 * Returns (a0): comparison result as in stdlib.
 * Parameters (a0, a1): strings to compare.
 * Clobbers: t0, t1.
 */
.global memcmp
.type memcmp, @function
memcmp:
1:
	lbu t0, 0(a0)
	lbu t1, 0(a1)
	bne t0, t1, 2f
	addi a0, a0, 1
	addi a1, a1, 1
	addi a2, a2, -1
	bnez a2, 1b
	li a0, 0
	ret
2:
	sub a0, t0, t1
	ret

/*
 * Defined in include/fbos/string.h
 *
 *     void memcpy(const void *dest, const void *src, size_t count)
 *
 * Parameter a0: destination string.
 * Parameter a1: source string.
 * Parameter a2: number of bytes to copy.
 * Clobbers: t0.
 */
.global memcpy
.type memcpy, @function
memcpy:
1:
	beqz a2, 3f
2:
	lbu t0, 0(a1)
	sb t0, 0(a0)
	addi a0, a0, 1
	addi a1, a1, 1
	addi a2, a2, -1
	bnez a2, 2b
3:
	ret