You are on page 1of 12

Royal Commission for Jubail and

Yanbu Yanbu University College


Department of Computer Science and Engineering

CS203 – COMPUTER ORGANIZATION AND ASSEMBLY (SEM:


XXX)
LAB 3: Integer, bit operations, and arrays

Total Marks: 10

Objectives:
• Learn to use MIPS bit manipulation instructions (logical and shift), integer multiplication
and division instructions in assembly language programs.
• Learn to implement loops and conditional expressions in assembly language programs.
• Learn how to implement array as an abstract data structure in MIPS assembly language

Contents:
1. Introduction
2. Bitwise logical instructions
3. Shift instructions
4. Conditional and unconditional branch instructions
5. Arrays

1. INTRODUCTION
Bit- manipulation instructions are essential to an architecture's ISA. At minimum, a NAND
ins t ruction is required, since all other logical functions can be derived from NAND
operation. These logical operations provides the programmer with more power to manipulate
data, and constitute the basis for building conditional expressions.
With conditional processing, one can alter the normal (sequential) program execution flow. To
accomplish this, computers employ the concept of branching, which resembles a tree structure in its
growth. The main trunk of the tree represent the normal flow of an executing program, while the
branches of the tree represent alternative paths for the tree to grow, hence resembling the alternative
cases a problem might address.
Not like high level languages, Assembly language has no notion of an array at all. Arrays like
variables are treated as a block of memory that could be allocated with a single directive, where
the first element is given a label.
The array as the most important and most general data structure has the following properties:
1. All elements must be of the same size.
2. The number of elements is fixed.
3. A label (address) is points to the first element of the array.
2. BITWISE LOGICAL INSTRUCTIONS

Instructions Description
and rd, rs, rt rd = rs & rt
andi rt, rs, immediate rt = rs & immediate
or rd, rs, rt rd = rs | rt
ori rt, rs, immediate rd = rs | immediate
nor rd, rs, rt rd = ! ( rs | rt )
xor rd, rs, rt To do a bitwise logical Exclusive OR.
xori rt, rs, immediate

The main usage of bitwise logical instructions are: to set, to clear, to invert, and to isolate some
selected bits in the destination operand. To do this, a source bit pattern known as a mask is
constructed. The individual mask bits are chosen based on the following properties of AND, OR, and
XOR with Z representing a bit (either 0 or 1):

AND OR XOR
Z AND 0 = 0 Z OR 0 = Z Z XOR 0 = Z
Z AND 1 = Z Z OR 1 = 1 Z XOR 1 = ~Z

The AND instruction can be used to CLEAR specific destination bits while preserving the others. A
zero mask bit clears the corresponding destination bit; a one mask bit preserves the corresponding
destination bit.
The OR instruction can be used to SET specific destination bits while preserving the others. A one
mask bit sets the corresponding destination bit; a zero mask bit preserves the corresponding
destination bit.
The XOR instruction can be used to INVERT specific destination bits while preserving the others. A
one mask bit inverts the corresponding destination bit; a zero mask bit preserves the corresponding
destination bit.

Example 1:
The following code fragment will clear bit 2, 4, 6, and 7 of the register $t2:

addi $t0, $zero, 0xFF2B


andi $t2, $t2, $t0

Example 2:
The following code fragment will set bit 7, 6, 5, 3 and 0 of the register $t2 using OR operation:

ori $t2, $t2, 0x00E9


Example 3:
The following code fragment will toggle bit 7, 2, and 0 of the register $t2 using XOR operation:

xori $t2, $t2, 0x85


3. SHIFT INSTRUCTIONS
Instruction Description
sll rd, rs, sa rd = rs << sa (Shift Left Logical)
sllv rd, rt, rs rd = rt << rs (To left-shift a word by a variable number of bits)
sra rd, rs, sa The contents of the low-order 32-bit word of rs are shifted right,
duplicating the sign-bit (bit 31) in the emptied bits; the word
result is placed in rd. The bit-shift amount is specified by sa.

srav rd, rt, rs rd = rt >> rs (Arithmetic)


srl rd, rs, sa rd = rs >> sa (Shift Right Logical)
srlv rd, rt, rs rd = rt >> rs

Logical Shift instructions are useful mainly in these situations:


1. To manipulate bits;
2. To multiply and divide unsigned numbers by a power of 2.

Example 1
The following code fragment will multiply the content of register $t0 with 80:

sll $t1, $t0, 4 # *16 sll


$t0, $t0, 6 # *64 addu $t0,
$t0, $t1

Example 2
Explain what the following code fragment will do?

addu $t1,$t0, $zero


sll $t0,$t0,4
srl $t1,$t1,4
or $t1,$t1,$t0

3.1 Assignment:
1. Write a MIPS assembly language program that converts all lowercase letters of a string to
uppercase ones.
.datastring: .asciiz "myname"length: .word 6bitmask: .byte 0xdf#
method using bit manipulation .text.globl mainmain: lw $s1,
bitmasklw $s0, lengthla $t1, stringloop: beq
$s0,$zero,exitloop#test if counter=0lb, $t0, 0($t1)and $t0,$t0,$s1
# change case to uppercasesb $t0, 0($t1)addiu $t1,$t1,1#increment
string addressaddiu $s0,$s0,-1#decrement counterj loopexitloop:li
$v0, 4#output the stringla $a0, stringsyscallli $v0, 10#exitsyscall
2. Write a MIPS assembly language program that displays the contents of register $t0 as
binary string.
.datastring: .asciiz "myname"bitmask: .byte 0xdf.text.globl
mainmain:la $t1, string lb $s1,bitmaskloop:lb $t0, 0($t1)
beq $t0,$0, exitloop #Convert lowercase to Uppercase
#addiu $t0, $t0,-32 and $t0,$t0,$s1 #use bitmask to change to
uppercase sb $t0, 0($t1)addiu $t1,$t1,1 #increment string
addressj loopexitloop:li $v0, 4#output the stringla $a0,
stringsyscallli $v0, 10#exitsyscall
4. CONDITIONAL AND UNCONDITIONAL BRANCH INSTRUCTIONS
Instruction Description
bgez rs, L if ( rs >= 0 ) go to L;
bgtz rs, L if ( rs > 0 ) go to L;
blez rs, L if ( rs <= 0 ) go to L;
bltz rs, L if ( rs < 0 ) go to L;
bne rs, rt, L if (rs != rt) go to L;
beq rs, rt, L if (rs == rt) go to L;
slt rd, rs, rt if ( rs < rt ) rd=1; else rd=0; rs and rt are signed integers.
sltu rd, rs, rt Same as slt except rs and rt are unsigned integers.
slti rt, rs, immediate if ( rs < signed immediate ) rd=1; else rd=0;
sltiu rt, rs, immediate if ( rs < unsigned immediate ) rd=1; else rd=0;
j L go to L
Example:
Following is a MIPS assembly language program that calculates the sum of all positive integers less
than or equal to N and displays the result on the monitor. Assuming that N is stored in the register
$t0.
Algorithm Assembly Language
$t0  N; li $t0, N
$t1  1; li $t1, 1
$a0  0; add $a0, $zero, $zero
loop: if ($t1 > $t0) go to print; loop: sltu $t2, $t0, $t1
$a0  $a0 + $t1; bgtz $t2, print
$t1  $t1 + 1; add $a0, $a0, $t1
go to loop; u $t1, $t1,
print: display $a0; addi 1 loop
exit; print: j

4.1 Assignment:
1. Write a MIPS assembly language program that displays the first N Fibonacci numbers.
Algorithm Assembly Language
$t0  N – 1;
$t1  1;
$a0  1; display $a0;
loop: display $a0;
$t0  $t0 – 1;
if ($t0 == 0) stop;
$a0  $a0 + $t1;
$t1  $a0 – $t1; go to loop;
stop:
2. Write a complete MIPS program to display all t h e ODD positive integers less than 1000.
5. ARRAYS
To declare an array you need: An array label, the number of elements, The size of each element, and
optionally, the initial value of each element.

Example:
.data
A01: .byte 'a', 'k', 'p', 5 # A01 is an array of 4 bytes: {'a', 'k', 'p', 5}
A02: .word 5, 6, -9, 7 # A02 is an array of 4 words: {5, 6, -9, 7}
B02: .space 40 # allocate 40 consecutive bytes, with storage uninitialized
# could be used as a 40-element character array # 10-
element integer array;
# a comment should indicate which!
var1: .half 3 # create a single short integer variable with initial value 3
B03: .word -1:30 # allocate 30 consecutive words with each element
# initialized with -1.

5.1 Traversing Single-Dimensional Array


To access elements of an array, we have to know the address of that element. Because all elements
have the same size, the address of an element of the array can be formulated as:

The address of ith-element (in byte) = starting address + size-of-element *


where, i
The first element of the array is (i)ndexed 0, and The size-of-element is the number of bytes in a single
array element. The size-of-element either is one byte, 2 bytes, 4 bytes, or 8 bytes.

Example:
The following code fragment is to access the sixth element of table1 array, and demonstrates two
ways of accessing that element:

.data
table1: .word 4, 5, 6, 7, 8, 9, 10, 21
.text
la $t0, table1 lw
$t1, 20($t0)
addiu $t2, $t0, 20
lw $t1, 0($t2)

5.2 Two-Dimensional Arrays


Two or higher dimensional arrays are treated as the same as simple single-dimensional arrays. To
declare the array M[rows][cols] of byte-sized elements: Calculate the number of elements in the
array: number-of-elements = rows * cols. For example,
M: .byte 0:number_of_elements
5.3 Storage Order
As mentioned before, memory is organized as a single-dimensional array. Two-dimensional arrays
must be treated as simple single-dimensional arrays. I n assembly language to declare two-
dimensional arrays, we have to arrange the arrays as single-dimensional arrays.
To do this, we have to know how to organize all elements of an array. There are two different ways
to organize the elements of two-dimensional array:
• Row-major order: The array is organized as a sequence of ROWS. Most of programming
languages such as C follow this method.
• Column-major order: The array is organized as a sequence of COLUMNS. This order is
being implemented in FORTRAN.
5.3.1 Address Calculation
Assume the row and column index starts from 0. The general formula to calculate the byte address of
the element [a, b] can be expressed as:

Row-major order: Starting Address + Size-of-element * ( a * number-of-columns +


b)

Column-major order: Starting Address + Size-of-element * ( b * number-of-rows +


Example:
Suppose the array size has 2 rows and 3 columns:

(0, 0) (0, 1) (0, 2)


(1, 0) (1, 1) (1, 2)

The array stored in row-major order:


(0,0) (0,1) (0,2) (1,0) (1,1) (1,2)
0 n 2n 3n 4n 5n
Lower address Higher address

The array stored in column-major order:

(0,0) (1,0) (0,1) (1,1) (0,2) (1,2)


0 n 2n 3n 4n 5n
Lower address Higher address

Example:
This is an example of how to traverse a 2-dimensional array. On the left the pseudo-code is presented,
and on the right is the corresponding assembly language implementation
Psedo-code MIPS Assembly Language
int M[][] = new int[9][4]; .data
M: .word 0:36
for(int j=0; j<9; j+ # The size-of-element = 4 bytes.
+) M[j][3] = 7; # a = j, b = 3, number-of-columns = 4 ; Then,
# the offset of M[3][4] is 4 * ( j * 4 + 3 ) = 4*(4*j) + 12
.text
la $t2, M
li $t1, 9
li $t0, 0
li $t4, 7
L2: beq $t0, $t1, X2
sll $t3, $t0, 4
addiu $t3, $t3, 12
addu $t3, $t3, $t2
sw $t4, 0($t3)
addiu $t0, $t0, 1
j L2
X2:

The solution of above example using pointer:


.data
M: .word 0:36
.text
la $t2, M
li $t1, 9
li $t0, 0
li $t4, 7 addiu $t2,
$t2, 12
L2: beq $t0, $t1, X2 sw
$t4, 0($t2) addiu
$t0, $t0, 1 addiu $t2, $t2, 16
j L2
X2:

5.4 Assignment
1. Write a MIPS assembly language to transpose a square integer matrix.
2. Write a MIPS assembly program that calculates the sum of all the elements in the following
array: int array[10]={12,21,3,40,15,6,17,8,29,10}

You might also like