Saturday, April 18, 2020

MIPS file manipulation and runtime exception tests

This is MIPS assembler code that demonstrates file manipulation (open, close read, write), read from and write to memory. EzMIPS handles all cases with care and raises Runtime Exception when appropriate. EzMIPS should never crash or hang.

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

# -------- file manipulation and runtime exception tests --------- #

.data

wTest:         .word   0x12345678
szValidFile:   .asciiz "c:/test.txt"
szInvalidFile: .asciiz "c:\\Users\\Antonis.ANTONIS-PC\\Desktop\\invalid.txt"
szSpace:       .space  2000

.text

main:

# ........................... Open file .......................... #
    
    la $a0, szValidFile    # if $a0 = address of valid null-terminated filename string
                           # $v0 = valid FILE pointer after the following syscall

   #la $a0, szInvalidFile  # if $a0 = address of invalid null-terminated filename string
                           # $v0 = -1 after the following syscall

    li $a1, 0              # $a1 = flag = 0 -> "r"
    li $v0, 13
    syscall
    
    move $a3, $v0          # Keep file descriptor safe!

# ................. read file text and put it in buffer .......... #

    move $a0, $v0       # if $a0 = valid file descriptor
                        # $v0 = number of bytes read after the following syscall
                        # if $a0 = invalid file descriptor, $v0 = -1 after the following syscall

    li $v0, 14          # read from file
    la $a1, szSpace     # adrress of buffer
    li $a2, 200         # number of bytes to be read
    syscall

# .......................... close file .......................... #

    # $a0 = file descriptor
    move $a0, $a3     # if $a0 = valid file descriptor -> $v0 = 0 after the following syscall
                      # if $a0 = invalid file descriptor -> $v0 = -1 after the following syscall
    li $v0, 0x10      # = 16
    syscall

# ........................... Open file .......................... #

    la $a0, szInvalidFile
    li $a1, 1              # $a1 = flag = 1 -> "w"
    li $v0, 13
    syscall
    move $a3, $v0          # Keep file descriptor safe!

# ......................... Write to file ........................ #

    move $a0, $v0         # $a0 = file descriptor
    la $a1, szSpace       # $a1 = address of output buffer
    li $a2, 100           # $a2 = number of characters to write
    li $v0, 15
    syscall
    # return: $v0 contains number of characters written (-1 if error)

# .......................... close file .......................... #

    # $a0 = file descriptor
    move $a0, $a3     # if $a0 = valid file descriptor -> $v0 = 0 after the following syscall
                      # if $a0 = invalid file descriptor -> $v0 = -1 after the following syscall

    li $v0, 0x10      # = 16
    syscall

# ............................. jr test .......................... #

    #jr $ra

# ............................ swr test .......................... #
    
    #la $t1, szValidFile
    #la $t1, wTest
    #addi $t1, $t1, -1
    swr $a0, 3($t1)
    swr $a1, 2($t1)
    swr $a2, 1($t1)
    swr $a3, 0($t1)

# ............................ swl test .......................... #
    
    #la $t1, szValidFile
    #la $t1, wTest
    #addi $t1, $t1, -1
    swl $a0, 0($t1)
    swl $a1, 1($t1)
    swl $a2, 2($t1)
    swl $a3, 3($t1)

# ............................ lwr test .......................... #
    
    la $t1, szValidFile
    #la $t1, wTest
    addi $t1, $t1, -1
    lwr $a0, 0($t1)
    lwr $a1, 1($t1)
    lwr $a2, 2($t1)
    lwr $a3, 3($t1)

# ........................... lhu test ........................... #
    
    #la $t1, szValidFile
    #la $t1, wTest
    #addi $t1, $t1, 1
    lhu $a0, 0($t1)
    lhu $a2, 2($t1)

# .......................... lbu test ............................ #
    
    #la $t1, szValidFile
    #la $t1, wTest
    #addi $t1, $t1, -1
    lbu $a0, 0($t1)
    lbu $a1, 1($t1)
    lbu $a2, 2($t1)
    lbu $a3, 3($t1)

# ............................ lw test ........................... #
    
    #la $t1, szValidFile
    #la $t1, wTest
    #addi $t1, $t1, -1
    lw $a0, 0($t1)
    lw $a1, 1($t1)
    lw $a2, 2($t1)
    lw $a3, 3($t1)


# ............................ lwl test .......................... #
    
    #la $t1, szValidFile
    #la $t1, wTest
    #addi $t1, $t1, -1
    lwl $a0, 0($t1)
    lwl $a1, 1($t1)
    lwl $a2, 2($t1)
    lwl $a3, 3($t1)

# ............................ lh test ........................... #
    
    #la $t1, szValidFile
    #la $t1, wTest
    lh $a0, 0($t1)

# ............................ lb test ........................... #
    
    #la $t1, szValidFile
    #la $t1, wTest
    lb $a0, 0($t1)

# ......................... Write to file ........................ #
    
    li $a0, 0              # $a0 = file descriptor
    li $a1, 0              # $a1 = address of output buffer
    la $a1, szValidFile    # $a1 = address of output buffer
    li $a2, 10             # $a2 = number of characters to write
    li $v0, 15
    syscall
    # return: $v0 contains number of characters written (-1 if error)

# .......................... Open file ........................... #
    
    # Runtime exception at 0x--------: cannot read directly from this address <0x00000000>
    li $a0, 0    # $a0 = address of null-terminated filename string
    li $a1, 0    # $a1 = flag = 0 -> "r"
    li $v0, 13
    syscall

# ........................ read integer .......................... #
    
    # Runtime exception at 0x--------: invalid integer input (syscall 5)
    # if invalid integer given by the user
    li $v0, 5
    syscall

# ....................... print string ........................... #
    
    # Runtime exception at 0x--------: cannot read directly from this address <0x00000000>
    li $a0, 0
    li $v0, 4
    syscall

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

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

Regards,

Antonis

No comments:

Post a Comment