Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does teq work in mips

Tags:

c++

mips

I compiled this code :

int main()
{
    int x;
    try
    {
        x=2/0;
    }
    catch (...)
    {
        x=1;
    }
    return 0;
}

to mips and here's what I got :

.file   1 "main.cpp"
.section .mdebug.abi32
.previous
.nan    legacy
.gnu_attribute 4, 1
.abicalls
.option pic0
.text
.align  2
.globl  main
.cfi_sections .eh_frame_entry
$LFB6 = .
.cfi_startproc
.set    nomips16
.set    nomicromips
.ent    main
.type   main, @function
main:
.frame  $fp,24,$31      # vars= 8, regs= 1/0, args= 0, gp= 8
.mask   0x40000000,-4
.fmask  0x00000000,0
.set    noreorder
.set    nomacro
addiu   $sp,$sp,-24
.cfi_def_cfa_offset 24
sw  $fp,20($sp)
.cfi_offset 30, -4
move    $fp,$sp
.cfi_def_cfa_register 30
li  $2,2            # 0x2
move    $3,$0
teq $3,$0,7
div $0,$2,$3
mfhi    $2
mflo    $2
sw  $2,8($fp)
move    $2,$0
.cfi_epilogue_begin
move    $sp,$fp
.cfi_def_cfa_register 29
lw  $fp,20($sp)
addiu   $sp,$sp,24
.cfi_restore 30
.cfi_def_cfa_offset 0
j   $31
nop

.set    macro
.set    reorder
.end    main
.cfi_endproc
$LFE6:
.size   main, .-main
.ident  "GCC: (Sourcery CodeBench Lite 2014.11-22) 4.9.1"

I want to ask particularly about the instruction teq, I know it means "trap if equal" so when the dividend is zero it will trap(and it is zero here).

What I don't understand is what happens after trap , I mean where is the catch block in this assembly file ? also when searching the web I see different things like coprocessor 0 ,exception code register ...etc.

So what's happening here ?

like image 785
niceman Avatar asked Mar 11 '26 21:03

niceman


1 Answers

The teq instruction is how the compiler has chosen to handle the undefined behavior that results from dividing by zero; there's no indication that dividing by zero triggers an exception that a catch block would handle.

You then wonder where the catch block appears in the listing. It doesn't. There is nothing representing the x=1 statement, probably because the assignment is never used afterward. The div instruction is another example of this. It stores the result in register $0, which is equivalent to discarding the result.

If you want to explore how exceptions are handled, use a throw statement.

In MIPS, there's a concept of an exception, but it's distinct from the C++ notion of exceptions. When the first two arguments to teq are equal, the CPU will signal a trap exception and "transfer control to a software exception handler in the kernel," according to the MIPS IV instruction-set manual. The kernel will probably print a message and terminate the program unless the program has asked the kernel to do something else instead. (The runtime library of your compiler might provide something to control the handling of CPU exceptions already, perhaps even converting them into C++ exceptions that can be caught in a catch block.)

The third argument to teq has no inherent meaning to the CPU, but the exception handler might know to treat 7 as an indicator of integer division by zero. (Floating-point division by zero is handled differently and doesn't necessarily trap.)

like image 150
Rob Kennedy Avatar answered Mar 14 '26 11:03

Rob Kennedy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!