-
Notifications
You must be signed in to change notification settings - Fork 0
/
printfString.s
83 lines (66 loc) · 1.4 KB
/
printfString.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
.data
str1: .asciiz "The number is # and here is a string $ that gets spliced in"
str2: .asciiz "WHEEE!"
.text
main: addi $sp, $sp, -4
sw $ra, 0($sp)
# Push all the arguments on the stack
addi $sp, $sp, -12
la $t0, str2
sw $t0, 8($sp)
li $t0, 999
sw $t0, 4($sp)
la $t0, str1
sw $t0, 0($sp)
jal printf
addi $sp, $sp, 12
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
# Your Modified Printf Here!
# Add support for $ to insert a string!
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 $t0, '$'
beq $a0, $t0, str_else
#if statement -- check against '$'
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
j if_done
str_else:
lw $a0, 0($s1)
li $v0, 4
syscall
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