Professional Documents
Culture Documents
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:
Example 2:
The following code fragment will set bit 7, 6, 5, 3 and 0 of the register $t2 using OR operation:
Example 1
The following code fragment will multiply the content of register $t0 with 80:
Example 2
Explain what the following code fragment will do?
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.
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)
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:
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}