Professional Documents
Culture Documents
Báo Cáo Tuần 3 - Nguyễn Quốc Giáp - 20225832
Báo Cáo Tuần 3 - Nguyễn Quốc Giáp - 20225832
Báo Cáo Tuần 3 - Nguyễn Quốc Giáp - 20225832
data
Báo Cáo Tuần 3
i: .word 5
B1 a, "i <= j"
j: .word -10
Chương trình:
result: .asciiz "i + j is greater than or equal to 0"
.data
else_result: .asciiz "i + j is less than 0"
i: .word 5
.text
j: .word 10
main:`
result: .asciiz "i is less than or equal to j"
# Load i and j into registers
else_result: .asciiz "i is greater than j"
lw $t0, i
.text
lw $t1, j
main:
# Add i and j
# Load i and j into registers
add $t2, $t0, $t1
lw $t0, i
# Compare i+j and 0
lw $t1, j
bge $t2, $zero, if_true
# Compare i and j
# Else part
ble $t0, $t1, if_true
# Print else result
# Else part
la $a0, else_result
# Print else result
li $v0, 4
la $a0, else_result
syscall
li $v0, 4
j endif
syscall
j endif
if_true:
if_true:
# Then part
# Then part
# Print result
# Print result
la $a0, result
la $a0, result
li $v0, 4
li $v0, 4
syscall
syscall
endif:
endif:
# End of program
# End of program
li $v0, 10
li $v0, 10
syscall
syscall
c, .data
i: .word 5
j: .word 10 else_result: .asciiz "At least one condition is false"
lw $t3, n li $v0, 4
# Compare i+j and m+n add $t2, $t0, $t1 # Compare i+j and 0
li $v0, 4 li $v0, 4
syscall syscall
j endif j endif
if_true: if_true:
li $v0, 4 li $v0, 4
syscall syscall
endif: endif:
li $v0, 10
syscall B2
a, .data
c, .data
A: .word 1, 2, 3, 4, 5
b, .data n: .word 5
A: .word 20, 30, 40, 10, 5 count_even: .word 0
n: .word 5 i: .word 0
sum: .word 0
i: .word 0 .text
main: la $t0, A
la $t0, A lw $t1, n
lw $t1, n li $t2, 0
li $t2, 0 li $t3, 0
li $t3, 0 loop:
lw $t4, n
loop: bge $t3, $t4, end_loop
lw $t4, n lw $t5, 0($t0)
bge $t3, $t4, end_loop add $t2, $t2, $t5
bge $t2, 100, end_loop
lw $t5, 0($t0) lw $t6, 0($t0)
div $t5, $zero, 2 bge $t6, 50, satisfy_condition
mfhi $t6 addi $t3, $t3, 1
addi $t0, $t0, 4
bne $t6, $zero, skip_increment j loop
addi $t2, $t2, 1 satisfy_condition:
li $t2, 100
skip_increment: j end_loop
addi $t3, $t3, 1 end_loop:
addi $t0, $t0, 4 li $v0, 10
j loop syscall
end_loop:
sw $t2, count_even
li $v0, 10
syscall B3,
d, .data Các thanh ghi được ảnh hưởng bởi các lệnh
jump/branch là thanh ghi PC - Program Counter và
A: .word 20, 30, 40, 60, 70
trong một số trường hợp, thanh ghi trạng thái của trạng
n: .word 5 thái máy (PSR - Processor Status Register).
sum: .word 0
Trong các chương trình mẫu 1-3:
i: .word 0
Ví dụ, trong chương trình mẫu 2, nếu điều kiện của lệnh
bne là đúng (i != n), chương trình sẽ nhảy tới nhãn
"loop"; nếu điều kiện sai, chương trình tiếp tục thực hiện
lệnh tiếp theo.
B4