-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_ints.s
85 lines (63 loc) · 1.66 KB
/
print_ints.s
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
# Ram Bhagat
.data
str: .asciiz "# squared is #\n"
.text
main:
addi $sp, $sp, -4
sw $ra, 0($sp)
li $s0, 1 # $t0 = 1
li $s5, 10 # $t5 = 10
addi $sp, $sp, -12
squaredLoop:
bgt $s0, $s5, endLoop
mul $s1, $s0, $s0 # $t1 = i * i
sw $s1, 8($sp)
sw $s0, 4($sp)
la $s1, str # $t1 = str
sw $s1, 0($sp)
jal printf
addi $s0, $s0, 1 # i++
j squaredLoop
endLoop:
addi $sp, $sp, 12
# restore RA, return
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
printf:
# Store ra, s0, s1
addi $sp, $sp, -12
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
# The arguments to the function are now at
# 12($sp) and above
lw $s0, 12($sp)
addi $s1, $sp, 16
# $s0 is now the POINTER to the original string
# $s1 is a pointer to the first argument after the string
# Get one letter at a time and print
loop: lb $a0, 0($s0)
# Exit if it's zero
beq $a0, $0, done
# if statement -- check against '#'
li $t0, '#'
beq $a0, $t0, else
# "If" part -- if it's not #
li $v0, 11
syscall
b if_done
else: # print the number pointed to by s1, then move the pointer
lw $a0, 0($s1)
li $v0, 1
syscall
addi $s1, $s1, 4
if_done:
addi $s0, $s0, 1
b loop
done: # Restore registers, return
lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
addi $sp, $sp, 12
jr $ra