CS201 Lab: SPIM Hardware Lab

The hardware that we are going to use is PIC32 Starter Kit. The following picture is a screen shot from "PIC32 Starter Kit - DM320001 | Microchip Technology Inc." web site for your information.


	In PCSpim, we can use system call (syscall) to handle the input and output.  
	In the hardware that we will be using this week, input and output will be
        handled differently.  Let's look at some of the methods such as how to read 
        integers, how to read a string, how to print a string, and how to print a 
        string that contains values.  These notes are the outcome of our
        collaboration with Trevor Douglas, a lab instructor in the Faculty of
        Engineering.

Print a String using printf function

 .data

  prompt1:     .asciiz "Please enter an integer:\n\000"

in your program, for example, you have  

  la $a0,prompt1		# print prompt on terminal
  jal printf			# output a string

Read an integer using readi function


  jal readi           # read an int and the value is in the $v0

Print a value using printf function

 .data

  printInt:    .asciiz "The sum is: %d\n\000"

in your program, you would have the following:

  la $a0, printInt      # print prompt on terminal
  jal printf		# it will print the value in $a1 at %d position

You indicate the position for the output data in the message.

Read a String using mreads function


  .data

  prompt1:     .asciiz "Please enter a string up to 50 chars:\n\000"
  stringBuffer:		.space 50

in your program, you would have the following to read a string:

  la	$a0,prompt1		# print prompt on terminal
  jal 	printf			# out a string

  la	$a0,stringBuffer	# load the address of the stringBuffer
  li	$a1,50			# load the length
  jal	mreads
  

Sample Program One

/*********************************************************************
 * FileName:        sum.S
 *
 * Processor:       PIC32MX
 * 
 * Assembler/Compiler/Linker:  MPLAB C32
 *
 * 
 * Description:
 * This project asks the user to enter an integer.  
 * The software will then determine the sum.
 *
 ********************************************************************/

#include <p32xxxx.h>


#################################################
  .data

  prompt1:     .asciiz "Please enter an integer:\n\000"
  printInt:    .asciiz "The sum is: %d\n\000"


## Students DO NOT edit this section
#################################################
#                                               #
#               text segment                    #
#                                               #
#################################################
  .text
  .align	2
  .globl	main
  .ent	main
  .type	main, @function
main:
  .frame	$fp,512,$31	# vars= 8, regs= 1/0, args= 0, gp= 8
  .mask	0x40000000,-8
  .fmask	0x00000000,0
  .set	noreorder
  .cpload	$v05
  .set	reorder
  addiu	$sp,$sp,-512
  sw	$31,508($sp)
  sw	$fp,504($sp)
  move	$fp,$sp
  .cprestore  16
## END Students DO NOT edit this section


## Students edit here  #################################################

  la $a0,prompt1	# print prompt on terminal
  jal printf		# print out a string
  jal readi             # read an int

  move $a0, $v0         # move into the first parameter location
  jal sumup             # call sumup


  la $a0, printInt	# prepare for printing
  move $a1, $v0         # move the result into $a1
  jal printf		# printf out the result


	    
## Do not edit  below ######################################

  move	$sp,$fp
  lw	$31,508($sp)
  lw	$fp,504($sp)
  addiu	$sp,$sp,512
  j	$31
  .end	main

## Do not edit  above ######################################	



############################################################
##
##	The function "sumup" will calculate the sum of 
##	the integers from 1 to N (N will be passed to
##	the function "sumup" by a register $a0, and 
##	the sum will be returned by $v0).
##
############################################################

sumup:
	li $t0,0
  	move $v0,$t0   		# Initialize the sum to 0


loop:   
        add $v0, $v0, $a0	# $v0 = $v0 + $a0
	addi $a0, $a0, -1	# $a0 = $a0 - 1
	bnez $a0, loop		# branch to lop if $a0 != 0

	jr $ra			# return to the calling function

Sample Program Two

/*********************************************************************
 * FileName:        vowelCount.S
 *
 * Processor:       PIC32MX
 * 
 * Assembler/Compiler/Linker:  MPLAB C32
 *
 * 
 * Description:
 * This project asks the user to enter a string.  
 * The program then counts the number of vowels in the string.
 *
 ********************************************************************/

#include <p32xxxx.h>

#################################################
  .data

  prompt1:     .asciiz "Please enter a string up to 50 chars long:\n\000"
  stringBuffer:		.space 50
  printNumVowels: .asciiz "The number of vowels is: %d\n\000"
  
  #Used for debugging
  #printChar:   .asciiz "The var sitting in the return is: %c\n\000"
  #printInt:    .asciiz "The int sitting in the return is: %d\n\000"

## Students DO NOT edit this section
#################################################
#                                               #
#               text segment                    #
#                                               #
#################################################
  .text
  .align	2
  .globl	main
  .ent	main
  .type	main, @function
main:
  .frame	$fp,512,$31	# vars= 8, regs= 1/0, args= 0, gp= 8
  .mask	0x40000000,-8
  .fmask	0x00000000,0
  .set	noreorder
  .cpload	$25
  .set	reorder
  addiu	$sp,$sp,-512
  sw	$31,508($sp)
  sw	$fp,504($sp)
  move	$fp,$sp
  .cprestore  16
## END Students DO NOT edit this section


## Students edit here  #################################################

  la	$a0,prompt1		# print prompt on terminal
  jal 	printf			# out a string

  la	$a0,stringBuffer	# load the address of the stringBuffer
  li	$a1,50			# load the length
  jal	mreads
  
 
  la $s0, stringBuffer
  lb $a0, ($s0)
  jal vowelp
  
  la $a0, printNumVowels
  move $a1, $v0
  jal printf

	    
## Do not edit  below ######################################

  move	$sp,$fp
  lw	$31,508($sp)
  lw	$fp,504($sp)
  addiu	$sp,$sp,512
  j	$31
  .end	main

## Do not edit  above ######################################	


vowelp:

	li $v0, 0		# initialize $v0 to 0
	beq $a0,'a',yes
	beq $a0,'e',yes
	beq $a0,'i',yes
	beq $a0,'o',yes
	beq $a0,'u',yes
	jr $ra
yes:	li $v0,1
	jr $ra
 
	



Lab Assignment

This page last modified:
Wednesday, 29-Mar-2017 12:05:22 CST
Accessed     times.

Copyright: Department of Computer Science, University of Regina.