Sample SPIM Program


# Sample Spim Program
#
# Written by Pat Troy, 1/26/2004

	.data
array1: .word 0:100
length: .word 100
max:    .word 0

prompt1:	.asciiz	"Enter ten integer numbers.\n"
prompt2:	.asciiz "The ten integers are:\n"
prompt3:	.asciiz "The ten values in hex are:\n"
newline:	.asciiz "\n"
tab:		.asciiz "\t"
hexdigits:	.asciiz "0123456789abcdef"
hexword:	.asciiz "00000000"
hexdig:		.asciiz "0"
goodbye:	.asciiz	"Goodbye\n"
dit:		.asciiz	"."
dash:		.asciiz	","

	.globl main

	.text


main:   
	# prompt for input
        li 	$v0, 4
	la 	$a0, prompt1
	syscall

	# set up the loop variables
        li 	$t0, 10
	la 	$t1, array1

        # Read in the integers
loop1:  li 	$v0, 5
	syscall
        sw  	$v0, ($t1)

        # decrement loop counter and continue
	addi 	$t0, $t0, -1
	addi  	$t1, $t1, 4
        bgtz	$t0, loop1

	# display the number read in
        li 	$v0, 4
	la 	$a0, prompt2
	syscall

	# set up the loop variables
        li 	$t0, 10
	la 	$t1, array1

        # print out the integers 
	# (first a tab, then the int, then a newline)
loop2:  li 	$v0, 4
        la	$a0, tab
	syscall
        li      $v0, 1
        lw  	$a0, ($t1)
	syscall
        li 	$v0, 4
        la	$a0, newline
	syscall

	# decrement loop counter and continue
	addi 	$t0, $t0, -1
	addi  	$t1, $t1, 4
        bgtz	$t0, loop2

	# print each number in hex
        li 	$v0, 4
	la 	$a0, prompt3
	syscall

	# set up the loop variables
        li 	$t0, 10			# loop3o counter
	la 	$t1, array1

	# get the value and put it in $t2
loop3o:	lw	$t2, ($t1)

	# initialize values for the inner loop
	la	$t6, hexdigits	
	la	$t7, hexword		
	li	$t3, 15			# the mask value
	sll	$t3, $t3, 28
	li 	$t4, 28			# loop3i counter and shift amount

	# mask off the correct 4 bits for a hex digit 
	# and shift for bit positions 0-3
loop3i:	and	$t5, $t2, $t3
	srl	$t5, $t5, $t4

	# get proper hex digit
	add	$t5, $t5, $t6
	lb	$t8, ($t5)
	sb	$t8, ($t7)

	# process loop values and branch
	srl	$t3, $t3, 4
	addi	$t7, $t7, 1
	addi	$t4, $t4, -4
	bgez	$t4, loop3i

	# output the hex word
	li 	$v0, 4
        la	$a0, tab
	syscall
        li      $v0, 4
        la  	$a0, hexword
	syscall
        li 	$v0, 4
        la	$a0, newline
	syscall

	# process loop values and branch
	addi 	$t0, $t0, -1
	addi  	$t1, $t1, 4
        bgtz	$t0, loop3o


	# end the program
        li 	$v0, 4
        la	$a0, goodbye
	syscall
	li	$v0, 10
	syscall