Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading files with MIPS assembly

I'm trying to write a program that reads in characters from a .dat file that correspond to different colors to be displayed in the LED simulator; x = off, R = red, etc. My problem is, I cannot figure out what I'm doing wrong with opening the .dat file. I've looked around and tried all I can think of, but each time I assemble and run, I get a -1 in $v0 signifying an error. Here's my code for opening/reading/closing the file:

.data  
fin: .asciiz "maze1.dat"      # filename for input
buffer: .asciiz ""

.text
#open a file for writing
li   $v0, 13       # system call for open file
la   $a0, fin      # board file name
li   $a1, 0        # Open for reading
li   $a2, 0
syscall            # open a file (file descriptor returned in $v0)
move $s6, $v0      # save the file descriptor 

#read from file
li   $v0, 14       # system call for read from file
move $a0, $s6      # file descriptor 
la   $a1, buffer   # address of buffer to which to read
li   $a2, 1024     # hardcoded buffer length
syscall            # read from file

# Close the file 
li   $v0, 16       # system call for close file
move $a0, $s6      # file descriptor to close
syscall            # close file

The file maze1.dat is in the same directory as the MIPS program. Any help or suggestions are greatly appreciated.

like image 760
CJ McAllister Avatar asked Nov 10 '10 18:11

CJ McAllister


2 Answers

The only issue is your buffer is simply an empty string, which is only reserving one byte (null byte). You should instead use buffer: .space 1024 or however many bytes you need. Everything else seems fine.

If you are having trouble opening the file, make sure the extension is exactly correct. But my test just worked a .dat file and a few random text files.

like image 58
Kizaru Avatar answered Oct 12 '22 21:10

Kizaru


Make sure the you are running MARS from the same directory the file is located. Simply move the MARS .jar to the directory containing "maze1.dat" and run it from there.

like image 41
David Zaragoza Avatar answered Oct 12 '22 22:10

David Zaragoza