Chọn Add File trong Project menu, chọn kiểu flie
Từ đây chúng ta có thể gõ code vào cửa sổ + Lưu ý:
Để tránh gặp phải các lỗi khi compile hoặc link, ta cần làm 1 số bước sau :
Tạo thư mục Mips : C:\Mips. Trong tất cả các đường dẫn, không được phép có dấu cách.
hiện biên dịch : C:\Mips\bin\xgcc.exe 2. Trong thẻ Directories chọn các mục sau :
Executable files : C:\Mips\bin
Include files : C:\MyLab\“tên project”\Objects Library files : C:\Mips\lib
CHƯƠNG 4: Lập trình hợp ngữ MIPS Ví dụ minh họa: Ví dụ 1: .globl main main: addu $s7, $0, $ra .data .globl hello
hello: .asciiz "\nHello World" .text li $v0, 4 la $a0, hello syscall addu $ra, $0, $s7 jr $ra
Ví dụ 2: Chương trình chia bằng cách thực hiện phép trừ liên tiếp
# Chuong trinh chia bang cach thuc hien phep tru lien tiep # Muc dich : Thuc hien phep chia bang cach tru lien tiep #
# vao : So bi chia # So chia
# ra : Ket qua tuong ung va so du (neu co) #
# Cac thanh ghi duoc su dung
# $t0 - chua so bi chia va sau khi thuc hien phep chia luu so du (neu co) # $t1 - chua so chia
# $t2 - chua ket qua
############# Data segment ######################## .data
divisor: # so chia
.asciiz " Nhap vao so chia : " divident:
.asciiz " Nhap vao so bi chia : " result:
.asciiz " Ket qua la : " redundance:
.asciiz "\n"
############## Code segment ########################### .text
.globl main main:
la $a0,divident # nap vao dia chi so bi chia li $v0,4 # print string
syscall li $v0,5 syscall
move $t0,$v0 # nap SBC vao $t0 la $a0,divisor # nap vao dia chi so chia li $v0,4 # print string
syscall li $v0,5 syscall
move $t1,$v0 # nap SC vao $t1
li $t2,0 # ket qua ban dau duoc khoi tao bang 0 while: blt $t0,$t1,endWhile subu $t0,$t0,$t1 # t0 -= t1 addu $t2,$t2,1 # t2 +=1 b while endWhile:
la $a0,result # in ra ket qua li $v0,4
syscall
move $a0,$t2 # dua ket qua vao a0 va in ra li $v0,1
syscall
la $a0,redundance # in ra so du li $v0,4
move $a0,$t0 # dua ket qua vao a0 va in ra li $v0,1 syscall li $v0,10 syscall Ví dụ 3: Tính số fibonaci # Tinh so fib #
# Muc dich : tinh so fib
# su dung lap # Vao : n # Ra : So fib(n) # # Tham so su dung # $a0 : chua n
# $v0 : chua ket qua tra ve
########################### Data Segment ################### .data
prompt:
.asciiz "nhap vao so thu n > 2 trong day fib : \n" error:
.asciiz "nhap vao mot so khong phu hop" sumMsg:
.asciiz " ket qua la: " newline: .asciiz "\n" ################### Code Segment ##################### .text .globl main main: la $a0,prompt li $v0,4 syscall li $v0,5
syscall # ket qua nhap vao duoc chua vao $v0 move $s0,$v0 # luu ket qua vao t0
# co loi xay ra la $a0,error li $v0,4 syscall b main notError: move $a0,$s0 jal fib move $s0,$v0 #in ra ket qua la $a0,sumMsg li $v0,4 syscall move $a0,$s0 li $v0,1 syscall li $v0,10 syscall fib:
# chuong trinh con tinh so fib # vao : a0 chua n
# ra : ket qua duoc luu trong $v0 #
# cac thanh ghi duoc su dung # t1 : chua so f(n-2) # t0 : chua so f(n-1) li $v0,1 ble $a0,2,return li $s0,1 li $s1,1 # khoi tao f1 = f2 = 1 loop: add $v0,$s0,$s1
move $s1,$s0 # $t1 chua phan tu f(n-2) move $s0,$v0 # $t0 chua phan tu f(n-1) sub $a0,$a0,1
bgt $a0,2,loop return:
Ví dụ 4: Tìm min, max trong 3 số và tính hiệu 2 số
############# Data segment ######################## .data
prompt: # so chia
.asciiz " Nhap vao ba so : \n" result:
.asciiz " Ket qua la : " newline: .asciiz "\n" ############## Code segment ########################### .text .globl main main:
la $a0,prompt # load dia chi cua prompt li $v0,4 # goi ham in ra man hinh string syscall # thuc thi
li $v0,5 # goi ham read int tu ban phim syscall # thuc thi
move $t0,$v0 # $ t0 = $ v0
li $v0,5 # goi ham read int tu ban phim syscall # thuc thi
move $t1,$v0 # $ t1 = $ v0
li $v0,5 # goi ham read int tu ban phim syscall # thuc thi
move $t2,$v0 # $ t2 = $ v0 la $a0,result # load dia chi cua result
li $v0,4 # goi ham in ra man hinh string syscall # thuc thi
move $a0,$v0 # range = $a0 li $v0,1 # goi ham print integer syscall # thuc thi
li $v0,10 # goi ham thoat syscall # thuc thi findRange:
################## find range ############### # vao : ba so luu trong $t0,$t1,$t2
# ra : range luu trong $v0
sub $sp,$sp,4 # luu 1 word sw $ra,($sp) # push $ra
jal findMin sub $sp,$sp,4
sw $v0,($sp) # cat min vao stack jal findMax # $v0 = max
lw $t0,($sp) # t0 = min add $sp,$sp,4
sub $v0,$v0,$t0 # range = max - min lw $ra,($sp) # lay dia chi ra
add $sp,$sp,4 jr $ra
findMin:
################# find min #################### # vao : ba so luu trong $t0,$t1,$t2
# ra : so nho nhat trog 3 so
move $v0,$t0 # min = t0 ? ble $t0,$t1,min_t0_t1
move $v0,$t1 # sai, max = $t1 min_t0_t1:
ble $v0,$t2,min # min = $v0 ? move $v0,$t2 # sai, min = $t2 min:
jr $ra # quay ve
findMax:
################# find max #################### # vao : ba so luu trong $t0,$t1,$t2
bge $t0,$t1,max_t0_t1
move $v0,$t1 # sai, max = $t1 max_t0_t1:
bge $v0,$t2,max # max = $v0 ? move $v0,$t2 # sai, max = $t2 max:
jr $ra # quay ve
Ví dụ 5: Tính tổng 3 số
# Tinh tong cua ba so #
# Muc dich : tinh tong cua ba so bang cach chuyen tham so qua thanh ghi co ban #Vao 3 so can tinh tong
#Ra Tong #
########################### Data Segment ################### .data
prompt:
.asciiz "nhap vao ba so bat ki : \n" sumMsg:
.asciiz " ket qua la: " newline: .asciiz "\n" ################### Code Segment ##################### .text .globl main main: la $a0,prompt li $v0,4 syscall li $v0,5 syscall
move $a0,$v0 # dua so thu nhat vao $a0 li $v0,5
syscall
move $a1,$v0 # dua so thu hai vao $a1 li $v0,5
move $a2,$v0 # dua so thu ba vao $a2 jal sum
move $t0,$v0 # luu ket qua vao $a0 la $a0,sumMsg li $v0,4 syscall move $a0,$t0 li $v0,1 syscall li $v0,10 syscall sum:
# chuong trinh con tinh tong cua ba so
# vao : ba so can tinh tong duoc dat trong $a0 -> $a3 # ra : gia tri tra ve duoc luu trong $v0
addu $v0,$a0,$a1 addu $v0,$v0,$a2 jr $ra Ví dụ 6: Chương trình dịch các phần tử của mảng ################### Data segment ################### .data prompt:
.asciiz "Moi nhap 1 so: \n" sum_msg:
.asciiz "Ket qua sau khi dich bit la: \n" newline:
.asciiz "\n" .align 2 array:
.space 160 # space for 40 integers
################### Code segment ################### .text
la $a0,prompt # prompt user for input li $v0,4
syscall
la $t0,array # $t0 = array pointer li $t1,40 # $t1 = array size
read_loop:
li $v0,5 # read the input number syscall
beqz $v0,exit_loop # if number = 0, exit loop sw $v0,0($t0)
addu $t0,$t0,4 # update array pointer subu $t1,$t1,1 # decrement loop count bnez $t1,read_loop # if loop count is not 0, # read more
exit_loop: li $t3,39
subu $t3,$t3,$t1 # $t3 = array size li $t0,0
lw $t1,array+0($t0) # $t1 = first element permute: lw $t2,array+4($t0) sw $t2,array+0($t0) addu $t0,$t0,4 subu $t3,$t3,1 bnez $t3,permute
sw $t1,array($t0) # last element = first element la $a0,sum_msg # write sum message
li $v0,4 syscall
li $t0,0 # $t0 = array byte index write_loop:
lw $a0,array($t0) beqz $a0,write_done
la $a0,newline # write newline li $v0,4
syscall
addu $t0,$t0,4 # update array byte index bne $t0,160,write_loop
write_done:
la $a0,newline # write newline li $v0,4
syscall
li $v0,10 # exit syscall
Ví dụ 7: Chương trình tính tổng các phần tử trong xâu
# Chuong trinh cong tong cua tung so # Muc dich : Cong tong cua mot day so # Day so nhap vao co kieu string # vao : Day so duoc nhap vao
# ra : Tong #
# Cac thanh ghi duoc su dung # $t0 - chi toi dia chi cua xau vao # $t1 - hold digit for processing # $t2 - chua tong
############# Data segment ######################## .data
numberPrompt:
.asciiz " Nhap vao mot so ( < 11 ki tu ) : " outMsg:
.asciiz " Tong cua cac so trong day la : " newline:
.asciiz "\n" number:
.space 11 # khoang ki tu de nhap do
############## Code segment ########################### .text
la $a0,numberPrompt # load dia chi li $v0,4 # print line
syscall
la $a0,number # number chua dia chi tra ve cua chuoi ## bat buoc phai luu trong bo nho ???
la $a1,11 # Kich thuoc li $v0,8
syscall
la $a0,outMsg # load dia chi thong bao ra li $v0,4
syscall
la $t0,number # t0 chua dia chi xau vao li $t2,0
while:
lb $t1,($t0) # doc ki tu vao $t1
beq $t1,0xa,endWhile # neu $t1==line feed then exit beq $t1,0,endWhile # gap ki tu null ( ma ascii == 0) and $t1,$t1,0x0f
addu $t2,$t2,$t1 addu $t0,$t0,1 b while
endWhile:
move $a0,$t2 #dua tong vao $a0 li $v0,1 syscall la $a0,newline li $v0,4 syscall li $v0,10 #quit syscall
Ví dụ 8: Chương trình nhân đôi 1 số nguyên
.data
szChao: .asciiz"Xin chao cac ban\n"
szCRLF: .asciiz"\n" .text
.globl main main:
# Buoc 1: Dua thong bao xin chao li $v0,4
la $a0,szChao syscall
Lap:
# Thong bao nhap 1 so li $v0,4
la $a0,szYeucaunhap syscall
# Doc gia tri, cat vao thanh ghi $s0 li $v0,5
syscall
move $s0,$v0 # Tinh nhan doi addu $a0,$s0,$s0 # Dua ra man hinh li $v0,1 syscall li $v0,4 la $a0,szCR syscall li $v0,11 li $a0,97 syscall jal Lap li $v0, 10 # Syscall to exit. syscall
# # data segment .data
string0: .asciiz "Day la xau vao\n" item0: .word 99
array0: .word 1 1 22 33 44 55 66 77 88 99 # mang hang so #-# # code segment
main: .text
.globl main
#Vi du 9: In 1 chuoi tren man hinh console, phuong phap su dung la load dia chi cua xau vao #$a0 sau do dung loi goi he thong de in xau
la $a0, string0 li $v0, 4 Syscall
# EXAMPLE 10: Nap vao 1 so nguyen tu data segment va in ra. lw $a0, item0
li $v0, 1 Syscall