2021年5月7日星期五

mips-g++-5.4 listing bnez skips div instruction

I'm trying to understand why mips-g++ compiles a simple division subroutine where it skips the actual div instruction with a bnez v0, <jmp> (pseudo inst for bne). My understanding is that if the divisor is zero it makes sense to skip div and trap or break in this case. Why should it branch if the divisor is not zero?

I would like to learn what I am missing.

subroutine

void do_div(int &a, int &b, int &result){      result = a/b;  }  

assembly listing:

00000000 <_Z6do_divRiS_S_>:     0:   27bdfff8    addiu   sp,sp,-8     4:   afbe0004    sw  s8,4(sp)     8:   03a0f025    move    s8,sp     c:   afc40008    sw  a0,8(s8)    10:   afc5000c    sw  a1,12(s8)    14:   afc60010    sw  a2,16(s8)    18:   8fc20008    lw  v0,8(s8)    1c:   00000000    nop    20:   8c430000    lw  v1,0(v0)    24:   8fc2000c    lw  v0,12(s8)    28:   00000000    nop    2c:   8c420000    lw  v0,0(v0)    30:   00000000    nop    34:   14400002    bnez    v0,40 <_Z6do_divRiS_S_+0x40>    38:   0062001a    div zero,v1,v0    3c:   0007000d    break   0x7    40:   00001010    mfhi    v0    44:   00001812    mflo    v1    48:   8fc20010    lw  v0,16(s8)    4c:   00000000    nop    50:   ac430000    sw  v1,0(v0)    54:   00000000    nop    58:   03c0e825    move    sp,s8    5c:   8fbe0004    lw  s8,4(sp)    60:   27bd0008    addiu   sp,sp,8    64:   03e00008    jr  ra    68:   00000000    nop    
https://stackoverflow.com/questions/67443291/mips-g-5-4-listing-bnez-skips-div-instruction May 08, 2021 at 09:28AM

没有评论:

发表评论