Sunday, April 5, 2020

MIPS register - register addition overflow (add/addu)

This is sample MIPS assembler code demonstrating register - register addition overflow. It detects addition overflow using:
a) MIPS assembler code, the basis of which is the addu & xor instructions
b) MIPS hardware overflow simulation supported by EzMIPS, the MIPS assembler editor & simulator. The code is fully commented.

Launch EzMIPS, copy the following MIPS code and paste it into EzMIPS. Assemble, Run.

# ----------- Register - register addition overflow -------------- #

.data

szMenu:         .ascii  "How to detect register addition overflow?\n"
                .ascii  "-----------------------------------------\n"
                .ascii  "1. Runtime exception: arithmetic overflow\n"
                .ascii  "2. MIPS Code\n"
                .asciiz "3. Exit\n"

szNoOverflow:   .asciiz "No Overflow!\n"
szOverflow:     .asciiz "Overflow!\n"

.text

main:

# ...... Test the addition overflow of 2 positive numbers ........ #


    li $t4, 0x7fffffff    # 2147483647
    li $t5, 0x1           #          1
    
    # OVERFLOW!!!
    # 0x7fffffff + 0x00000001 = 0x80000000
    # 2147483647 + 1          = -2147483648

# ... Uncomment to test addition overflow of 2 negative numbers .. #

#   li $t4, 0x80000000    # -2147483648
#   li $t5, 0xffffffff    #          -1

    # OVERFLOW!!!
    # 0x80000000  + 0xffffffff = 0x7fffffff
    # -2147483648 + -1         = 2147483647

# ................................................................ #
    
    li $t1, 1
    li $t2, 2
    li $t3, 3

# ................................................................ #

print_menu:

    # print menu
    li $v0, 4
    la $a0, szMenu
    syscall

# ................................................................ #
    
    # get user input
    li $v0, 5
    syscall
    # $v0 holds user input

    beq $v0, $t2, no_trap
    beq $v0, $t3, exit
    bne $v0, $t1, print_menu

# ................................................................ #
    
    # Runtime exception: arithmetic overflow
    add $t0, $t4, $t5   # addition with Overflow Runtime exception
    j print_menu        # if no runtime exception occured, print menu

# ................................................................ #

no_trap:

    #addition with NO overflow detection
    addu $t0, $t4, $t5           # Don't trap!

    # Let's see with MIPS code if overflow occurs
    xor $t6, $t4, $t5            # if signs differ $t6 < 0
    slt $t6, $t6, $zero
    bne $t6, $zero, no_overflow  # signs differ

    xor $t6, $t0, $t4            # signs match, check result
    slt $t6, $t6, $zero          # sum sign different
    bne $t6, $zero, overflow

# ................................................................ #

no_overflow:

    li $v0, 4
    la $a0, szNoOverflow
    syscall

    j print_menu

# ................................................................ #

overflow:

    li $v0, 4
    la $a0, szOverflow
    syscall

    j print_menu

# ................................................................ #

exit:

    li $v0, 10
    syscall

# ---------------------------------------------------------------- #

Please let me know of any suggestions or bugs regarding the code above.

Regards,

Antonis

No comments:

Post a Comment