################################### # Testing for overflow on integer addition # Sample solution ################################### .data MES0: .asciiz "Welcome, this is an integer addition overflow checker\n" MES1: .asciiz "Input an integer: " MES2: .asciiz "SUM OK, SUM = " MES3: .asciiz "OVERFLOW, MAX = " MES4: .asciiz "UNDERFLOW, MIN = " MES5: .asciiz "GOODBYE\n" NL: .asciiz "\n" MAXMES: .asciiz "OVERFLOW, MAX = " MINMES: .asciiz "UNDERFLOW, MIN = " ####NOTE for the program template, they need to figure these values themselves. Just replace the next two #### values with 1 & -1 respectively. MAX: .word 1 # What is really needed here is the maximum positive integer in MIPS - you figure it out MIN: .word -1 # What is really needed here is the minimum integer in MIPS - you figure it out .text main: li $v0,4 la $a0,MES0 syscall agn: li $v0,4 la $a0,MES1 syscall li $v0,5 syscall move $t1,$v0 # put first integer in $t1 li $v0,4 la $a0,MES1 syscall li $v0,5 syscall move $t2,$v0 # put second integer in $t2 ################################ # At this point $t1 and $t2 contain the integers that we want to add # # We will add these two integers, and then check to see if overflow can occur. # If it cannot occur, i.e. they are of differing signs, then we just output their # sum. If overflow can occur, i.e. they are of the same sign, then will check to see # the sign of the sum is the same as that of the integers. If it is, then overflow # has not occurred, and we can output the sum. # If the sign of the sum is not the same as that of the integers we just added, then we # must determine if we have overflow or underflow and out put the appropriate value. # NOTE: The MIPS will trap on a real overflow when performing an add, so you cannot use a regular # add, unless you know how to set up the trap mechanism. For now, we won't worry about this. # However, we are in luck, because addu (add unsigned) does the same as an add, but it # does not check for or trap on an overflow. addu $t0,$t1,$t2 # Do the addition xor ????????? # Now we check to see if the signs are the same blt $t3,$zero,novr # xor does an exclusive-or of every bit of the two numbers # We only want to know if the left most bits, i.e. the sign bits, differ. # Thus, using the xor, the left most bit in $t3 will be a 1 only if they differ. # Since the left most bit is the sign bit, $t3 will be negative (<0) if its # left most bit is a 1. So we know the values are of differing signs if $t3 # is negative. That means, $t0 has the correct sum ################################ # At this point we know the two integers are of the same sign and thus there could be overflow # # What you need to do here is see if the sign of the result ($t0) differs from that of either # of the integerss ($t1 or $t1). If it is the same, then there was no overflow. If not, then overflow has occurred. xor ???????? # Check to see if the sign of the results differs from that of the operands # NOTE: we only have to do the one comparison since we know the sign # of the two operands, i.e. the two integers read in $t1, and $t2, is the same. bge $t3,$zero,novr # The sign bit of $t3 will be a 0 if it is the same as that of the two operands # That means $t3 must have a value >= 0 # We end up here if overflow has occurred. If the sign of the result in $t3 is 0, then we must # have added two negative numbers and the overflow caused the result to be positive. Thus, we want # to output the most negative number MIPS can reprresent. Otherwise, we want to output the most positive value. # You fill in this part # We come here if there has been no overflow. That may be because it could not occur, or because, # even though it could occur, it did not. # We get here if no overflow has occurred. What we now need to do is check to see if -1, -1 was input novr: # You fill in this part li $v0,10 # This terminates execution syscall nostop: # You fill in this part j agn # Get another pair of values to add