CS201 Lab: MIPS Addressing Modes


	There are different ways to specify the address of the operands 
	for any given operations such as load, add or branch.  
	The different ways of determining the address of the operands are 
        called addressing modes.
	
	In this lab, we are going to explore different addressing modes 
	of MIPS processor and learn how all instructions can fit into a 
	single word (32 bits).

MIPS Instruction Formats

There are three types of instruction format. They are Register Type, Immediate Type, and Jump Type.











	The meaning of the different parts in each format:

	op - 	6-bit operation code 

	rs - 	5-bit source register specifier

	rd - 	5-bit destination register specifier

	rt - 	5-bit target (source/destination) register 
		or branch condition

	immediate - 16-bit immediate branch displacement
		    or address displacement

	target - 	26-bit jump target address

	shamt  - 	5-bit shift amount

	funct  - 	6-bit function field

There are different MIPS addressing modes. We have already seen some examples in the previous labs.
Here are the four addressing modes:
		
In the following sections, we will examine each of these modes, observe how they are implemented using the above formats and
introduce indexed addressing mode, which is implemented by the assembler as a pseudo instruction.

MIPS Register addressing

The simplest addressing mode is the register addressing. Instructions using registers execute fast because they do not have
the delay associated with memory access. But, the number of registers is limited since only 5-bits are reserved to select a register.
Register addressing is a form of direct addressing. The value in the register is an operand instead of being a memory address to an operand.

Now look at an example:

The following table shows some of the opcodes used by MIPS.
The value of rs, rd, rt should be the actual register number, and can fit in five bits since there are 32 registers.




MIPS Immediate addressing

MIPS immediate addressing means that one operand is a constant within the instruction itself.
The advantage of using it is that there is no need to have extra memory access to fetch the operand.
But keep in mind that the operand is limited to 16 bits in size.

The jump instruction format can also be considered as an example of immediate addressing, since the destination is held in the instruction.
Here is a reference for you: The Jump Instruction

MIPS Base addressing

In the C programming language, a record or a structure can be defined.
For example:

	struct marks
	{
		int CS100;
		int Math110;
		int BIOL101;
		int PHYS100;
		int ENGL100;
	};

In base register addressing we add a small constant to a pointer held in a register.
The register may point to a structure or some other collection of data, and we need to load a value at a constant offset from the beginning of the structure.
Because each MIPS instruction fits in one word, the size of the constant is limited to 16 bits.
Now look at the syntax.

	lw rd,i(rb)

For example, if $t0 pointed to the base of a record or structure, we could get at the fields using

	lw $t1, ($t0)
	lw $t1,4($t0)
	lw $t1,8($t0)
	lw $t1,12($t0)
	lw $t1,16($t0)
	etc ...

We have used a form of base addressing with zero offset in the program length.s.

This kind of addressing is also known as indirect register addressing since the operand is at the memory location whose address is in the register.

MIPS PC-relative addressing

PC-relative addressing is used for conditional branches. The address is the sum of the program counter and a constant in the instruction.

Here is an example to help you understand it.

An Example Using Base addressing

Program min-max.s is an interesting program using the base-addressing mode.
It searches an array of words for the biggest and smallest integer elements.
The .word directive is used to set up an array of 15 four-byte words in the data section.
An array data structure is a named list of items stored in memory.
The following picture illustrates an array of words stored in memory.

Here is the program min-max.s for you to study. The lw instruction is used to load each word into a register.


##
## 	Program Name:	min-max.s 
##
##		- will print out the minimum value min 
##		- and the maximum value max of an array.
##		
##		- Assume the array has at least two elements (a[0] and a[1]). 
##		- It initializes both min and max to a[0] and then
##		- goes through the loop count - 1 times.
##		- This program will use pointers.
##		
##
##		$t0 - point to array elements in turn
##		$t1 - contains count of elements
##      	$t2 - contains min
##		$t3 - contains max
##		$t4 - each word from array in turn
##

#################################################
#                                               #
#               text segment                    #
#                                               #
#################################################

        .text
        .globl __start
__start:           	   	# execution starts here

	lw $t1,count		# $t1 is the counter and contains array size now.
	la $t0,array		# $t0 contains base address of the array.
				# $t0 will point to the array elements.
	lw $t2,($t0)		# $t2 is current min, like initializing min = a[0].
	lw $t3,($t0)		# $t3 is current max, like initializing max = a[0].
	add $t0,$t0,4		# $t0 is updated and points to a[1].
	add $t1,$t1,-1		# $t1, the counter is updated.
	
        
loop:	lw $t4,($t0)		# load next word from array
	bge $t4,$t2,notMin	# skip if a[i] >= min
	move $t2,$t4		# otherwise, copy a[i] to min

notMin:	ble $t4,$t3,notMax	# skip if a[i] <= max
	move $t3,$t4		# otherwise, copy a[i] to max


notMax:	add $t1,$t1,-1		# decrement counter
	add $t0,$t0,4		# increment pointer by 4 to the next word
	bnez $t1, loop		# continue if $t1 > 0,  exit loop when $t1 == 0


	la $a0,ans1   		# print prompt on terminal
	li $v0,4		# system call to print
	syscall			# out "min = "

               
	move $a0,$t2		# print result min
	li $v0,1
	syscall
	
	
	la $a0,ans2		# print out "max = "
	li $v0,4
	syscall 

	move $a0,$t3		# print result max
	li $v0,1
	syscall

	la $a0,endl		# syscal to print out
	li $v0,4		# a new line
	syscall 

	li $v0,10		# Exit
	syscall			# Bye!


#################################################
#                                               #
#               data segment                    #
#                                               #
#################################################

        .data
	array:	.word 3, 4, 2, 6, 12, 7, 18, 26, 2, 14, 19, 7, 8, 12, 13
	count:	.word 15
	ans1:	.asciiz "min = "
	ans2:	.asciiz "\nmax = "
	endl:	.asciiz "\n"	   

##
## 	end of file min-max.s


Lab Assignment


Copyright: Department of Computer Science, University of Regina.