Thứ Hai, 5 tháng 3, 2012

Bài tập hợp ngữ MIPS 1


Xây dựng chương trình hợp ngữ MIPS sau:

1. Viết chương trình nhập vào năm sinh và năm hiện tại, in ra số tuổi.
Ví dụ:
Nam hien tai: 2010
Nam sinh cua ban: 1989
Ket qua: ban duoc 21 tuoi.

2. Viết chương trình, thực hiện nhập vào một ký tự và in mã ASCII tương ứng của ký tự đó.
Ví dụ:
Nhap ky tu: B
Ma ASCII tuong ung: 42
3. Nhập vào 3 số a, b, c, in ra màn hình 3 số này theo thứ tự tăng dần.
Ví dụ:
So a: 3
So b: 5
So c: 2
Sau khi sap thu tu: 2 3 5
4. Nhập vào 4 số a, b, c, d (không dùng mảng dữ liệu):
a. In ra số lớn nhất và nhỏ nhất
b. In ra 2 số không phải lớn nhất và nhỏ nhất
Ví dụ:
So a: 3
So b: 5
So c: 2
So d: 7
So lon nhat: 7 va So nho nhat: 2
Hai so khong phai la so lon nhat va nho nhat: 3 5
 Bài 1:
.data
  s1: .asciiz   "Nam hien tai: "
  s2: .asciiz   "Nam sinh cua ban: "
  s3: .asciiz   "Ket qua: ban duoc "
  s4: .asciiz   " tuoi"
.text

main:
  la $a0, s1
  li $v0, 4
  syscall

  li $v0, 5
  syscall
  move $t0, $v0

  la $a0, s2
  li $v0, 4
  syscall

  li $v0, 5
  syscall
  move $t1, $v0

  sub $t2, $t0, $t1
  addi $t2, $t2, 1

  li $v0, 4
  la $a0, s3
  syscall

  move $a0, $t2
  li $v0,1
  syscall

  li $v0, 4
  la $a0, s4
  syscall
Bài 2:
.data
  s1: .asciiz   "Nhap ky tu: "
  s2: .asciiz   "\nMa ASCII tuong ung: "

.text

main:

  la $a0, s1
  li $v0, 4
  syscall

  li $v0, 12
  syscall

  move $t0, $v0

  la $a0, s2
  li $v0, 4
  syscall

  move $a0, $t0
  li $v0, 1
  syscall
Bài 3:
.data
  s1: .asciiz   "So a: "
  s2: .asciiz   "So b: "
  s3: .asciiz   "So c: "
  s4: .asciiz   "Sau khi sap thu tu: "

.text

main:
  la $a0, s1
  li $v0, 4
  syscall

  li $v0, 5
  syscall
  move $t0,$v0

  la $a0, s2
  li $v0, 4
  syscall
  li $v0, 5
  syscall
  move $t1,$v0

  la $a0, s3
  li $v0, 4
  syscall
  li $v0, 5
  syscall
  move $t2,$v0

  slt $s0,$t1,$t0
  bne $s0,$0,L1
  j Next1
  L1:
  move $t3,$t0
  move $t0,$t1
  move $t1,$t3
  Next1:

  slt $s0,$t2,$t0
  bne $s0,$0,L2
  j Next2
  L2:
  move $t3,$t0
  move $t0,$t2
  move $t2,$t3
  Next2:

  slt $s0,$t2,$t1
  bne $s0,$0,L3
  j Next3
  L3:
  move $t3,$t2
  move $t2,$t1
  move $t1,$t3
  Next3:

  la $a0, s4
  li $v0, 4
  syscall

  move $a0, $t0
  li $v0, 1
  syscall

  li $a0, 32
  li $v0, 11
  syscall

  move $a0, $t1
  li $v0, 1
  syscall

  li $a0, 32
  li $v0, 11
  syscall

  move $a0, $t2
  li $v0, 1
  syscall
Bài 4:
.data
  _a: .asciiz   "So a: "
  _b: .asciiz   "So b: "
  _c: .asciiz   "So c: "
  _d: .asciiz   "So d: "
  _max: .asciiz   "So lon nhat: "
  _min: .asciiz   " va So nho nhat: "
  _other: .asciiz   "\nHai so khong phai la lon nhat va nho nhat: "

.text

main:

  la $a0,_a
  li $v0,4
  syscall
  li $v0,5
  syscall
  move $t0,$v0

  la $a0,_b
  li $v0,4
  syscall
  li $v0,5
  syscall
  move $t1,$v0

  la $a0,_c
  li $v0,4
  syscall
  li $v0,5
  syscall
  move $t2,$v0

  la $a0,_d
  li $v0,4
  syscall
  li $v0,5
  syscall
  move $t3,$v0

  slt $s0,$t1,$t0
  beq $s0,$0, L1
  j L2
  L1:
  move $t4,$t0
  move $t0,$t1
  move $t1,$t4
  L2:

  slt $s0,$t2,$t0
  beq $s0,$0, L3
  j L4
  L3:
  move $t4,$t0
  move $t0,$t2
  move $t2,$t4
  L4:

  slt $s0,$t3,$t0
  beq $s0,$0, L5
  j L6
  L5:
  move $t4,$t0
  move $t0,$t3
  move $t3,$t4
  L6:

  slt $s0,$t2,$t1
  beq $s0,$0, L7
  j L8
  L7:
  move $t4,$t1
  move $t1,$t2
  move $t2,$t4
  L8:

  slt $s0,$t3,$t1
  beq $s0,$0, L9
  j L10
  L9:
  move $t4,$t1
  move $t1,$t3
  move $t3,$t4
  L10:

  slt $s0,$t3,$t2
  beq $s0,$0, L11
  j L12
  L11:
  move $t4,$t2
  move $t2,$t3
  move $t3,$t4
  L12:

  la $a0, _max
  li $v0, 4
  syscall

  move $a0, $t0
  li $v0, 1
  syscall

  la $a0, _min
  li $v0, 4
  syscall

  move $a0, $t3
  li $v0, 1
  syscall

  la $a0, _other
  li $v0, 4
  syscall

  move $a0, $t2
  li $v0,1
  syscall

  li $a0, 32
  li $v0,11
  syscall

  move $a0, $t1
  li $v0,1
  syscall

Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 nhận xét

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
© Download do an khoa luan tai lieu
Designed by BlogThietKe Cooperated with Duy Pham
Released under Creative Commons 3.0 CC BY-NC 3.0
Posts RSSComments RSS
Back to top