-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString_comparison.asm
More file actions
60 lines (50 loc) · 1.5 KB
/
String_comparison.asm
File metadata and controls
60 lines (50 loc) · 1.5 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
#Program for the string comparison
.data
msg1: .asciiz "Enter the string-"
msg2: .asciiz "Given String are equal"
msg3: .asciiz "Given string are not equal"
str1: .space 100 #telling assembler to reserve the space about 100 bytes and address of the 1st byte is store in the str1
str2: .space 100
.text
main:
li $v0, 4
la $a0, msg1
syscall
li $v0, 8
la $a0, str1
la $a1, 100
syscall
move $t0, $a0
li $v0, 4
la $a0, msg1
syscall
li $v0, 8
la $a0, str2
la $a1, 100
syscall
move $t1, $a0
move $a0, $t0
move $a1, $t1
jal comparison
exit:
li $v0, 10
syscall
comparison:
lb $s0, 0($a0) #this way, i am loading the value present in the $a0 to the $s0
lb $s1, 0($a1)
bne $s0, $s1, notequal #if they are equal, assemble will jump to the notequal label
beq $s0, $zero, equal #the reason I didn't choose $s0, $s1, bcoz it will quit when character is equal. But might think why 0? it to tell whether my string comes to an end or not
#also, if the string will be same then notequal label will not excuted thus at last when string comes to an end, equal label will be executed.
addi $a0, $a0, 1 #moving to next byte means, next character
addi $a1, $a1, 1
j comparison
equal:
li $v0, 4
la $a0, msg2
syscall
j exit #finally, jump to the exit label
notequal:
li $v0, 4
la $a0, msg3
syscall
j exit