Báo Cáo Tuần 3 - Nguyễn Quốc Giáp - 20225832

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

b, .

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"

m: .word 3 .text main:

n: .word 7 # Load i and j into registers

result: .asciiz "i + j is greater than m + n" lw $t0, i

else_result: .asciiz "i + j is less than or equal to m + n" lw $t1, j

.text main: # Compare i and j

# Load i, j, m, and n into registers ble $t0, $t1, check_second_condition

lw $t0, i # Else part

lw $t1, j # Print else result

lw $t2, m la $a0, else_result

lw $t3, n li $v0, 4

# Add i and j syscall

add $t4, $t0, $t1 j endif

# Add m and n check_second_condition:

add $t5, $t2, $t3 # Add i and j

# Compare i+j and m+n add $t2, $t0, $t1 # Compare i+j and 0

bgt $t4, $t5, if_true bge $t2, $zero, if_true

# Else part # Else part

# Print else result # Print else result

la $a0, else_result la $a0, else_result

li $v0, 4 li $v0, 4

syscall syscall

j endif 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 syscall

li $v0, 10

syscall B2
a, .data

d, .data A: .word 1, 2, 3, 4, 5 # Mảng A mẫu


i: .word 5 n: .word 5 # Số phần tử trong mảng A
j: .word 10 sum: .word 0 # Biến để lưu tổng
result: .asciiz "Both conditions are true"
i: .word 0 # Biến đếm vòng lặp
.text
.text main:
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:
loop: lw $t4, n
lw $t4, n bge $t2, 100, end
bgt $t3, $t4, end
lw $t5, 0($t0)
lw $t5, 0($t0) add $t2, $t2, $t5
add $t2, $t2, $t5
addi $t3, $t3, 1
addi $t3, $t3, 1 addi $t0, $t0, 4
addi $t0, $t0, 4
j loop
j loop
end:
end: sw $t2, sum
sw $t2, sum li $v0, 10
li $v0, 10 syscall
syscall

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

- Lệnh j : trong chương trình 1, nếu lệnh j được thực


.text hiện từ "else" đến "endif", địa chỉ mục tiêu sẽ được tính
main: toán bằng cách thêm một offset đến "endif" từ vị trí của
lệnh j.
- Lệnh bne và beq :Cả hai lệnh này đều sử dụng một
offset để tính địa chỉ mục tiêu dựa trên kết quả của điều
kiện so sánh. Nếu điều kiện là đúng, thì offset được thêm
vào địa chỉ của lệnh hiện tại để nhảy đến đích; nếu điều
kiện sai, thì không có offset được thêm vào và chương
trình nhảy sang lệnh tiếp theo.

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

You might also like