Implement streq routine
This commit is contained in:
parent
51ee97064b
commit
5f3cbbb8a4
1 changed files with 27 additions and 0 deletions
|
@ -55,3 +55,30 @@ read:
|
||||||
li a0, 0 # 0 = standard input (stdin)
|
li a0, 0 # 0 = standard input (stdin)
|
||||||
ecall # invoke syscall
|
ecall # invoke syscall
|
||||||
ret
|
ret
|
||||||
|
|
||||||
|
|
||||||
|
# Determine if two strings are equal
|
||||||
|
# arguments:
|
||||||
|
# a0: address of first string
|
||||||
|
# a1: length of first string
|
||||||
|
# a2: address of second string
|
||||||
|
# a3: length of second string
|
||||||
|
# return:
|
||||||
|
# a0: 1 if equal, 0 otherwise
|
||||||
|
streq:
|
||||||
|
bne a1, a2, 2f # If the lengths are not the same then the strings are different
|
||||||
|
1:
|
||||||
|
beqz a1, 3f # If the remaining length is zero, string are the same
|
||||||
|
lbu a4, 0(a0) # load byte from string 1
|
||||||
|
lbu a5, 0(a2) # load byte from string 2
|
||||||
|
bne a4, a5, 2f # return false if bytes are not equal
|
||||||
|
addi a1, a1, -1 # decrement a1
|
||||||
|
addi a0, a0, 1 # go to next byte of string 1
|
||||||
|
addi a2, a2, 1 # go to next byte of string 2
|
||||||
|
j 1b # loop
|
||||||
|
2:
|
||||||
|
li a0, 0 # 0 = false
|
||||||
|
ret
|
||||||
|
3:
|
||||||
|
li a0, 1 # 1 = true
|
||||||
|
ret
|
||||||
|
|
Loading…
Reference in a new issue