Tải bản đầy đủ (.pdf) (7 trang)

Bài tập hợp ngữ MIPS 2 ppsx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (132.5 KB, 7 trang )

Bài tập hợp ngữ MIPS 2
1. Nhập vào N số nguyên, lưu vào Stack. Xuất ra màn hình theo thứ tự ngược
lại lúc nhập vào. Với N <= 20.
Ví dụ:
Nhập vào: 9 7 2 0 4 6 1
Xuất ra: 1 6 4 0 2 7 9
2. Xuất ra màn hình N số nguyên chẵn đầu tiên. Với N <= 20.
Ví dụ:
N = 5
Xuất ra: 0 2 4 6 8
3. Dùng thuật toán Insertion Sort để sắp xếp dãy số tăng dần:
Ví dụ:
Dãy nhập vào là: 7 3 2 0 4 9 4 8
Dãy sắp xếp là: 0 2 3 4 4 7 8 9
Bài 1:
.data
text_1: .asciiz "n = "
text_2: .asciiz "Xuat ra: "
.text
main:
la $a0, text_1 #load text_1 string
jal print_text

jal read_int
move $s0, $v0 # n: $s0

move $s1, $s0 # $s1 = $s0
begin_loop_1:
beq $s1, $zero, end_loop_1 # if (n==0) jump end_loop_1

jal read_int


move $a0, $v0
jal stack_push
addi $s1, $s1, -1 # n

j begin_loop_1
end_loop_1:

la $a0, text_2 #load text_2 string
jal print_text

move $s1, $s0 # $s1 = $s0
begin_loop_2:
beq $s1, $zero, end_loop_2 # if (n==0) jump end_loop_2

jal stack_pop
move $a0, $v0
jal print_int
jal print_space

addi $s1, $s1, -1 # n
j begin_loop_2
end_loop_2:

j end_program

print_text:
li $v0, 4 #print text
syscall
jr $ra


read_int:
li $v0, 5 #read interger
syscall
jr $ra

print_int:
li $v0, 1 #print interger
syscall
jr $ra

print_space:
addi $a0,$zero, 32 # $a0 = " "
li $v0, 11 #print character
syscall
jr $ra

stack_push:
addi $sp, $sp, -4 # $sp -=4 (new 4 byte in stack)
sw $a0, 0($sp) #put a word to stack
jr $ra

stack_pop:
lw $v0, 0($sp) #get a word form stack
addi $sp, $sp, 4 # $sp +=4 (free 4 byte in stack)
jr $ra

end_program:
Bài 2:
.data
text_1: .asciiz "n = "

text_2: .asciiz "Xuat ra: "

.text

main:
la $a0, text_1 #load text_1 string
jal print_text

jal read_int
move $s0, $v0 # n: $s0

la $a0, text_2 #load text_2 string
jal print_text

li $s1, 0 # $s1=0

begin_loop:
beq $s0, $zero, end_loop # if (n==0) jump end_loop

move $a0, $s1
jal print_int
jal print_space

addi $s1, $s1, 2 # $s1 += 2
addi $s0, $s0, -1 # n

j begin_loop
end_loop:

j end_program


print_text:
li $v0, 4 #print text
syscall
jr $ra

read_int:
li $v0, 5 #read interger
syscall
jr $ra

print_int:
li $v0, 1 #print interger
syscall
jr $ra

print_space:
addi $a0,$zero, 32 # $a0 = " "
li $v0, 11 #print character
syscall
jr $ra

end_program:
Bài 3:
.data
text_1: .asciiz "n = "
text_2: .asciiz "Xuat ra: "
.text
main:
la $a0, text_1 #load text_1 string

jal print_text

jal read_int
move $s0, $v0 # n: $s0

move $s1, $s0 # $s1 = $s0
begin_loop_1:
beq $s1, $zero, end_loop_1 # if (n==0) jump end_loop_1

jal read_int
move $a0, $v0
jal stack_push
addi $s1, $s1, -1 # n

j begin_loop_1
end_loop_1:

addi $a0, $zero, 1
move $a1, $s0
jal insert_sort

la $a0, text_2 #load text_2 string
jal print_text

move $s1, $s0 # $s1 = $s0
begin_loop_2:
beq $s1, $zero, end_loop_2 # if (n==0) jump end_loop_2

jal stack_pop
move $a0, $v0

jal print_int
jal print_space

addi $s1, $s1, -1 # n
j begin_loop_2
end_loop_2:

j end_program

print_text:
li $v0, 4 #print text
syscall
jr $ra

read_int:
li $v0, 5 #read interger
syscall
jr $ra

print_int:
li $v0, 1 #print interger
syscall
jr $ra

print_space:
addi $a0,$zero, 32 # $a0 = " "
li $v0, 11 #print character
syscall
jr $ra


stack_push:
addi $sp, $sp, -4 # $sp -=4 (new 4 byte in stack)
sw $a0, 0($sp) #put a word to stack
jr $ra
stack_pop:
lw $v0, 0($sp) #get a word form stack
addi $sp, $sp, 4 # $sp +=4 (free 4 byte in stack)
jr $ra

insert_sort: #vitri=$a0

beq $a0, $a1, end_sort

move $t0,$sp
move $t1, $a0
begin_loop:
beq $t1, $zero, end_loop
addi $t0, $t0, 4
addi $t1, $t1, -1
j begin_loop
end_loop:

lw $t1,0($t0) # $t1=a[i]

for:
beq $t0, $sp, next
lw $t2, -4($t0) # $t2=a[j-1]
ble $t2, $t1, next
sw $t2,0($t0)
addi $t0, $t0, -4

j for
next:
sw $t1,0($t0) #a[j]=$t1

addi $a0, $a0, 1
j insert_sort
end_sort:
jr $ra

end_program:

×