.data
cau1: .asciiz "\nNhap vao 1 chuoi: "
endl: .asciiz "\n"
cau1_2: .asciiz "\nChuoi vua nhap: "
declaration: .asciiz "\nNhap 1 ky tu: "
cau2a: .asciiz "Ky tu lien truoc: "
cau2b: .asciiz "Ky tu lien sau: "
cau3: .asciiz "Ky tu thuong: "
cau4_1: .asciiz "\nNhap so thu nhat: "
cau4_2: .asciiz "\nNhap so thu hai: "
cau4_3: .asciiz "\nTong: "
cau4_4: .asciiz "\nHieu: "
cau4_5: .asciiz "\nTich: "
cau4_6: .asciiz "\nThuong: "
cau4_7: .asciiz " | Du: "
stNhapA: .asciiz "Nhap gia tri so A: "
stNhapB: .asciiz "Nhap gia tri so B: "
stLon: .asciiz "So Lon Hon La: "
chuSo: .asciiz " la chu so\n"
chuCai: .asciiz " la chu cai\n"
chuInHoa: .asciiz " la chu in hoa\n"
NhapN: .asciiz "\nNhap vao 1 so: "
KetQua: .asciiz "\nTong tu "
den: .asciiz " den "
LA: .asciiz " la: "
mang: .word 0:100
NhapMang: .asciiz "\nNhap so phan tu mang: "
MangVuaNhap: .asciiz "\nMang vua nhap: "
KhoangTrang: .asciiz " "
str: .space 100 # chu?i ban ??u
revstr: .space 100 # chu?i sau khi ??o ng??c
NhapChuoi: .asciiz "\nNhap vao 1 chuoi: "
DaoNguoc: .asciiz "\nChuoi vua duoc dao nguoc: "
.text
main:
# Nhap / xuat 1 chuoi
la $a0, cau1
addi $v0, $0, 4
syscall
addi $a1, $0, 40 # khoi tao bo dem a1 de nhap chuoi voi 40 ki tu
addi $v0, $0, 8
syscall
addi $s0, $a0, 0 # luu chuoi vua nhap vao $s0
la $a0, cau1_2 # in thong bao
addi $v0, $0, 4
syscall
addi $a0, $s0, 0 # xuat chuoi
addi $v0, $0, 4
syscall
# Nhap 1 ki tu, xuat ra ki tu truoc, sau no:
la $a0, declaration # in thong bao nhap
addi $v0, $0, 4
syscall
addi $v0, $0, 12 # Nhap
syscall
addi $s0, $v0, 0
la $a0, endl # xuong hang
addi $v0, $0, 4
syscall
la $a0, cau2a # in thong bao xuat 1
addi $v0, $0, 4
syscall
addi $a0, $s0, -1 # truoc
addi $v0, $0, 11 # xuat
syscall
la $a0, endl
addi $v0, $0, 4
syscall
la $a0, cau2b # in thong bao xuat 2
addi $v0, $0, 4
syscall
addi $a0, $s0, 1 # sau
addi $v0, $0, 11 # xuat
syscall
la $a0, endl # xuong hang
addi $v0, $0, 4
syscall
# Nhap 1 ki tu in hoa, xuat ra ki tu thuong
la $a0, declaration # in thong bao nhap
addi $v0, $0, 4
syscall
addi $v0, $0, 12 # nhap ki tu
syscall
addi $s0, $v0, 32 # Bien thanh ki tu thuong
la $a0, endl # xuong hang
addi $v0, $0, 4
syscall
la $a0, cau3 # in thong bao xuat
addi $v0, $0, 4
syscall
addi $a0, $s0, 0 # xuat ket qua
addi $v0, $0, 11
syscall
# Nhap 2 so, tinh tong, hieu, tich, thuong
# Nhap so thu nhat:
la $a0, cau4_1 # thong bao nhap 1
addi $v0, $0, 4
syscall
addi $v0, $0, 5 # nhap so thu nhat
syscall
add $s0, $0, $v0 # Luu so thu nhat vao $s0
# Nhap so thu 2:
la $a0, cau4_2 # in thong bao nhap thu 2
addi $v0, $0, 4
syscall
addi $v0, $0, 5 # nhap so thu hai
syscall
add $s1, $0, $v0 # Luu so thu hai vao $s1
# Tinh tong:
la $a0, cau4_3 # in thong bao xuat tong
addi $v0, $0, 4
syscall
add $s2, $s0, $s1 # tinh tong
addi $a0, $s2, 0 # xuat ket qua
addi $v0, $0, 1
syscall
# Tinh hieu
la $a0, cau4_4 # in thong bao xuat hieu
addi $v0, $0, 4
syscall
sub $s2, $s0, $s1# tinh hieu
addi $a0, $s2, 0 # Xuat ket qua
addi $v0, $0, 1
syscall
# Tinh tich:
la $a0, cau4_5 # in thong bao xuat tich
addi $v0, $0, 4
syscall
mult $s0, $s1 # nhan 2 so
mflo $s2 # bo ket qua vua tinh duoc vao $s2
addi $a0, $s2, 0 # Xuat ket qua
addi $v0, $0, 1
syscall
# T?nh th??ng
la $a0, cau4_6 # in thong bao xuat thuong
addi $v0, $0, 4
syscall
div $s0, $s1 # chia 2 so
mflo $s2 # xuat phan chia het vao $s2
mfhi $s3 # xuat phan du vao $s3
addi $a0, $s2, 0 # xuat thuong
addi $v0, $0, 1
syscall
la $a0, cau4_7 # in thong bao du
addi $v0, $0, 4
syscall
addi $a0, $s3, 0 # Xuat phan du
addi $v0, $0, 1
syscall
la $a0, endl # xuong hang
addi $v0, $0, 4
syscall
# Nhap 2 so, tim so lon hon:
NhapSoThuI:
la $a0, stNhapA # in thong bao nhap 1
addi $v0, $0, 4
syscall
addi $v0, $0, 5 # nhap so thu nhat:
syscall
addi $s1, $v0, 0 # luu vao $s1
NhapSoThuII:
la $a0, stNhapB # in thong bao nhap 2
addi $v0, $0, 4
syscall
addi $v0, $0, 5 # nhap so thu hai:
syscall
addi $s2, $v0, 0 # luu vao $s2
# thu?t to?n kh?ng c?n d?ng l?nh ?i?u ki?n:
# int c = a - b;
# int k = (c >> 31) & 0x1;
# int max = a - k * c;
# ?? tham kh?o t?i: http://stackoverflow.com/questions/4772780/explain-this-snippet-which-finds-the-maximum-of-two-integers-without-using-if-el
sub $s3, $s1, $s2 # s3 = s1 - s2
sra $s4, $s3, 31 # s4 = s3 >> 31
and $s4, $s4, 1 # s4 = s4 & 1
mult $s4, $s3
mflo $s5 # s5 = s4 * s3
sub $s6, $s1, $s5 # s6 = s1 - s5 => Gi? tr? c?n t?m
la $a0, stLon # in thong bao xuat ket qua
addi $v0, $0, 4
syscall
addi $a0, $s6, 0 # xuat ket qua
addi $v0, $0, 1
syscall
# Nhap 1 ki tu, xet la chu thuong, chu hoa hay so
la $a0, declaration # in thong bao nhap
addi $v0, $0, 4
syscall
addi $v0, $0, 12 # nhap
syscall
addi $s0, $v0, 0 # luu vao $s0
la $a0, endl # xuong hang
addi $v0, $0, 4
syscall
addi $a0, $s0, 0 #in ki tu vua nhap
addi $v0, $0, 11
syscall
#xet ma ASCII
# chu so
subi $s2, $s0, 58 # so sanh ma ASCII
bltz $s2, laChuSo # neu thoa thi in ra chu so
# chu in hoa
subi $s4, $s0, 91 # so sanh ma ASCII
bltz $s4, laChuInHoa # neu thoa thi in ra chu in hoa
# chu cai
subi $s3, $s0, 123 # so sanh ma ASCII
bltz $s3, laChuThuong # neu thoa thi in ra chu thuong
j fin # sai thi nhay den fin
laChuSo:
la $a0, chuSo # in ket qua
addi $v0, $0, 4
syscall
j fin3 # sai thi nhay den fin 3
laChuInHoa:
la $a0, chuInHoa # in ket qua
addi $v0, $0, 4
syscall
j fin2 # sai thi nhay den fin 2
laChuThuong:
la $a0, chuCai # in ket qua
addi $v0, $0, 4
syscall
fin2:
fin:
fin3:
# Nhap so phan tu roi nhap tung phan tu cua mang, in ra mang ra man hinh:
#Nhap so luong phan tu
la $a0, NhapMang # in thong bao nhap so luong
addi $v0, $0, 4
syscall
addi $v0, $0, 5 # nhap so luong
syscall
addi $s0, $v0, 0 # luu so luong phan tu vao $s0
addi $s1, $s0, 0 # s1 = s0
la $t0, mang # gan dia chi mang vao $t0
do: # Nhap tung phan tu
beq $s1, 0, ndo # s1 = 0 thi nhay sang ndo
addi $v0, $0, 5 # nhap phan tu
syscall
sw $v0, ($t0) # luu vao $t0
addi $s1, $s1, -1 # giam di so luong can nhap di 1
addi $t0, $t0, 4 # t0 = t0 + 4, nhay den phan tu ke tiep (moi phan tu la 4 byte)
b do # nhay den do
ndo:
la $a0, MangVuaNhap # in thng bao xuat
addi $v0, $0, 4
syscall
la $t0, mang # gan di chi mang cho t0
addi $s1, $s0, 0 # s1 = s0 = so luong phan tu
do_1: # Xuat tung phan tu
beq $s1, 0, ndo_1 # khi s1 < 0 thi nhay den ndo_1
lw $t2, ($t0) # ghi dia chi t0 vao t2, tro toi phan tu mang
addi $a0, $t2, 0 # in t2, con tro phan tu, ra man hinh
addi $v0, $0, 1
syscall
la $a0, KhoangTrang # in khoang trang
addi $v0, $0, 4
syscall
addi $s1, $s1, -1 # giam di so luong can in di 1
addi $t0, $t0, 4 # nhay den phan tu ke tiep
b do_1 # lap lai
ndo_1:
# Nhap vao 1 so nguyen n, tinh tong cac phan tu tu 1 toi n
addi $v0, $0, 4 # in thong bao nhap so luong phan tu
la $a0, NhapN
syscall
addi $v0 ,$0, 5 # nhap n
syscall
add $s0, $0, $v0 # so luong phep tinh dang nam trong $s0
add $t0, $0, $0 # bien chay la $t0
add $s1, $0, $0 # Tong la $s1
addi $s2, $0, 1 # s2 = 1, bat dau tu 1
Next:
addi $t1, $t0, 1
ble $t0, $s0, VongLap # dieu kien dung: t0 <= s0 ( i < n), neu chua dat thi nhay den VongLap
# Khi da tinh xong
la $a0, KetQua # in thong bao ket qua
addi $v0, $0, 4
syscall
addi $a0, $s2, 0 # in so 1
addi $v0, $0, 1
syscall
addi $v0,$0,4 # in chu " den "
la $a0, den
syscall
addi $a0, $t0, 0 # in n
addi $v0, $0, 1
syscall
la $a0, LA
addi $v0,$0,4 # in " la: "
syscall
# In ket qua
addi $v0, $0, 1
add $a0, $0, $s1
syscall
j EXIT
VongLap: # qua trinh tinh toan
add $s1, $s1, $t0 # s1 += i;
addi $t0, $t0, 1 # buoc nhay, i + 1
j Next
EXIT:
# Nhap 1 chuoi, xuat ra chuoi in nguoc
la $a0, NhapChuoi # in thong bao nhap
addi $v0, $0, 4
syscall
addi $a1, $0, 100 # khoi tao bo dem de nhap 100 ki tu
addi $v0, $0, 8 # nhap chuoi
la $a0, str # luu ket qua vua nhap vao bien str
syscall
addi $t1, $0, 0 # t1 = 0
la $a0, DaoNguoc # in thong bao ket qua
addi $v0, $0, 4
syscall
lap:
lbu $t0, str($t1)
beqz $t0, khoiTao # t0 = 0 thi nhay den khoiTao
addi $t1, $t1, 1
j lap
khoiTao: # khoi tao i, j de duyet tung phan tu cua 2 chuoi
addi $t1, $t1, -1 # i = str.length() - 1, chay tu cuoi len dau
addi $t2, $0, 0 # j = 0, chay tu dau
chuyenDoi: # qua trinh bien doi chuoi
blt $t1, $0, hoanThanh # t1 < 0 th? xong.
lb $t3, str($t1) # t3 = str[i]
sb $t3, revstr($t2) # t3 = revstr[j]
subi $t1, $t1, 1 # i - 1
addi $t2, $t2, 1 # j + 1
j chuyenDoi
hoanThanh: # sau khi hoan thanh
la $a0, revstr
addi $v0, $0, 4 # in ket qua
syscall
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.