-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintNumberReverseSingleLine
More file actions
99 lines (85 loc) · 1.85 KB
/
PrintNumberReverseSingleLine
File metadata and controls
99 lines (85 loc) · 1.85 KB
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
.data
myArray: .space 80
message: .asciiz "Enter a number: "
message1: .asciiz " "
message2: .asciiz "Enter a number that is less than or equal 20: "
message3: .asciiz "\n"
.align 2
.text
main:
li $t0,0 #make sure $t0 is 0
while:
li $t1, 80 #load 76 into $a0
slt $t1, $t1,$t0 #compare $a0 with $t0
bne $t1, 0, exit #while loop if size $t1= 80 exit the loop
#print the message
li $v0, 4
la $a0, message
syscall
#ask user to input the number
li $v0, 5
syscall
#store the number into array
sw $v0, myArray($t0)
#every input takes 4 bits so add 4 bits into the arraysize
addi $t0, $t0, 4
j while
exit:
#make $t0 to 80 in order to print reverse
li $t0, 80
#another loop that load the numbers
loop:
beq $t0 , 0 , finish #if $t0 = 0 jump to finish
lw $t6, myArray($t0) #load from array into $t6
subi $t0, $t0, 4 #subtract to $t0 each time
li $v0,1
move $a0, $t6 #print out $t6
syscall
li $v0, 4 #print a space
la $a0, message1
syscall
j loop
finish:
#print a new line
li $v0,4
la $a0,message3
syscall
#ask user to inputer a number that equal or less than 20
li $v0, 4
la $a0 ,message2
syscall
#user input number
li $v0, 5
move $s1, $v0
syscall
li $t0, 80 #load 80 into $t0
li $t1,0 #load 0 into $t1
#forloop print number
forloop:
beq $t0, 0, last #if $t0 = 0 jump to last
lw $t6, myArray($t0) #load word into $t6 from array($t0)
subi $t0, $t0,4 #reverse, subtract 4 everytime from $t0
#print the number
li $v0, 1
move $a0,$t6
syscall
#separate the number by space
li $v0,4
la $a0, message1
syscall
#if-statement if $t1 = userInput number then jump to line
addi $t1,$t1,1
beq $t1, $s1, line
#else jump back to forloop
j forloop
#print a line
line:
li $t1,0 #reset $t1 to 0
li $v0,4
la $a0, message3
syscall
j forloop #jump back to the forloop
#end call
last:
li $v0,10
syscall