# MIPS arithmetic overflow



## nsr1 (Jul 18, 2009)

Hi,

Could you please explain why I am getting an arithmetic overflow error with this code:

```
.data
  str: .asciiz "The value of $s0 is: "
  
.text
main:                # Start of code section

    li $a0, 44          
    li $a1, 10          

    jal compare

    li $v0,4       #print string
    la $a0, str
    syscall   

    li $v0,1      #print the value of $s0
    move $a0, $s0
    syscall
compare:
    addi $sp, $sp, -4 # allocate frame = 4 bytes
    sw $s0, 0($sp) 
    jal sub 
    li $t0, 0 
    bltz $v0, exit 
    li $t0, 1 
exit:
    move $v0, $t0 
    lw $s0, 0($sp)
    addi $sp, $sp, 4
    jr $ra 
sub:
    sub $v0, $a0, $a1 
    jr $ra
```
Error: Error in C:\Users\****\Downloads\2-19-1a.asm line 29: Runtime exception at 0x00400048: arithmetic overflow.

Thanks.


----------



## allnodcoms (Jun 30, 2007)

I've not worked in assembler for years, around twenty to be exact, it was MC68K on the Atari and early Motorola Macs... Happy days! Anyway, back on the farm...

I'm guessing it's an error in your _compare:_ sub. Is 'bltz' a call? It sounds like 'branch less than zero', but I'm sure I used 'blt' (branch less than) with 0 as an operand. It's been a long time though.

Secondly, if your 'blt' call is true you skip to _exit:_ and if not it just falls through, but because you don't 'jr' (jump return) before the exit sub it gets executed anyway, so exit is called wether the contents of $v0 is <0 or not. Is this what you want?

I'd try:

```
bltz $v0, exit #change this to
blt $v0, 0, exit
```
and add

```
jr $ra
```
just before your _exit:_ label. At the end of the day though, if you're just trying to compare two numbers then there are way easier ways of doing it than this... 

I need to go get my assembler out now... Feel the need to hack some bits.

Danny


----------

