0% found this document useful (0 votes)
42 views4 pages

Lab 6 - Control Instructions and Loop

The document discusses control instructions and loops in assembly language programming. It describes two types of control instructions: unconditional instructions like J and B that jump or branch unconditionally, and conditional instructions like BEQ and BGT that compare registers and branch if conditions are met. It then gives examples of writing a program to check if a number is even or odd using these conditional branch instructions. The document also discusses implementing loops in assembly by using jump instructions to repeat a block of code, and gives examples like calculating a sum or finding the maximum of a range.

Uploaded by

Kappa Boosted
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)
42 views4 pages

Lab 6 - Control Instructions and Loop

The document discusses control instructions and loops in assembly language programming. It describes two types of control instructions: unconditional instructions like J and B that jump or branch unconditionally, and conditional instructions like BEQ and BGT that compare registers and branch if conditions are met. It then gives examples of writing a program to check if a number is even or odd using these conditional branch instructions. The document also discusses implementing loops in assembly by using jump instructions to repeat a block of code, and gives examples like calculating a sum or finding the maximum of a range.

Uploaded by

Kappa Boosted
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/ 4

FACULTY OF INFORMATION TECHNOLOGY

DEPARTMENT OF COMPUTER NETWORK AND DATA COMMUNICATION


Subject: Computer organization

LAB 6
Control Instructions and Loop
1. Control Instructions
There are 2 types of control instructions:
a) Unconditional Control Instructions
Syntax:
j <label> # Unconditionally jump to the specified <label>
b <label> # Unconditionally branch to the specified <label>

The “b” (branch) may be used instead of the “j” (jump). Both are encoded as the same instruction
(an unconditional jump).

b) conditional Control Instructions


- High-level programming language, we have comparison expressions: =, >, >=, <, <=, !=
- Assembly programming language, we do not have comparison expressions as above, but
we must use the following commands:
Syntax:
- beq <Rsrc>, <Src>, <label> # if Rsrc == Src branch to <label>.
# (beq: Branch equal)
- bgt <Rsrc>, <Src>, <label> # if Rsrc > Src branch to <label>.
# (bgt: branch greater than)
- bge <Rsrc>, <Src>, <label> # if Rsrc >= Src branch to <label>.
# (bge: branch greater than or equal)
- blt <Rsrc>, <Src>, <label> # if Rsrc < Src branch to <label>.
# (blt: branch less than)
- ble <Rsrc>, <Src>, <label> # if Rsrc <= Src branch to <label>.
# (ble: branch less than or equal)
- bne <Rsrc>, <Src>, <label> # if Rsrc != Src branch to <label>.
# (bne: branch not equal)

Example 1: Writing a program to input an integer number, check whether the entered number is
even number or odd number

.data
msg: .asciiz "Enter an integer number:"
msg_even: .asciiz "\nThe number you entered is even"
msg_odd: .asciiz "\nThe number you entered is odd"
.text
.globl main
main:
# Print a string
li $v0,4
Truong Dinh Tu 1
FACULTY OF INFORMATION TECHNOLOGY
DEPARTMENT OF COMPUTER NETWORK AND DATA COMMUNICATION
Subject: Computer organization

la $a0, msg
syscall

# Read an integer
li $v0,5
syscall
move $t0, $v0 # move the entered value to $t0

# Compare even or odd numbers


# divide by 2 get remainder:
# if remainder < 1 is even, otherwise odd
li $t1,1
rem $t2,t0,2 # $t2= $t0%2
blt $t2,$t1,even # if $t2 < 1 then branch to even label
odd: # This is odd label
# Print a msg_odd string
li $v0,4
la $a0, msg_odd
syscall
j Exit

even: # This is even label


# Print a msg_even string
li $v0,4
la $a0, msg_even
syscall

Exit: # This is Exit label


# Exit
li $v0,10
syscall

2. LOOP STRUCTURE

- High-level programming language, we have: FOR (i=0;i<=n;i++); or do ...while; or while()


Example: Program to calculate the sum of numbers from 1 to n
C language will write as follows:
Sum = 0;
For (i=1; i<n; i++)
Sum = sum + i;

Printf “The Sum result here”;

Truong Dinh Tu 2
FACULTY OF INFORMATION TECHNOLOGY
DEPARTMENT OF COMPUTER NETWORK AND DATA COMMUNICATION
Subject: Computer organization

- Assembly programming language will write as follows:


li $t0, 0 # $t0 = 0 (Sum =0)
li $t1, 1 # $t1 = 1 (i=1)
lw $t2, n # $t2 = n
LoopSum: #LoopSum label
bgt $t1,$t2, ExitLoop # if i>n jump to ExitLoop label
add $t0, $t0, $t1 # sum = sum + i
add $t1,$t1,1 # i = i+1
j LoopSum # jump to LoopSum label

ExitLoop: # ExitLoop label


# print “The result here”

Practical exercise:
1/ Write a program to input an integer N, calculate the sum of numbers from 1 to N.
Output the result to the screen.

2/ Write a program to input an integer N, calculate the sum of squares of numbers from 1
to N. Output the result to the screen.

3/ Write a program to input an integer N, calculate the sum of even numbers from 1 to N.
Output the result to the screen.

4/ Write a program to input an integer N, check if the number is divisible by 3 or not?


Output the result to the screen.

5/ Write a program to input an integer, check if the number is positive, negative or Zero.
Output the result to the screen.

6/ Write a program to input 2 integers a and b. Check that a > b or b > a or 2 numbers are
equal. Output the message to the screen.

7/ Write a program to input a integer N, and if the user enters a negative number or zero,
the program must ask for input again until a positive integer value is entered.

8/ Write a program to input two integers A and B. Calculate the sum of all positive
integers whose values are in the range [A, B].

Truong Dinh Tu 3
FACULTY OF INFORMATION TECHNOLOGY
DEPARTMENT OF COMPUTER NETWORK AND DATA COMMUNICATION
Subject: Computer organization

9/ Write a program to input two integers A and B. Find the maximum number and output
the result to the screen.

10/ Write a program to input two integers x and y. Calculate the power xy

Truong Dinh Tu 4

You might also like