Skip to content

Commit b3f1df8

Browse files
Loops in MIPS
1 parent 54d000a commit b3f1df8

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

palindrome.asm

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# check if user provided string is palindrome
2+
3+
.data
4+
5+
welcomeMsg: .asciiz "Enter a string: "
6+
userString: .space 256
7+
newline: .asciiz "\n"
8+
yes: .asciiz "IS PALINDROME"
9+
no: .asciiz "NOT PALINDROME"
10+
11+
.text
12+
13+
main:
14+
li $v0, 4
15+
la $a0, welcomeMsg
16+
syscall
17+
la $a0, userString
18+
li $a1, 256
19+
li $v0, 8
20+
syscall
21+
22+
li $v0, 4
23+
la $a0, userString
24+
syscall
25+
lb $t5, newline
26+
27+
# store address of last element of userString in $a1
28+
add $t1, $a0, $zero
29+
lengthZero:
30+
lb $t3, ($t1)
31+
addi $t1, $t1, 1
32+
beq $t3, $t5, done # this step is required as MIPS reads newline after string
33+
bne $t3, $zero, lengthZero
34+
done:
35+
addi $t1, $t1, -2
36+
# check if palindrome
37+
add $t0, $a0, $zero
38+
39+
check_letter:
40+
lb $t2, ($t0)
41+
lb $t3, ($t1)
42+
bne $t2, $t3, notPalindrome
43+
addi $t0, $t0, 1
44+
addi $t1, $t1, -1
45+
blt $t0, $t1, check_letter
46+
47+
li $v0, 4
48+
la $a0, yes
49+
syscall
50+
j endProgram
51+
52+
notPalindrome:
53+
li $v0, 4
54+
la $a0, no
55+
syscall
56+
endProgram:
57+
li $v0, 10
58+
syscall

print_array.asm

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.data
2+
3+
list: .word 17, 5, 92, 87, 41, 10, 23, 55, 72, 36
4+
listSize: .word 10
5+
msg: .asciiz "Array is: "
6+
space: .asciiz " "
7+
8+
.text
9+
10+
lw $t0, listSize
11+
la $a2, list
12+
li $t1, 0
13+
14+
print:
15+
beq $t1, $t0, exit
16+
lw $a0, ($a2)
17+
li $v0, 1
18+
syscall
19+
li $v0, 4
20+
la $a0, space
21+
syscall
22+
addi $a2, $a2, 4
23+
addi $t1, $t1, 1
24+
j print
25+
26+
exit:
27+
li $v0, 10
28+
syscall

reverse_string.asm

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# reverse a user inputted string in place and print it
2+
3+
.data
4+
5+
welcomeMsg: .asciiz "Input string: "
6+
userString: .space 256
7+
newline: .asciiz "\n"
8+
9+
.text
10+
11+
li $v0, 4
12+
la $a0, welcomeMsg
13+
syscall
14+
15+
li $v0, 8
16+
la $a0, userString
17+
li $a1, 256
18+
syscall
19+
20+
li $v0, 4
21+
syscall
22+
23+
add $t0, $a0, $zero
24+
lb $t5, newline
25+
find_end:
26+
lb $t1, ($t0)
27+
addi $t0, $t0, 1
28+
beq $t5, $t1, end
29+
bne $t1, $zero, find_end
30+
end:
31+
addi $t0, $t0, -2
32+
33+
swap:
34+
lb $t2, ($a0)
35+
lb $t3, ($t0)
36+
sb $t2, ($t0)
37+
sb $t3, ($a0)
38+
addi $a0, $a0, 1
39+
addi $t0, $t0, -1
40+
ble $a0, $t0, swap
41+
42+
li $v0, 4
43+
la $a0, userString
44+
syscall
45+
46+
li $v0, 10
47+
syscall

0 commit comments

Comments
 (0)