-
Notifications
You must be signed in to change notification settings - Fork 8
/
print_pyramid.asm
92 lines (78 loc) · 1.34 KB
/
print_pyramid.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
# Print a pyramid of height 'n' (user input) as shown below (for n=4)
# *
# * *
# * * *
# * * * *
.data
msg: .asciiz "Enter height of pyramid: "
space: .asciiz " "
newline: .asciiz "\n"
star: .asciiz "*"
.text
main:
# user input
li $v0, 4
la $a0, msg
syscall
li $v0, 5
syscall
move $s0, $v0 # height of pyramid
move $s1, $v0
addi $s1, $s1, -1 # number of spaces required before each line
li $v0, 4
la $a0, newline
syscall
li $t0, 1
print_row:
bgt $t0, $s0, exit
# print spaces
move $a1, $s1
jal print_spaces
addi $s1, $s1, -1
# print stars
move $a1, $t0
jal print_stars
addi $t0, $t0, 1
j print_row
exit: li $v0, 10
syscall
print_stars:
# print $a1 number of stars
addi $sp, $sp, -12
sw $ra, ($sp)
sw $v0, 4($sp)
sw $a0, 8($sp)
li $v0, 4
star_loop:
beq $a1, $zero, return_stars
la $a0, star
syscall
la $a0, space
syscall
addi $a1, $a1, -1
j star_loop
return_stars: la $a0, newline
syscall
lw $ra, ($sp)
lw $v0, 4($sp)
lw $a0, 8($sp)
addi $sp, $sp, 12
jr $ra
print_spaces:
# print $a1 number of spaces
addi $sp, $sp, -12
sw $ra, ($sp)
sw $v0, 4($sp)
sw $a0, 8($sp)
li $v0, 4
la $a0, space
print_one_more:
beq $a1, $zero, return_spaces
syscall
addi $a1, $a1, -1
j print_one_more
return_spaces: lw $ra, ($sp)
lw $v0, 4($sp)
lw $a0, 8($sp)
addi $sp, $sp, 12
jr $ra