# File: P05-2-soln-2.asm # Written by: Archana Chidanandan, Sept. 16, 2003 # # Given a current position component and a velocity component, the MIPS procedure # MoveObject calculates a new position component. It assumes periodic boundaries at positions # zero and eleven in both directions, so a ball that goes off of one edge comes back # at the opposite edge. # ############################################################################### # # NOTE: This procedure adheres STRICTLY to the MIPS register usage conventions. # ############################################################################### # # It is intended to help CS232 students familiarize themselves with MIPS and SPIM. # The students are asked to write : # # 1) A procedure MoveBall that calls MoveObject to update the x-component and the # y-component of the position of a moving "ball." # 2) A main program that initializes the positions and velocities of a "ball" # and then calls repeatedly MoveBall to update its position. # # Register usage # # $a0 - Position component # $a1 - Velocity component # $v0 - Updated position component # .text # Text section of the program (as opposed to data). .globl main .globl MoveBall main: li $s0, 2 # Assign the x-pos li $s1, 3 # Assign the x-vel li $s2, 5 # Assign the y-pos li $s3, 7 # Assign the y-vel loop: move $a0, $s0 # Move x-pos to $a0 move $a1, $s1 # Move x-vel to $a1 move $a2, $s2 # Move y-pos to $a2 move $a3, $s3 # Move y-vel to $a3 jal MoveBall # Call procedure MoveBall move $s0, $v0 # Move the new x-pos to $s0 move $s2, $v1 # Move the new y-pos to $s2 j loop # Continue loop with new positions for # x and y done: addi $v0, $zero, 10 # Ready to exit syscall MoveBall: la $t0, RA # Load address of RA into $t0 sw $ra, 0($t0) # Store return address in "main" to memory # location addressed as RA move $t1, $a0 # Move x-pos and x-vel to registers $t1-2 move $t2, $a1 # After the "Move" procedure call # the values in registers $t0-9 and $a0-3 # will be over-written. # Therefore, the values in $a2 and $a3 # (the y-pos and y-vel) have to be # written into saved registers. # Since the values in saved registers have to # be preserved, we must save the contents # to locations in memory, before we can write # into them. la $t0, SOne sw $s1, 0($t0) la $t0, STwo sw $s2, 0($t0) move $s1, $a2 # Move y-val, y-pos to $s1-2 move $s2, $a3 move $a0, $t1 # Move x-val, x-pos to $a0-1 move $a1, $t2 jal MoveObject # Call Move to update the x-pos value # Since the value of the new x-pos (in $v0) has # to be returned to main, we have to save it # in a saved register, until the end of # of the MoveBall procedure. # The value of the saved register has # to be written to memory before that. la $t0, SThree sw $s3, 0($t0) move $s3, $v0 move $a0, $s1 # Move y-val, y-pos to $a0-1 move $a1, $s2 jal MoveObject # Call Move to update the y-pos value move $v1, $v0 # Store new y-pos value to $v1 move $v0, $s3 # Move the new x-pos value to $v0. # Restore the values of the saved registers. la $t0, SOne lw $s1, 0($t0) la $t0, STwo lw $s2, 0($t0) la $t0, SThree lw $s3, 0($t0) la $t0, RA lw $ra, 0($t0) # Load from memory the return address in main jr $ra # Go back to main MoveObject: add $t0, $a0, $0 # position component add $t1, $a1, $0 # velocity component add $t0, $t0, $t1 # update component ignoring walls cklb: slt $t2, $t0, $0 # set flag if component is less than zero beq $t2, $0, ckub # if flag is clear, ball didn't "cross" wall at zero addi $t0, $t0, 11 # bring ball back in the other side j cklb # a fast ball could "cross" more than once ckub: slti $t2, $t0, 11 # set flag if component is not at least eleven bne $t2, $0, exit # if flag is set, ball didn't "cross" wall at eleven addi $t0, $t0, -11 # bring ball back in the other side j ckub # a fast ball could "cross" more than once exit: move $v0, $t0 # set return value # Wipe out other non-preserved registers for the fun of it li $t0, -1 li $t1, -1 li $t2, -1 li $t3, -1 li $t4, -1 li $t5, -1 li $t6, -1 li $t7, -1 li $t8, -1 li $t9, -1 li $a0, -1 li $a1, -1 li $a2, -1 li $a3, -1 li $v1, -1 jr $ra .data # Data section of the program RA: .word 0 SOne: .word 0 STwo: .word 0 SThree: .word 0