MIPS汇编:分支似乎不起作用(MARS)

时间:2018-04-22 17:33:22

标签: assembly mips

我正在完成一项任务(此处描述:Overflow problems with summing squares)并且我已经解决了溢出问题。现在的新问题是MARS并不想跟随分支机构,即使满足它们的条件,总是按照MARS。

代码在这里:

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)
        addu    $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
        multu   $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
        addiu   $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
        j       squaredSum              # go back to the beginning of the loop

adduover:
        addu    $t7, $t7, $t9           # add LO to $t7
        addu    $t6, $t6, $t8           # add HI to $t6
        *blt     $t7, $t9, correction*           # if total sum of LO is lower than LO before summation (32-bit overflow) then jump to label "correction"
        *blt     $t6, $t8, overflow*      # if total sum of HI is lower than HI before summation (64-bit overflow) then jump to label "overflow"
        jr      $ra                     # get back to squaredSum

correction:
        addiu   $t6, $t6, 1             # add 1 to the most significant bits (we made a full circle)
        blt     $t6, $t8, overflow      # check once more for 64-bit overflow after the addition
        jr      $ra                     # get back to squaredSum

overflow:
        li      $v0, 3735928559         # return as result (0x00000000DEADBEEF) in case of 64-bit overflow
        j       sumExit                 # jump to sumExit

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, $t7                # send least significant 32 bits to $v0 (exercise output)
        move    $v1, $t6                # same as above for most significant 
        jr  $t5                     # return to squaredSum's caller's address

0 个答案:

没有答案