-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek3_task2_find.asm
61 lines (52 loc) · 1.02 KB
/
week3_task2_find.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
.data
buf: .space 100
suc: .asciiz "\nSuccess! Location: "
fai: .asciiz "\nFail!\n"
endline: .asciiz "\r\n"
.text
.globl main
.macro linebreak
la $a0,endline
li $v0,4
syscall
.end_macro
main:
linebreak
la $a0, buf
li $a1, 100
li $v0, 8
syscall
inputchar:
li $v0,12
syscall
beq,$v0,'?',exit
add $t1,$v0,$0 # the value of input char
add $t2,$0,$0 # mark the position in string
la $s1, buf # $s1 should be initialized when inputting each time
find_loop:
lb $s0, 0($s1)
beq $t1,$s0 success
# confirm the existence of next char
addi $t2,$t2,1
slt $t3,$t2,$a1 # t3 =1 if t2 is smaller than $a1(the length of string)
beq $t3,0,fail
# shift to address of next char
addi $s1,$s1,1
j find_loop
success:
la $a0,suc
li $v0,4
syscall
# print the location
addi $a0, $t2, 1
li $v0,1
syscall
linebreak
j inputchar
fail:
la $a0,fai
li $v0,4
syscall
j inputchar
exit:
li $v0,10