Bài tập mẫu về mảng trong MIPS
# array.asm
# nhap va xuat mang mot chieu cac so nguyen
# cac thanh ghi su dung:
# $t0: so luong phan tu n
# $t1: chi so mang
# $a1: dia chi co so (base address) cua mang[/color]
.data
input1: .asciiz "Nhap n: "
input2: .asciiz "[ "
input3: .asciiz " ] = "
output: .asciiz "\nMang da nhap: "
array: .word 0:100 # int array[100]
.text
main:
# nhap n
la $a0, input1
addi $v0, $zero, 4
syscall
addi $v0, $zero, 5
syscall
addi $t0, $v0, 0
# khoi tao
addi $t1, $zero, 0
la $a1, array
NhapMang:
# kiem tra so lan lap
subu $t2, $t1, $t0
bgez $t2, KetThucNhap
# xuat dau nhac nhap
la $a0, input2
addi $v0, $zero, 4
syscall
addi $a0, $t1, 0
addi $v0, $zero, 1
syscall
la $a0, input3
addi $v0, $zero, 4
syscall
# nhap so nguyen va luu vao array[i]
addi $v0, $zero, 5
syscall
sw $v0, ($a1)
# tang chi so
addi $t1, $t1, 1
addi $a1, $a1, 4
j NhapMang
KetThucNhap:
la $a0, output
addi $v0, $zero, 4
syscall
# khoi tao
la $a1, array
addi $t1, $zero, 0
XuatMang:
# kiem tra so lan lap
subu $t2, $t1, $t0
bgez $t2, KetThucXuat
# xuat phan tu array[i]
lw $a0, ($a1)
addi $v0, $zero, 1
syscall
# xuat khoang trang
addi $a0, $zero, 0x20
addi $v0, $zero, 11
syscall
# tang chi so
addi $t1, $t1, 1
addi $a1, $a1, 4
j XuatMang
KetThuc:
addi $v0, $zero, 10
syscall
.data
Trả lờiXóan: .word 0:100 # gioi han 100 ptu
tb1:.asciiz"\nNhap phan tu mang:"
tb2:.asciiz"["
arr: .asciiz ""
tb3:.asciiz"]"
tb4: .asciiz"\n cac phan tu 1234:"
.text
main:
# xuat tb1
la $a0,tb1
li $v0,4
syscall
li $v0, 5
syscall
addi $t0, $v0, 0
# khoi tao mang
addi $t1,$zero,0 #goi $t1 la chi so mang
la $a1,n # goi $a1 la dia chi co so mang
nhap:
# khoi tao vong lap
li $t0,0
lw $a1,n
# kiem tra vong lap
sub $t2,$t0,$t1
bgtz $t2,ketthuc
#xuat tb2
la $a0,tb2
li $v0,4
syscall
# xuat chi so mang
addi $a0,$t1,0
li $v0,1
syscall
#xuat tb3
la $a0,tb3
li $v0,4
syscall
# nhap va luu so nguyen vao mang
li $v0,5
syscall
sw $v0,($a1)
#tang chi so $t1 cua mang n
addi $t1,$t1,1 # i++
addi $a1,$a1,4
j nhap # quay lai kt vong lap
ketthuc:
#xuat tb4
la $a0,tb4
li $v0,4
syscall
# khoi tao mang sn
la $a1,n
li $t1,0
addi $t1,$t1,1 #tang chi so len 1
addi $a1,$a1,4
xuat:
# xuat lap
sub $t2,$t0,$t1
bgtz $t2,Exit
#xuat phan tu mang
lw $a0,($a1)
li $v0,1
syscall
addi $t1,$t1,1
addi $a1,$a1,4
#xuat tb3
la $a0,tb3
li $v0,4
syscall
j xuat
Exit:
li $v0,10
syscall
// dòng này # nhap va luu so nguyen vao mang
li $v0,5
syscall
sw $v0,($a1)
của mình ko chay dc bạn có thể xem giúp mình dc ko