0% found this document useful (0 votes)
8 views14 pages

Control Structures - Assembly Language Program

The document discusses control structures in assembly language programming, focusing on sequences, branches, and loops. It provides examples of simple if statements, if-else statements, and loops, including while and for loops, along with their corresponding MIPS assembly code. Additionally, it includes exercises for translating high-level language statements into assembly instructions.

Uploaded by

rusiruchapana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views14 pages

Control Structures - Assembly Language Program

The document discusses control structures in assembly language programming, focusing on sequences, branches, and loops. It provides examples of simple if statements, if-else statements, and loops, including while and for loops, along with their corresponding MIPS assembly code. Additionally, it includes exercises for translating high-level language statements into assembly instructions.

Uploaded by

rusiruchapana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Control structures -

Assembly language program


CS3221 Assembly Language Programming
Control structures
• The structured programming paradigm is built on the
concept that all programs can be built using just 3 types
of program control structures. These structures are:
• Sequences that allow programs to execute statements in order
one after another.
• Branches that allow programs to jump to other points in a
program.
• Loops that allow a program to execute a fragment of code multiple
times.
Simple if statements
if (num > 0){ .text
print("Number is positive") # if (num > 0 )
lw $t0, num
}
sgt $t1, $t0, $zero # $t1 is the boolean (num > 0)
beqz $t1, end_if # note: the code block is
The simple if code fragment above is # entered if.if logical is
translated by: # true,skipped if false. {
a) calculating the boolean value to la $a0, PositiveNumber # print ("Number is positive")
control entering the if statement code li $v0, 4
block.
syscall # }
b) entering the code block if the end_if:
boolean is true, or branching around it if
it is false. li $v0, 10
syscall
.data
num: .word 5
PositiveNumber: .asciiz "Number is positive"
Simple if statement with complex logical
conditions
if ((x > 0 && ((x%2) == 0)) if ((x > 0) && ((x%2) == 0) && (x < 10))
# is x > 0 and even? # is 0 < x < 10 and even?

lw $t0, x lw $t0, x
sgt $t1, $t0, $zero sgt $t1, $t0, $zero
div $t0, 2 li $t5, 10
mfhi $t0 slt $t2, $t0, $t5
seqz $t2,$t0 rem $t3, $t0, 2
and $t1, $t1, $t2 and $t1, $t1, $t2
beqz $t1, end_if and $t1, $t1, $t3
beqz $t1, end_if
.text

if-else statements lw $t0, num


sgt $t1, $t0, $zero
beqz $t1, else
#if block
if (num > 0) {
la $a0, PositiveNumber
print("Number is positive") } li $v0, 4
syscall
else {
b end_if
print("Number is negative") #else block
} else:
la $a0, NegativeNumber
li $v0, 4
syscall
end_if:
li $v0, 10
syscall
.data
num: .word -5
PositiveNumber: .asciiz "Number is positive"
NegativeNumber: .asciiz "Number is negative"
if-elseif-else statements
if (grade > 100) || grade < 0){
print("Grade must be between 0..100")
} elseif (grade >= 90){
print("Grade is A")
} elseif (grade >= 80){
print("Grade is B")
} elseif (grade >= 70){
print("Grade is C")
} elseif (grade >= 60){
print("Grade is D")
} else{
print("Grade is F")
}
.text grade_C: .data
#if block sge $t1, $s0, 70 num: .word 70
lw $s0, num beqz $t1, grade_D InvalidInput: .asciiz "Number must be
slti $t1, $s0, 0 la $a0, OutputC > 0 and < 100"
sgt $t2, $s0, 100 j Print_End OutputA: .asciiz "Grade is A"
or $t1, $t1, $t2 grade_D: OutputB: .asciiz "Grade is B"
beqz $t1, grade_A sge $t1, $s0, 60 OutputC: .asciiz "Grade is C"
#invalid input block beqz $t1, else OutputD: .asciiz "Grade is D"
la $a0, InvalidInput la $a0, OutputD OutputF: .asciiz "Grade is F"
j Print_End j Print_End
grade_A:
else:
sge $t1, $s0, 90
la $a0, OutputF
beqz $t1, grade_B
j Print_End
la $a0, OutputA
j Print_End Print_End:
grade_B: li $vo,4
sge $t1, $s0, 80 syscall
beqz $t1, grade_C li $vo,10
la $a0, OutputB syscall
j Print_End
Loops
• while… - a loop with a guard statement that controls whether or not
the loop is executed. The major use of sentinel control loops is to
process input until some condition (a sentinel value) is met.
• for… loop…
# code block
la $a0, output
While…. loop li $v0, 4
syscall
int i = prompt("Enter an integer, or -1 to
move $a0, $s0
exit")
li $v0, 1
while (i != -1)
syscall
{
print("You entered " + i);
la $a0, prompt
i = prompt("Enter an integer, or -1 to exit");
li $v0, 4
}
syscall
li $v0, 5
.text
syscall
#set sentinel value (prompt the user for input).
move $s0, $v0
la $a0, prompt
b start_loop
li $v0, 4
end_loop:
syscall
li $v0,10
li $v0, 5
syscall
syscall
.data
move $s0, $v0
prompt: .asciiz "\nEnter an integer, -1 to
start_loop:
stop: "
sne $t1, $s0, -1
output: .asciiz "\nYou entered: "
beqz $t1, end_loop
Counter control loop – for… loop…
• A counter controlled loop is a loop which is intended to be executed
some number of times.
• Normally this is associated with a for loop in most HLL.
• The general format is to
• specify a starting value for a counter,
• the ending condition (normally when the counter reaches a predetermined
value) , and
• the increment operation on the counter.
for…. loop
n = prompt("enter the value to calculate the sum up to: ") ;
total = 0; # Initial the total variable for sum
for (i = 0; i < n; i++)
{
total = total + i ; // total+=i;
}
print("Total = " + total);
.text move $a1, $s2
la $a0, prompt li $v0, 1
li $v0, 4 syscall
syscall
li $v0, 5 exit:
syscall li $v0,10
move $s1, $v0 syscall
li $s0, 0 .data
li $s2, 0 # Initialize the total
prompt: .asciiz "enter the value to calculate
start_loop: the sum up to: "
sle $t1, $s0, $s1
output: .asciiz "The final result is: "
beqz $t1, end_loop
# code block
add $s2, $s2, $s0 # total+=i
addi $s0, $s0, 1 # i++
b start_loop
end_loop:
la $a0, output
li $v0, 4
syscall
Exercise: Translate the following HLL
statements into assembly instructions.
 if(i== j)  int sum= 0;
f = g + h; int i;
f = f –i; for(i = 0; i != 10; i = i+1) {
 if (i== j) sum= sum+ i;
f = g + h; }
else  int n = 10;
f = f –i;
int f1 = 1, f2 = 1;
 int pow= 1;
while (n != 0) {
int x = 0; f1 = f1 + f2;
while(pow!= 128) { f2 = f1 – f2;
pow= pow* 2; n = n – 1;
x = x + 1; }
} # result is f1
Exercise
1. Write a MIPS program that prompts a positive integer from a user
and prints whether it is even or odd.
2. Translate this HLL code segment to MIPS. Write a complete MIPS
program
if (i!=j)
h=i+j;
else
h=i-j;

You might also like