Lab 6 - Control Instructions and Loop
Lab 6 - Control Instructions and Loop
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).
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
2. LOOP STRUCTURE
Truong Dinh Tu 2
FACULTY OF INFORMATION TECHNOLOGY
DEPARTMENT OF COMPUTER NETWORK AND DATA COMMUNICATION
Subject: Computer organization
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.
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