CS201 Lab: Control Flow Structures

	
	In this lab, we are going to explore how to make loops and
	how to implement if-then-else statement in SPIM.

Control Structure Instructions

In this section, the control structure instructions and examples are given for your reference.

Jump Instruction


	The simplest control instruction jump

		j  addr 

	means jump to address addr which makes the processor to 
	fetch the next instruction from the word at address addr.
	Actually, the implementation is just copying the value of
	the address to the program counter.


	There are another two jump instructions

		jal  addr    and

		jr  Rsrc  

	They are used for procedure/function calls.  
	We will talk more about them later.


Conditional Branches

The jump instruction is also known as an unconditional jump. That means the processor always goes to the jump target address without any conditions. The conditional branch is another kind of interesting control structure. The MIPS conditional branch instructions have names that begin with b (b is for branches and j is for jump).

Here are some examples of MIPS jump, branch and compare instructions:


	beq	$t0, $t1, addr 	
	means branch to addr if $t0 == $t1

	beqz	$t0 addr 	
	means branch to addr if $t0 == 0

	bne	$t0, $t1, addr 	
	means branch to addr if $t0 != $t1

	blt	$t0, $t1, addr 	
	means branch to addr if $t0 < $t1

	ble $t0, 5, loop
	means branch to loop if $t0 <= 5

	Here are some examples with psuedo-instructions: 

	beq	$t0, $t1, 80 	
	# branch on equal
	# if ($t0 == $t1)  
	#	go to PC+4+80

	bne	$t0, $t1, -36 	
	# branch on not equal
	# if ($t0 != $t1)  
	#	go to PC+4-36

	slt	$t1, $t2, $t3 	
	# set less than
	# if ($t2 < $t3)  
	#	$t1 = 1
	# else 
	#	$t1 = 0

	slti	$t1, $t2, 7 	
	# set less than immediate
	# if ($t2 < 7)  
	#	$t1 = 1
	# else 
	#	$t1 = 0


	j 0x0040068
	# jump
	# go to 0x0040068

	jr $ra	
	# jump register
	# go to $ra

	jal 0x00400014
	# jump and link
	# $ra = PC + 4
	# go to 0x00400014

Example Programs Using Loops and Conditional Branches

Example 1:

The following program prints out the length of a given string.

##
## 	The program --- length.s 
##
##		- will print out the length of character
##		  string "str".
##		  
##
##		- t0 - holds each byte from string in turn
##		- t1 - contains count of characters
##      	- t2 - points to the string
##

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

        .text
        .globl __start
__start:                	# execution starts here

        la $t2,str		# t2 points to the string
        li $t1,0        	# t1 holds the count
nextCh: lb $t0,($t2)		# get a byte from the string
	beqz $t0,strEnd 	# zero means end of string
	add $t1, $t1,1		# increment count
	add $t2, 1		# move pointer one character
	j nextCh		# go round the loop again

strEnd: 
	la $a0,ans		# System call 
	li $v0,4		# to print out
        syscall         	# the string message

	move $a0,$t1		# copy the count to a0
	li $v0,1		# System call 1 
	syscall			# to print the length worked out

	la $a0,endl		# syscall to print out
	li $v0,4		# a newline
	syscall

	
	li $v0,10
	syscall			# Bye!


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

        .data
	str:	.asciiz "hello world"
	ans:	.asciiz "Length is "
	endl:	.asciiz "\n"	   

##
## 	end of file length.s


Example 2:

The following program replaces some characters in a given string.

##
## 	The program --- loop1.s 
##
##		- will replace all occurrences of 'a' with
##		  'A' in the string "chararray" and 
##		  print the resulting string.
##
##		- chararray:  	"abbbaabbbabababab\n"
##		- output:	"AbbbAAbbbAbAbAbAb"
##      	
##

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

        .text
        .globl __start
__start:                	# execution starts here

        la $t2,chararray	# t2 points to the string
        li $t1,'A'        	# t1 holds the char 'A'
nextCh: lb $t0,($t2)		# get a byte from the string
	beqz $t0,strEnd 	# zero means end of string
	bne $t0,'a',nota	# if not 'a' go to nota
	sb $t1,($t2)		# store upper case 'A'

nota:	add $t2, 1		# move pointer one character
	j nextCh		# go round the loop again

strEnd: 
	la $a0,chararray	# System call 
	li $v0,4		# to print out
        syscall         	# the modified string 

	
	li $v0,10
	syscall			# Bye!


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

        .data
	chararray:	.asciiz "abbbaabbbabababab\n"
	
##
## 	end of file loop1.s



Lab Assignment


Copyright: Department of Computer Science, University of Regina.