-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswap.asm
75 lines (58 loc) · 933 Bytes
/
swap.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
# modify and swap two variables in memory
.globl _start
.data # variable declarations
var1: .word 15
var2: .word 19
newline: .asciz "\n"
.text # instructions
_start:
li a7, 1
la a0, var1
ecall
jal print_newline
li a7, 1
la a0, var2
ecall
jal print_newline
# load addresses of var1, var2 to registers t0, t1
la t0, var1
la t1, var2
# load values of var1, var2 to registers t2, t3
lw t2, (t0)
lw t3, (t1)
# modify var1, var2
addi t2, t2, 1
slli t3, t3, 2
# print var1, var2
li a7, 1
mv a0, t2
ecall
jal print_newline
li a7, 1
mv a0, t3
ecall
jal print_newline
# swap var1, var2 in memory
sw t2, (t1)
sw t3, (t0)
# load values of var1, var2 to registers again
lw t2, (t0)
lw t3, (t1)
# print var1, var2
li a7, 1
mv a0, t2
ecall
jal print_newline
li a7, 1
mv a0, t3
ecall
# end
j exit
print_newline:
li a7, 4
la a0, newline
ecall
ret
exit:
li a7, 10
ecall