Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Computer Organization & Assembly Language: Lab Manual 5

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Computer Organization & Assembly Language

Lab Manual 5

Lab Engineer: Maroof Kousar

Topic: MIPS Instruction Set Formats & MIPS Registers

Knowledge Unit of Systems and Technology


UMT Sialkot Campus Pakistan
MIPS Instruction Formats are sometimes called MIPS instruction encoding formats. Instruction
encoding means the MIPS instruction are translated into binary numbers and then bring back to
original format for human understanding(decoding). Encoding should be done in a way that decoding
is easy. MIPS ISA has a 32-bit fixed instruction encoding. MIPS instruction formats include:

 R-Format
 I-Format
 J-Format
 FI-Format
 FR Format

MIPS Instruction Format Table

Let's discuss MIPS instruction formats in details with tables:

NAME TOTAL 32-BITS DESCRIPTION

Field Size 6-bits 5- 5-bits 5- 5-bits6- total 32-bits


bits bits bits
R-Format opcod rd rs rt shamt funct Arithematic and Logic Instructions
e
I-Format opcod rd rs immediate value Branches, Immediate, Data Transfer
e
J-Format opcod Target Address Jump Instructions
e
FR- opcod fmt ft fs Fd funct Floating Point (R) Instructions
Format e
FI-Format opcod fmt ft immediate value Floating Point (I) Instructions
e

MIPS R-Format

General R-type instruction has the following form:

opcode $rd, $rs, $rt

MIPS Instruction R-Format is used for arithmetic and logical instruction. Encoding format used for

MIPS R-Formate is given below:

TOTAL 32-BITS

6-bits 5-bits 5-bits 5-bits 5-bits 6-bits

opcod rd rs Rt shamt funct


e

Opcode (bit 31-bit 26)


Opcode stands for "operational code". It is the machine representation of instructions. It's is 6-bit
long.

rs (bit 25-bit 21)

The first source register is rs. The source register contains a value for the operation. It's is 5-bit long.

rt (bit 20-bit 16)

This is the second source register. It's is 5-bit long.

rd (bit 15-bit 11)

The destination register is rd. It stores the result of operations performed or rs and rt. It's is 5-bit
long.

shamt (bit 10-bit 6)

Shamt stands for shift amount. It shows how many bits rs is shifted. It's is 5-bit long.

funct (bit 5-bit 0)

The function is used in addition to opcode to specify the operation. Its size is 6-bits.

R Type Instruction Format Example

Consider the following subtraction example to understand the use of R-type:

sub $t1, $t2, $t3

Where SUB is the opcode, $t1 is the destination register $rd, $t2 is the first source and $t3 is the
second source register.

MIPS I-Format

I stands for "immediate value". An immediate is a 16-bit value.  The General form of an I-Type
instruction is as follows:

opcode $rt, $rs, immediate value

Encoding format used for MIPS I-Formate is given below:

TOTAL 32-BITS

6-bits 5-bits 5-bits 5-bits 5-bits 6-bits

opcod rd rs immediate value


e
Opcode (bit 31-bit 26)

It is operational code. It is the machine representation of instructions. It's is 6-bit long. i.e. add, sub

rs (bit 25-bit 21)

The first source register is rs. The source register contains a value for the operation. It's is 5-bit long.

rd (bit 20-bit 16)

Rt is the destination register. It contains the results.

immediate value(bit 15-bit 0)

Immediate value is a 16-bit value used with another source register.

I-type Instruction Format Example

Consider the following subtraction example to understand the use of I-type:

li $t2,30

sub $t1, $t2, 26

Where SUB is the opcode, $t1 is the destination register $rt, $t2 is the first source and 26 is the
immediate value.

MIPS J-Format

J stands for "JUMP.". J-type has a jump instruction and a target address. General representation of J-
Type is as follows:

jump          Target_Address

TOTAL 32-BITS

6-bits 5-bits 5-bits 5-bits 5-bits 6-bits

opcod Target Address


e

Opcode (bit 31-bit 26)

In I-format opcode is a 6-bit jump instruction. They are usually j, jal, jar  etc

Target Address (bit 25-bit 0)


The target address is 26-bit address in a program where the control is to be transferred.

J-type Instruction Format Example

Consider the following subtraction example to understand the use of I-type:

j addition

addition:

add $t1,$t2,$t3

Here we have a 26-bit target address "addition".

MIPS FR-Format

It's similar to R-type instruction, the only difference is that it is reserved for floating point numbers.

TOTAL 32-BITS

6-bits 5-bits 5-bits 5-bits 5-bits 6-bits

opcod fmt ft fs fd funct


e

MIPS FI-Format

It's similar to I-type instruction, the only difference is that it is reserved for floating point numbers.

TOTAL 32-BITS

6-bits 5-bits 5-bits 5-bits 5-bits 6-bits

opcod rs rt immediate value


e

Mips Registers

A Register is the smallest memory on a processor. There are multiple registers on a processor
assigned various tasks. In assembly language, we operate on data through a set of registers. There
are different registers for each architecture such as MIPS registers, X86 registers, and ARM
registers. It can be directly addressed or accessed.

A register is usually equal to the size of the processor. I.e., if a processor is 32 bit than the registers
will also be 32 bits.

Whereas there are some basic registers used for various operation on a processor. Some of the basic
registers are:
 Memory buffer register (MBR)
 Memory address register (MAR)
 Instruction register (IR)
 Instruction buffer register (IBR)
 Program counter (PC)
 Accumulator (AC) and multiplier quotient (MQ)

Mips Registers Table

General Purpose Registors Special Purpose Registors


32 Bits
$0 $s0 HI
$at $s1 LO
$v0 $s2
$v1 $s3
$a0 $s4
$a1 $s5
$a2 $s6
$a3 $s7
$t0 $t8
$t1 $t9
$t2 $k0
$t3 $k1
$t4 $gp
$t5 $sp
$t6 $fp
$t7 $ra PC (Program Counter)

In MIPS assembly each register is 32 bit. These registers are divided into the general purpose and
special purpose registers.

General Purpose Registers

There are 32 general purpose registers. General purpose means these all can be used as an operand
in the instructions but still there are limitations for some registers. i.e

 $0, termed $zero, always has a value of “0”.


  $31, termed $ra (return address), is reserved for storing the return address for subroutine
call/return.

Special Purpose Registers

There are three special purpose registers in MIPS ISA:

 HI/LO registers used to store the result from multiplication.


 PC register (program counter).
• Always keeps the pointer to the current program execution point; instruction fetching
occurs at the address in PC.
• Not directly visible and manipulated by programmers in MIPS

Task:
Write a program in which:

1- Use any 3 R-Type and I-Type instructions in your task.


2- Use j instruction inside your code to demonstrate its usage.
3- Use registers from $t0 to $t9 in your previous 2 tasks.

Mips Syntax is pretty similar to x86 and Arm syntax. Below is the basic syntax of MIPS instruction
set:

.data  #Declare or initialize variables

.text

#Here main program goes (Instructions)

#End instruction to terminate the program

In data section variables are declared or initialize. See example below:

Example 1:

.data
welcome: .asciiz  “\nWelcome to MIPS:”

In the above welcome is variable initialized with string data.

.text

li $v0,4   # output message


la $a0,msg1     # indicate the message!
syscall

li $v0,10   # exit program!


syscall

Example 2:

.data
msg1: .asciiz  “\nSum of value and 100:”
In the above msg1 is variable initialized with string data.

.text

li $t0, 5

addi $t1,$t0,100    # $t1 = $t0 + 100

li $v0,4   # output message


la $a0,msg1    # indicate the message!
syscall

li $v0,1   # print integer!


move $a0,$t1   # value to print!
syscall

li $v0,10   # exit program!


syscall

Hope you understood the basic Syntax! If there any question let us know in the comment box.

Task:
Your task is to print your roll no, name, batch and subject.

You might also like