Lab 3
Conditional Execution
lOMoARcPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution
P.1.
Objectives
After completing this experiment, you will be able to:
- write some simple MIPS programs with conditional execution using branching and
jumping instructions.
For each of the programming exercises, demonstrate your program to the instructor, format
and comment your program appropriately.
Activities
- Sample codes, P1-P4.
- P5 will be shown in class Materials Needed
-Read Lab2notes.pdf for MIPS
Procedures
If-then-else style conditional statement in MIPS assembly language
Line
Sample Code
Comments
1.
.data
2
msg: .asciiz "The value of f is"
3
ff: .word 6
4
gg: .word 12
5
hh: .word -21
6
ii: .word 4
7
jj: .word 9
8
9
.text
10
11
.globl main
12
main: lw $s0, ff
13
lw $s1, gg
14
lw $s2, hh
15
lw $s3, ii
16
lw $s4, jj
lOMoARcPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
L1:
L2:
bne $s3, $s4, L1
add $s0, $s1, $s2
b L2
sub $s0, $s0, $s3 sub
$s0, $s0, $s4
la $a0,msg
li $v0,4
syscall
move $a0, $s0
li $v0, 1
syscall
li $v0, 10
syscall
lOMoARcPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution
Write a corresponding to a simple if-then-else style conditional statement under C-
language (from line 18-23):
if (s3 != s4) {
s0 = s1 + s2;
} else {
s0 = s0 - s3;
}
s0 = s0 - s4;
Rewrite the above program, but the values of ff through jj are read
from the console
msg: .asciiz "The value of f is"
# Read the values of ff, gg, hh, ii, and jj from the console
move $s0, $v0
lOMoARcPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution
syscall
move $s1, $v0
li $v0, 5
syscall
move $s2, $v0
li $v0, 5
syscall
move $s3, $v0
li $v0, 5
syscall
move $s4, $v0
bne $s3, $s4, L1
add $s0, $s1, $s2 b
L2
L1:
sub $s0, $s0, $s3
L2:
sub $s0, $s0, $s4
la $a0,msg
li $v0,4
syscall
move $a0, $s0
li $v0, 1
syscall
li $v0, 10
syscall
c) Rewrite Program 1, but the values of ff through jj are read from word length integers in
successive memory locations.
.data
msg: .asciiz "The value of f is"
lOMoARcPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution
lOMoARcPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution
values: .word 6, 12, -21, 4, 9
.text
.globl main
main:
la $t0, values
lw $s0, 0($t0)
lw $s1, 4($t0)
lw $s2, 8($t0)
lw $s3, 12($t0)
lw $s4, 16($t0)
bne $s3, $s4, L1
add $s0, $s1, $s2
b L2
L1:
sub $s0, $s0, $s3
L2:
sub $s0, $s0, $s4
la $a0,msg
li $v0,4
syscall
move $a0, $s0
li $v0, 1
syscall
li $v0, 10
syscall
d)Rewrite the program 1 by using effective address
.data
msg: .asciiz "The value of f
is" ff: .word 6 gg: .word 12
hh: .word -21 ii: .word 4
lOMoARcPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution
lOMoARcPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution
jj: .word 9
.text
.globl main
main:
# Load the values of ff, gg, hh, ii, and jj into
registers. lw $s0, ff lw $s1, gg lw $s2, hh lw
$s3, ii lw $s4, jj
# Compute the effective address of the branch instruction.
addi $t0, $s3, 4
# Branch to L1 if s3 != s4, or to L2 otherwise.
bne $s3, $s4, L1
# Compute the effective address of the addition instruction.
addi $t1, $s1, 4
# Add s1 and s2, and store the result in s0.
add $s0, $s1, $s2
# Branch to L2 unconditionally.
j L2
L1:
# Compute the effective address of the subtraction
instruction. addi $t2, $s0, 4
# Subtract s3 from s0, and store the result in s0.
sub $s0, $s0, $s3
L2:
# Compute the effective address of the subtraction
instruction. addi $t3, $s0, 4
# Subtract s4 from s0, and store the result in s0.
sub $s0, $s0, $s4
# Load the address of the message string into a0.
la $a0,msg
# Print the message string.
li $v0,4
syscall
# Move the value of s0 to a0.
lOMoARcPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution
move $a0, $s0
# Print the value of s0.
li $v0, 1
syscall
# Exit the program.
li $v0, 10
syscall
P.2. Sum Function
Write a MIPS program to calculate the sum of all input numbers as follow:
Use loop to accept an unspecified number of integer inputs. When a value of “0” is
entered, print out the calculation and exit the program. Note: 0 is not counted in
calculation.
If the number is even, SQUARE that number before adding it to the Sum.
If the number is odd, CUBE that number before adding it to the Sum.
lOMoARcPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution
.data
Enter an integer: 3
Enter an integer: 2
Enter an integer: 4
Enter an integer: 5
Enter an integer: 0
The Sum is: 154
lOMoARcPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution
.text
Save your program as yourfullname_sum.s and go through your code with different
values.
sum: .word 0
prompt: .asciiz "Enter an integer: "
msg: .asciiz "The sum: "
name: .asciiz "TruongBuuDuy\nITITIU21188\n"
zero: .word 0
main:
# Print the
name li $v0, 4
la $a0, name
syscall
input_loop:
# Print the
prompt li $v0, 4
la $a0, prompt
syscall
# Read the input
number li $v0, 5
syscall
# Check if the input number is 0
move $t0, $v0
lOMoARcPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution
lw $t1, zero
beq $t0, $t1, exit
# Determine if the input number is even or
odd andi $t1, $t0, 1 beqz $t1, even
odd:
# Cube the odd
number mul $t2, $t0,
$t0 mul $t2, $t2, $t0
j update_sum
even:
# Square the even number
mul $t2, $t0, $t0
update_sum:
# Add the input number to the sum
lw $t1, sum add $t1, $t1, $t2 sw
$t1, sum
# Jump back to the input loop
j input_loop
exit:
# Print the
sum li $v0, 4 la
$a0, msg syscall
# Print out the calculation
li $v0, 1
lw $a0, sum
syscall
# Exit the program
li $v0, 10
syscall
P.3 it will be shown in class
lOMoARcPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution
.data result: .word 0 counter: .word 0 prompt1:
.asciiz "Enter the rst number: " prompt2: .asciiz
"Enter the second number: " newline: .asciiz "\n"
name: .asciiz "TruongBuuDuy\nITITIU21188\n" op:
.asciiz "Output: "
.text
.globl main main:
li $v0, 4
la $a0, name
syscall
# prompt user for rst number
li $v0, 4 la $a0, prompt1
syscall
# read rst number
li $v0, 5 syscall
lOMoARcPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution
lOMoARcPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution
# save rst number to register $t0
move $t0, $v0
# prompt user for second number
li $v0, 4 la $a0, prompt2 syscall
# read second number
li $v0, 5
syscall
# save second number to register $t1
move $t1, $v0
# inialize result to 0
li $t2, 0
# loop counter
move $t3, $t0
loop:
# check if loop counter is 0
beq $t3, $zero, output
# add addend to result
add $t2, $t2, $t1
lOMoARcPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution
Nov., 2023 1
0
# decrement counter
sub $t3, $t3, 1
# repeat loop
j loop
output:
# store result in memory
sw $t2, result
li $v0, 4
la $a0, op
syscall
# print result to console
li $v0, 1 lw $a0, result
syscall
# print newline character to console
li $v0, 4 la $a0, newline syscall
Nov., 2023 1
1

Preview text:

Lab 3 Conditional Execution lOMoAR cPSD| 59078336 Objectives
After completing this experiment, you will be able to:
COMPUTER SCIENCE & ENGINEERING Conditional execution
- write some simple MIPS programs with conditional execution using branching and jumping instructions.
For each of the programming exercises, demonstrate your program to the instructor, format
and comment your program appropriately. Activities - Sample codes, P1-P4.
- P5 will be shown in class Materials Needed
-Read Lab2notes.pdf for MIPS Procedures
If-then-else style conditional statement in MIPS assembly language Line Sample Code Comments 1. .data 2
msg: .asciiz "The value of f is" 3 ff: .word 6 4 gg: .word 12 5 hh: .word -21 6 ii: .word 4 7 jj: .word 9 8 9 .text 10 .globl main 11 12 main: lw $s0, ff 13 lw $s1, gg 14 lw $s2, hh 15 lw $s3, ii 16 lw $s4, jj P.1. lOMoAR cPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution 17 L1: 18 L2: 19 bne $s3, $s4, L1 20 add $s0, $s1, $s2 21 b L2 22 a) sub $s0, $s0, $s3 sub 23 24 $s0, $s0, $s4 25 26 la $a0,msg li $v0,4 27 syscall 28 29 move $a0, $s0 30 li $v0, 1 31 syscall 32 33 li $v0, 10 34 syscall lOMoAR cPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution
b) Write a corresponding to a simple if-then-else style conditional statement under C-
language (from line 18-23): if (s3 != s4) { s0 = s1 + s2; } else { s0 = s0 - s3; .data } s0 = s0 - s4; .text .globl main main: li $v0, 5 syscall
Rewrite the above program, but the values of ff through jj are read from the console li $v0,
msg: .asciiz "The value of f is" 5
# Read the values of ff, gg, hh, ii, and jj from the console move $s0, $v0 lOMoAR cPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution syscall move $s1, $v0 li $v0, 5 syscall move $s2, $v0 li $v0, 5 syscall move $s3, $v0 li $v0, 5 syscall move $s4, $v0 bne $s3, $s4, L1 add $s0, $s1, $s2 b L2 L1: sub $s0, $s0, $s3 L2: sub $s0, $s0, $s4 la $a0,msg li $v0,4 syscall move $a0, $s0 li $v0, 1 syscall li $v0, 10 syscall
c) Rewrite Program 1, but the values of ff through jj are read from word length integers in successive memory locations. .data
msg: .asciiz "The value of f is" lOMoAR cPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution lOMoAR cPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution
values: .word 6, 12, -21, 4, 9 .text .globl main main: la $t0, values lw $s0, 0($t0) lw $s1, 4($t0) lw $s2, 8($t0) lw $s3, 12($t0) lw $s4, 16($t0) bne $s3, $s4, L1 add $s0, $s1, $s2 b L2 L1: sub $s0, $s0, $s3 L2: sub $s0, $s0, $s4 la $a0,msg li $v0,4 syscall move $a0, $s0 li $v0, 1 syscall li $v0, 10 syscall
d)Rewrite the program 1 by using effective address .data msg: .asciiz "The value of f is" ff: .word 6 gg: .word 12 hh: .word -21 ii: .word 4 lOMoAR cPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution lOMoAR cPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution jj: .word 9 .text .globl main main:
# Load the values of ff, gg, hh, ii, and jj into
registers. lw $s0, ff lw $s1, gg lw $s2, hh lw $s3, ii lw $s4, jj
# Compute the effective address of the branch instruction. addi $t0, $s3, 4
# Branch to L1 if s3 != s4, or to L2 otherwise. bne $s3, $s4, L1
# Compute the effective address of the addition instruction. addi $t1, $s1, 4
# Add s1 and s2, and store the result in s0. add $s0, $s1, $s2
# Branch to L2 unconditionally. j L2 L1:
# Compute the effective address of the subtraction instruction. addi $t2, $s0, 4
# Subtract s3 from s0, and store the result in s0. sub $s0, $s0, $s3 L2:
# Compute the effective address of the subtraction instruction. addi $t3, $s0, 4
# Subtract s4 from s0, and store the result in s0. sub $s0, $s0, $s4
# Load the address of the message string into a0. la $a0,msg # Print the message string. li $v0,4 syscall
# Move the value of s0 to a0. lOMoAR cPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution move $a0, $s0 # Print the value of s0. li $v0, 1 syscall # Exit the program. li $v0, 10 syscall P.2. Sum Function
Write a MIPS program to calculate the sum of all input numbers as follow:
• Use loop to accept an unspecified number of integer inputs. When a value of “0” is
entered, print out the calculation and exit the program. Note: 0 is not counted in calculation.
• If the number is even, SQUARE that number before adding it to the Sum.
• If the number is odd, CUBE that number before adding it to the Sum. lOMoAR cPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution .data Enter an integer: 3 Enter an integer: 2 Enter an integer: 4 Enter an integer: 5 Enter an integer: 0 The Sum is: 154 lOMoAR cPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution
.text Save your program as yourfullname_sum.s and go through your code with different values. sum: .word 0
prompt: .asciiz "Enter an integer: " msg: .asciiz "The sum: "
name: .asciiz "TruongBuuDuy\nITITIU21188\n" zero: .word 0 main: # Print the name li $v0, 4 la $a0, name syscall input_loop: # Print the prompt li $v0, 4 la $a0, prompt syscall # Read the input number li $v0, 5 syscall
# Check if the input number is 0 move $t0, $v0 lOMoAR cPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution lw $t1, zero beq $t0, $t1, exit
# Determine if the input number is even or
odd andi $t1, $t0, 1 beqz $t1, even odd: # Cube the odd number mul $t2, $t0, $t0 mul $t2, $t2, $t0 j update_sum even: # Square the even number mul $t2, $t0, $t0 update_sum:
# Add the input number to the sum
lw $t1, sum add $t1, $t1, $t2 sw $t1, sum
# Jump back to the input loop j input_loop exit: # Print the sum li $v0, 4 la $a0, msg syscall # Print out the calculation li $v0, 1 lw $a0, sum syscall # Exit the program li $v0, 10 syscall
P.3 it will be shown in class lOMoAR cPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution
.data result: .word 0 counter: .word 0 prompt1:
.asciiz "Enter the first number: " prompt2: .asciiz
"Enter the second number: " newline: .asciiz "\n"
name: .asciiz "TruongBuuDuy\nITITIU21188\n" op: .asciiz "Output: " .text .globl main main: li $v0, 4 la $a0, name syscall
# prompt user for first number li $v0, 4 la $a0, prompt1 syscall # read first number li $v0, 5 syscall lOMoAR cPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution lOMoAR cPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution
# save first number to register $t0 move $t0, $v0
# prompt user for second number
li $v0, 4 la $a0, prompt2 syscall # read second number li $v0, 5 syscall
# save second number to register $t1 move $t1, $v0 # initialize result to 0 li $t2, 0 # loop counter move $t3, $t0 loop: # check if loop counter is 0 beq $t3, $zero, output # add addend to result add $t2, $t2, $t1 lOMoAR cPSD| 59078336
COMPUTER SCIENCE & ENGINEERING Conditional execution Nov., 2023 1 0 # decrement counter sub $t3, $t3, 1 # repeat loop j loop output: # store result in memory sw $t2, result li $v0, 4 la $a0, op syscall # print result to console li $v0, 1 lw $a0, result syscall
# print newline character to console
li $v0, 4 la $a0, newline syscall Nov., 2023 1 1