-
Notifications
You must be signed in to change notification settings - Fork 0
/
27.ReverseArray.asm
119 lines (81 loc) · 1.9 KB
/
27.ReverseArray.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
.text
.globl main
main:
# printing
# msg1
li $v0, 4
la $a0, msg1
syscall
# get number of elements onto $s1
li $v0, 5
syscall
move $s1, $v0 # $s1 length
la $s3, array # $s3 array
j getArrayInput
getArrayInput:
li $t0, 0
loop2:
beq $t0, $s1, back
# mgs2 enter element
li $v0, 4
la $a0, msg2
syscall
# get input
li $v0, 5
syscall
move $t8, $v0
sw $t8, ($s3)
addi $s3, $s3, 4 # next array element
addi $t0, $t0, 1 # next number
j loop2
back:
la $s3, array # $s3 array
la $s4, array
addi $t9, $s1, -1
sll $t9, $t9, 2
add $s4, $s4, $t9
# addi $s4, $s4, 16 # last word
# la $s1, len
# lw $s1, ($s1) # $s1 length = 5
li $t0, 1 # $t0 first element
move $t1, $s1 # $t1 last element
loop1:
beq $t0, $t1, exit
bgt $t0, $t1, exit
lw $t3, ($s3) # $t3 front load
lw $t4, ($s4) # $t4 back load
sw $t3, ($s4) # swap and store
sw $t4, ($s3)
addi $t0, $t0, 1 # exit conditions
addi $t1, $t1, -1
addi $s3, $s3, 4 # from first move to next word
addi $s4, $s4, -4 # from last come back one word
j loop1 # jump to loop
exit:
# 1
la $t5, array # $t5 base address
li $t0, 0 # $t0
move $t1, $s1 # $t1, length
loop3:
# display new array
beq $t0, $t1, end # exit condition
lw $t2, ($t5) # each word
li $v0, 1 # print value
move $a0, $t2
syscall
li $v0, 4 # print newline
la $a0, newline
syscall
# increment word and loop condition
addi $t5, $t5, 4
addi $t0, $t0, 1
j loop3
end:
li $v0, 10
syscall
.data
array: .space 400
newline: .asciiz "\n"
number: .space 4
msg1: .asciiz "Enter the number of Elements: "
msg2: .asciiz "Enter element : "