Tuesday, May 5, 2020

MIPS Runtime Exception when writing to memory

This is MIPS assembler code that demonstrates Runtime Exception handling when trying to write to an invalid memory address as supported by EzMIPS v0.9.1.5. EzMIPS handles all cases with care, raises Runtime Exception and exits MIPS program gracefully. EzMIPS should never crash or hang.

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

# Runtime Exception handling for writing to invalid memory address v0.9.1.5 #

.data

szFileName: .asciiz "test.txt"
buff:       .space 1000

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

.text

main:
    li $t8, 0x100

    swr $t7, 4($t8)
    # Runtime exception at 0x--------: cannot write directly to this address <0x00000104>
    swl $t7, 4($t8)
    # Runtime exception at 0x--------: cannot write directly to this address <0x00000104>
    sw $t7, 4($t8)
    # Runtime exception at 0x--------: cannot write directly to this address <0x00000104>
    sh $t7, 4($t8)
    # Runtime exception at 0x--------: cannot write directly to this address <0x00000104>
    sb $t7, 4($t8)
    # Runtime exception at 0x--------: cannot write directly to this address <0x00000104>

# ........................ open file ............................. #

# $a0 = address of null-terminated filename string, $a1 = flags, $a2 = mode */
# return: $v0 contains file descriptor (negative if error) */
    
    la $a0, szFileName
    li $a1, 0                   # for read-only
    li $v0, 13
    syscall
    # $v0 contains file descriptor (negative if error)

# ........................ read from file ........................ #

# $a0 = file descriptor, $a1 = address of input buffer, $a2 = maximum number of characters to read,
# return: $v0 contains number of characters read (0 if end-of-file, negative if error).
    
    move $a0, $v0
    #la $a1, buff
    li $a2, 100                # read max 100 chars
    li $v0, 14
    syscall
    # Runtime exception at 0x--------: cannot write directly to this address <0x00000000>

# .............. put string given by user into buffer ............ #

    li $a0, 0                   # $a0 = address of input buffer
    li $a1, 100                 # 100 bytes
    li $v0, 8                   # read string into buffer
    syscall                     # do it!

    # Runtime exception at 0x--------: cannot write directly to this address <0x00000000>

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

out:
    sll $zero, $zero, 0

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

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

Regards,

Antonis

No comments:

Post a Comment