-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab7-Assignment5.asm
99 lines (88 loc) · 1.82 KB
/
Lab7-Assignment5.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
.data
Message1: .asciiz "dia chi gia tri lon nhat tai thanh ghi $s"
Message2: .asciiz ", gia tri lon nhat la "
Message3: .asciiz "\ndia chi gia tri nho nhat tai thanh ghi $s"
Message4: .asciiz ", gia tri nho nhat la "
.text
li $s0, 90
li $s1, 30
li $s2, 2
li $s3, 0
li $s4, -48
li $s5, 68
li $s6, -80
li $s7, 44
main:
sw $s0, -4($sp) # dat vao ngan xep cac gia tri tuong ung
sw $s1, -12($sp)
sw $s2, -20($sp)
sw $s3, -28($sp)
sw $s4, -36($sp)
sw $s5, -44($sp)
sw $s6, -52($sp)
sw $s7, -60($sp)
add $t0, $zero, $zero # khoi tao bien i = 0
jal loop
print_max:
li $v0, 4
la $a0, Message1
syscall
li $v0, 1
add $a0, $t2,$zero
syscall
li $v0, 4
la $a0, Message2
syscall
li $v0, 1
add $a0, $s0, $zero
syscall
print_min:
li $v0, 4
la $a0, Message3
syscall
li $v0, 1
add $a0, $t3, $zero
syscall
li $v0, 4
la $a0, Message4
syscall
li $v0, 1
add $a0, $s1, $zero
syscall
exit:
li $v0, 10
syscall
loop:
sw $t0, 0($sp)
addi $t0, $t0, 1 # i++
slti $t1, $t0, 8
bne $t1, $zero, move_to_next_number
lw $s0, -4($sp)
lw $t2, 0($sp) # vi tri
find_max:
lw $t3, 4($sp) # phan tu ke tiep sau max
lw $t4, 8($sp) # vi tri cua phan tu do
slt $t1, $t3, $s0 # so sanh the tim max
bne $t1, $zero, adjust_max
add $s0, $t3, $zero
add $t2, $t4, $zero
adjust_max:
addi $sp,$sp,8 # di chuyen con tro stack
bgtz $t4, find_max # Neu i > 0 thi loop
lw $s1,-4($sp) # khoi tao max ban dau
lw $t3,0($sp)
find_min:
lw $t5, -8($sp)
lw $t4,-12($sp)
slt $t1, $s1, $t4
bne $t1, $zero, adjust_min
add $s1, $t4, $zero
add $t3, $t5, $zero
adjust_min:
addi $sp, $sp, -8
slti $t1, $t5, 7
bne $t1, $zero, find_min
jr $ra
move_to_next_number:
addi $sp, $sp, -8
j loop