-
Notifications
You must be signed in to change notification settings - Fork 8
/
print_shapes.asm
122 lines (106 loc) · 1.91 KB
/
print_shapes.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# Write a program that asks if the user wants a triangle or a square. It then asks the
# user for the size of the object (the number of lines it takes to draw the object). The program
# then writes a triangle or a square of stars "*" to the console.*******
#
# *******
# *******
# *******
# *******
# *******
#
# or
#
# *
# **
# ***
# ****
# *****
# ******
#
# Write a subroutine for each figure. In them, use a subroutine print_star_line that writes a
# line of a given number of stars. (that number is passed as an argument to
# print_star_line function).
.data
msg: .asciiz "Triangle(0) or Square(1)?"
size: .asciiz "\nRequired size? "
star: .asciiz "*"
newline: .asciiz "\n"
.text
main:
li $v0, 4
la $a0, msg
syscall
li $v0, 5
syscall
move $t0, $v0
li $v0, 4
la $a0, size
syscall
li $v0, 5
syscall
move $a1, $v0
li $v0, 4
la $a0, newline
syscall
beq $t0, $zero, triangle
jal print_square
j exit
triangle:
jal print_triangle
exit:
li $v0, 10
syscall
print_square:
addi $sp, $sp, -4
sw $ra, ($sp)
li $t0, 1
move $t1, $a1
print_square_line:
addi $sp, $sp, -8
sw $t0, ($sp)
sw $t1, 4($sp)
move $a1, $t1
jal print_star_line
lw $t1, 4($sp)
lw $t0, ($sp)
addi $sp, $sp, 8
addi $t0, $t0, 1
ble $t0, $t1, print_square_line
lw $ra, ($sp)
addi $sp, $sp, 4
jr $ra
print_triangle:
# print a triangle of 'n' lines, where $a1 = n
addi $sp, $sp, -4
sw $ra, ($sp)
li $t0, 1
move $t1, $a1
print_line:
addi $sp, $sp, -8
sw $t0, ($sp)
sw $t1, 4($sp)
move $a1, $t0
jal print_star_line
lw $t1, 4($sp)
lw $t0, ($sp)
addi $sp, $sp, 8
addi $t0, $t0, 1
ble $t0, $t1, print_line
lw $ra, ($sp)
addi $sp, $sp, 4
jr $ra
print_star_line:
# print a line with 'n' stars, where $a1 = n
addi $sp, $sp, -4
sw $ra, ($sp)
li $v0, 4
la $a0, star
printstar:
syscall
addi $a1, $a1, -1
bgt $a1, $zero, printstar
la $a0, newline
syscall
lw $ra, ($sp)
addi $sp, $sp, 4
jr $ra