Tuesday, April 7, 2020

MIPS div and divu instructions

This is sample MIPS assembler code demonstrating the correct operation of the div and divu instructions 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.

# --------------- div, divu division instructions ---------------- #
.data

szMenu:         .ascii  "Choose division type\n"
                .ascii  "--------------------------\n"
                .ascii  "1. Use div instruction\n"
                .ascii  "2. Use divu instruction\n"
                .asciiz "3. Exit\n"

.text

main:

# ................................................................ #
# Comment any 2 lines for $t5 and any 2 lines for $t6 and check result of
# div and divu instructions. Any combination should give appropriate results! 

    li $t5, 0xffffffff
#   li $t5, 0x7fffffff
#   li $t5, 0x80000000

#   li $t6, 0xffffffff
#   li $t6, 0x7fffffff
    li $t6, 0x80000000

# ................................................................ #
    
    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, $t3, exit
    beq $v0, $t2, use_divu
    bne $v0, $t1, print_menu

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

use_div:
    div $t5, $t6
    j print_menu         # Display menu again

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

use_divu:
    divu $t5, $t6
    j print_menu         # Display menu again

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

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