Opening file always returns "-1" in QtSpim

1.1k views Asked by At

I'm working on a university project where we have to write Data to a file via MIPS. I get everything to run on MARS, but on QtSpim, just opening a file always returns an error:

.data
file_name: .asciiz "test.pgm"

#
# main
#

.text
.globl main

main:
    # Open File in write mode
    li    $v0, 13           # $v0 = 13, option for opening file
    la    $a0, file_name    # $a0 = &file_name
    li    $a1, 1            # $a1 = 1, write-flag
    li    $a2, 0            # $a2 = 0, mode is ignored
    syscall                 # open File, save descriptor to $v0

    jr $ra                  # return

When I run this in QtSpim step by step I get a -1 in $v0 after the syscall. Any ideas, why QtSpim can't run this? I'm on Windows 10, QtSpim version 9.1.17, although the same version on LinuxMint 18 has the same problem. Any help appreciated, for now I'll just run everything on MARS, but we have to hand in something that works on QtSpim.


UPDATE

It seems to work when I use absolute paths, but the file must exist in order to open it in write mode. Is there a way to create the file if it doesn't exist?

2

There are 2 answers

0
Aram Becker On BEST ANSWER

Well, after a lot of trial and error, it seems to work when I open the file with Flag 0x41 and mode 0x1FF.

If I understand it correctly, 0x41 is for write with create and 0x1FF for the correct permissions.

main:
    # Open File in write mode
    li    $v0, 13           # $v0 = 13, option for opening file
    la    $a0, file_name    # $a0 = &file_name
    li    $a1, 0x41         # $a1 = 0x41, write-flag with create
    li    $a2, 0x1FF        # $a2 = 0, permissions
    syscall                 # open File, save descriptor to $v0

    jr $ra                  # return

And absolute paths, apparently QtSpim can't even write into it's own directory when started as admin, so really only absolute paths work.

0
314 On

Relative paths in QtSpim are based on the directory it is executed from, which may be a different one than the location of your code or the image.