求和方格的溢出问题

时间:2018-04-22 11:47:46

标签: assembly mips

我正在处理要求我们执行的任务:

在MIPS程序集中编写一个程序,给定$a0中的内存地址到n个32位无符号数的数组的第一个元素(n存储在$a1中),它应该是正方形他们每个人和正方形相加。应该在$v1上返回32个最高有效位,而在$v0上返回最不重要的位。

特别退货(在v0上):

  • 如果数组没有元素(n = 0)则归零
  • DEADBEEF(hex)= 3735928559(dec),如果结果不能适合64位。

到目前为止我做了什么:(输入是通过单独处理,给予我们的代码)

squaredSum:
            beq     $t5, $zero, getRetAddr  # if $t5 (caller's ret. addr.) is zero (ie. first time the function is called, then get the caller's ret. addr.)  
            move    $t0, $a1                # $t0: array size (we want to turn it into bytes)
            sll     $t0, $t0, 2             # multiply array size by 4, so we get the total number of bytes (2 left bitwise shifts)
            beq     $t1, $t0, sumExit       # if offset ($t1) = bytes, then end of array, so we terminate (jump to "exit" label)
            add     $t2, $t1, $a0           # $t2: address from which we start reading (base + offset)
            lw      $t3, 0($t2)             # $t3: the number to be squared is loaded here
            mult    $t3, $t3                # multiply $t3 with itself (square), result of multiplication goes to internal registers Hi and Lo
            mfhi    $t8                     # load HI's contents into $t8
            mflo    $t9                     # load LO's contents into $t9
            addi    $t1, $t1, 4             # add 4 to offset (move to next element, since numbers are 32bit)
            jal     adduover                # call adduover to sum the newly found square
            b       squaredSum              # go back to the beginning of the loop

adduover:
            add    $t7, $t7, $t9            # add Lo to $t7
            add    $t6, $t6, $t8            # add Hi to $t6
            blt    $t7, $t9, correction     # if total sum of Lo is lower than Lo before summation (32bit overflow) then jump to label "correction"
            blt    $t6, $t8, overflow       # if total sum of Hi is lower than before summation (64bit overflow) then jump to label "overflow"s
            jr     $ra

correction:
            addi    $t8, $t8, 1             # add 1 to the most significant bits (we made a full circle)
            jr      $ra                     # continue with adduover

overflow:
            li      $v0, 3735928559         # return as result (0x00000000DEADBEEF) in case of 64-bit overflow
            jr      $ra                     # continue with adduover

getRetAddr:
            move    $t5, $ra                # get the return addr. of squareSum's caller, because it gets overwritten during execution
            j       squaredSum              # continue with squaredSum

sumExit:
            move $v0, $t9                   # send least significant 32 bits to $v0 (exercise output)
            move $v1, $t8                   # same as above for most significant

                jr   $t5                        # return to squaredsum's caller's address

对于小数字(即使在大型数组中)的输入,程序按预期工作。我的问题是,当我输入一个大数字(即2000000000)时,在乘法过程中lo寄存器溢出并翻转为负数,这意味着存储在lo中的所有内容都会被签名,而我想要它是无符号的,以便返回0并向上计数。有没有解决方法呢?

感谢您的时间。

PS:仅供参考,我使用MARS来解释代码。

0 个答案:

没有答案