aanders5 0 Junior Poster

Ok, I have two pieces of code, this is my FIRST time ever trying to do a SPIM coding, so forgive me if this is a total trainwreck. :)

I will FIRST post the question, along with pseudo code of what it is suppose to do.
THEN I will post my awesomely horrific SPIM code.

1.The given algorithm computes the sum of the non-trivial factors of a number. Without loss of generality, the value of number is hard coded to be 36.

Pseudo code for #1, NOT SPIM.

#int     number = 36
#int     sof
#int     tf

#void main( )
#{
#    sof  =  0
#    tf   =  2
#    while (tf * tf < number)
#    {
#        if (number % tf == 0)
#        {
#            sof  =  (number / tf) + tf +  sof
#        }
#        tf  =  tf + 1
#    }

#    if (tf * tf == number)
#    {
#      sof  =  tf + sof
#    }
#}

My SPIM CODE for #1.

#Author Austin Anderson
.text        
            .globl main

main:       li $v0, 4 
            li $v1, 4              
	    li $t0, 0, 4
	    li $t1, 2, 4
	    li $t2, number, 4	
	    li $t3, 0, 4
            .globl while
while:      beq $t0, t$1

            .globl if
if:         blt
            beq t$3, $t2, $t1
	    beq t$0, 
            div t$2, t$1,
            add $v0, t$2, t$0
            b end
            endwhile

            .globl if
if:         blt 
            beq $v1, t$2
            mul v$1, $t1, $t1
            add t$0, t$1, t$0
            b end

 	    syscall

----------------------------------------------------------

2. Compute both the perimeter and the area of a 5 by 12 rectangle.
Pseudo code for #2, NOT SPIM.

# int  length = 12
# int  width = 5
# int  perimeter
# int  area

# void main()
# {
#   perimeter = (length + width) * 2
#   area      = length * width
# }
#Author Austin Anderson
.text        
            .globl main

main:       li $v0, 4 
            li $v1, 4              
	    li $t0, 5, 4
	    li $t1, 12, 4
	    li $t2, perimeter, 4
	    li $t3, area, 4

            add v$0, t$1, t$0
	    mul $t2, v$0, 2

            mul $t3, t$0, t$1

	    syscall

Again, forgive me if this is bloody murder to any of you. I am insanely new to this, and the course only requires me program in SPIM like..3x, and then we will never use it again. I want to understand it though.

Thanks, and based off of this question's response, I may start using this site more often.
-Austin! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.