Demo entry 2842787

Lab 2


Submitted by anonymous on Oct 14, 2015 at 23:53
Language: S. Code size: 8.3 kB.

# Lab 2
# Luke Gnos & Justin Cellona

	.globl main

	li $v0, 4              #Write bintohex test message
	la $a0, bthTest

	li $a0, 1852594634     #0b01101110011011000101100111001010
	li $a1, 0x10000000
	jal bintohex
	move $t0, $v0

	li $v0, 4
	move $a0, $t0
	li $v0, 4
	la $a0, newline

	li $v0, 4              #Write fibonacci Test message
	la $a0, fibTest       

	li $a0, 0
	li $a1, 1
	li $a2, 0
	jal fibonacciValue

	li $v0, 4              #Shift test 1
	la $a0, shiftTest1       
	li $a0, 0xE608F001
	jal shiftSubroutine

	li $v0, 4              #Shift test 2
	la $a0, shiftTest2       

	li $a0, 0xE608F000
	jal shiftSubroutine

	move $a0, $v0
	li $a1, 0x10000000
	jal bintohex

	move $t0, $v0

	li $v0, 4
	move $a0, $t0
	li $v0, 4
	la $a0, newline

	li $v0, 4              #Double Fixed point add test
	la $a0, fpaTest       

        li $a0, 15
	li $a1, 0xFFFFFFFF
	li $a2, -20
	li $a3, 5

	jal doubleFixedPointAdd

	move $t0, $v0 #high
	move $t1, $v1 #low

	li $v0, 1
	move $a0, $t0

	li $v0, 4
	la $a0, newline

	li $v0, 1
	move $a0, $t1

	li $v0, 4
	la $a0, newline
	syscall               #end double fpa test

	li $v0, 4              #Table function test
	la $a0, addTableTest       
	jal addTableFunction

	li $v0, 10


#Assumes $a0 holds a pointer to the start of the array in memory

addTableFunction :
	li $t0, 0x100000D0
	li $t1, 4155543
	sw $t1, 0($t0)
	li $t1, 3112
	sw $t1, 4($t0)
	li $t1, -2
	sw $t1, 8($t0)
	li $t1, 1054
	sw $t1, 12($t0)
	li $t1, -33543
	sw $t1, 16($t0)
	li $t1, 1233
	sw $t1, 20($t0)
	li $t1, -433433
	sw $t1, 24($t0)
	li $t1, 10101
	sw $t1, 28($t0)
	li $t1, 16384
	sw $t1, 32($t0)
	move $t2, $t0
	li $t1, 0          
	li $t0, 0              #initialize result variable to 0
	li $t4, 9

atfLoop :
	lw $t3, 0($t2)           #load the word stored in the table
	addi $t2, $t2, 4          #increment pointer by a word
	add $t0, $t0, $t3           #update res variable
	addi $t1, $t1, 1         #increment loop variable
	bne $t1, $t4, atfLoop        #check if done, if not loop again

	#t0 holds result
	move $t8, $ra            #store old ra
	move $a0, $t0
	li $a1, 0x10000000 
	jal bintohex               #call bintohex
	move $ra, $t8           #restore old ra
	add $t6, $0, $v0            #store result of bintohex

	li $v0, 4
	move $a0, $t6

	jr $ra

#a0-a3 hold 64-bit values a and b, add together
# Puts result in v0 and v1, with v0 holding the high bits
doubleFixedPointAdd :
	addu $t0, $a1, $a3          #Add low words together
	sltu $t1, $t0, $a3

	add $t1, $t1, $a2          #Add high words together
	add $t1, $t1, $a0          #Add overflow 1 if necessary
	move $v0, $t1              #Set function result register
	move $v1, $t0
	j $ra

# Assumes a0 in the form of fff0 0nn0 0000 x000 yyyy 0000 0000 0000
# Will put v0 in form of 0000 0000 0000 0000 yyyy 000x 0fff 00nn
shiftSubroutine :
	#And out the important bits to check for bad input
	li $t5, 0x19F70FFF 
	and $t0, $a0, $t5
	beq $t0, $0, ssbrCont          #check if invalid input exists
	li $v0, 4
	la $a0, ssrError
	j $ra

ssbrCont :
	li $t5, 0x6000000
	and $t0, $a0, $t5           #Get 'n' values and shift over to far right
	srl $t0, $t0, 0x19
	li $t5, 0xE0000000
	and $t1, $a0, $t5           #Get 'f' values and shift over
	srl $t1, $t1, 0x1D
	li $t5, 0x80000
	and $t2, $a0, $t5           #get 'x' value and shift over
	srl $t2, $t2, 0x13
	li $t5, 0xF000
	and $t3, $a0, $t5           #Get 'y' values and shift over
	srl $t3, $t3, 0x0C

	li $t4, 0                   #clear result register
	sll $t4, $t3, 4             #put in y values and shift over
	or $t4, $t4, $t2            #put in x value and shift over
	sll $t4, $t4, 4
	or $t4, $t4, $t1            #put in f values and shift over
	sll $t4, $t4, 4
	or $t4, $t4, $t0            #put in n values
	move $v0, $t4              #set function result register

	jr $ra                          #return


# Expects two arguments, being the last two numbers in the sequence
#  or on the first call, the first two numbers (0,1)
# Expects a2 to be 0 if initial call
#Stops at F20 or 6765
fibonacciValue :
	add $t1, $a0, $a1         #Add the two previous numbers of the sequence
	add $t0, $a1, $0          #save a1 arg

	#Check if it is the first call, if it is, print out the zero
	bne $a2, $0, fibNotFirstCall
	li $t3, 0
	li $v0, 1
	move $a0, $t3

	li $v0, 4
	la $a0, newline
	li $a2, 1
fibNotFirstCall :	

	#Print out the number computed in this call and a newline
	li $v0, 1
	move $a0, $t0

	li $v0, 4
	la $a0, newline
	move $a0, $t0
	move $a1, $t1

	li $t2, 10946      #set compare value to F21 = 10946
	#If at F20 return to main, otherwise continue
	bne $t1, $t2, fibContinue 
	jr $ra

fibContinue :
	j fibonacciValue          #Call function recursively


# Function that takes in a 32bit binary value (a0) and a
#  address (a1) to place the result of transforming it into the
#  ascii string representation of the hex
# Returns a pointer to the address given (a1)
bintohex :
	li $t1, 0x50
	add $t0, $a1, $t1          #Set t0 to a1 + 10bytes
	li $t2, 10                 #set loop var to 
	addi $t1, $0, 0x30         #set t1 to ascii '0'
bthAsciiNum :     
     sb $t1, 0($t0)             #store that value in memory
     addi $t1, $t1, 1           #increment ascii value
     addi $t0, $t0, 8           #increment pointer to memory
     addi $t3, $0, 1            #decrement loop var (t2)
     sub $t2, $t2, $t3
     bne $t2, $0, bthAsciiNum   #check if done with 0-9, loop back if not

     addi $t2, $0, 6            #set loop var to 6
     addi $t1, $0, 0x41         #set t1 to ascii 'A'

bthAsciiLet :
     sb $t1, 0($t0)             #store value in memory
     addi $t1, $t1, 0x01        #increment ascii value
     addi $t0, $t0, 0x08        #increment pointer to memory
     addi $t3, $0, 0x01         #decrement loop var (t2)
     sub $t2, $t2 $t3
     bne $t2, $0, bthAsciiLet   #check if done with A-F, loop back if not

     addi $t1, $0, 8         #set loop var to 8 (t2)
     add $t0, $0, $a1           #store original mem addr for result
bthOuter :
	move $t2, $a0	           #copy a0 into temp
	srl $t2, $t2, 28                #shift temp over to bottom 4 bits
	sll $a0, $a0, 4                 #shift a0 over by 4

	addi $t3, $a1, 0x50        #load first byte of asciiLib
	li $t4, 0
bthInner :
	beq $t2, $t4, bthInnerDone  #If at the right asciiChar, go to store
	addi $t4, $t4, 0x01         #Increment loop var
	addi $t3, $t3, 8           #increment pointer in asciiLib
	j bthInner
bthInnerDone :
     lbu $t4, 0($t3)            #get the asciiChar pointed at by t3
     sb $t4, 0($t0)             #store the found asciichar (t2)
     addi $t0, $t0, 1        #increment result pointer address
     li $t5, 1               #decrement loop variable
     sub $t1, $t1, $t5          
     bne $t1, $0, bthOuter

     add $t4, $0, $0            #store 0 or 'null' at last location
     sb $t4, 0($t0)

#return the start of the result memory
     addi $v0, $a1, 0x00      #put pointer to result in function result register
     #print out somehow??   mips syscall??

     jr $ra                      #Return to main caller

end :
newline:	.asciiz "\n"
ssrError:	.asciiz "Zero fields detected to be non-zero\n"
bthTest:	.asciiz "Testing bintohex function with 0x6E6C59CA\n"
fibTest:	.asciiz "Testing recursive Fibonacci Sequence\n"
shiftTest1:	.asciiz "Testing shift function with 1 in 0-field\n"
shiftTest2:	.asciiz "Testing shift function with all 1's in non-zero fields\n"
fpaTest:	.asciiz "Testing Double fixed point add function with -15 and 20 in the high registers, and 0xFFFFFFFF and 5 in the low registers\n"
addTableTest:	.asciiz "Testing table add function with given Table 1\n"

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).