user13077682
user13077682

Reputation:

Open and read file in mips

I'm trying to open and read a file in mips but doesnt show anything. I have the file in same directory as mars.jar and I think the code is right. I tried everything and searched and still it doesnt work. Please help me I really need to deliver this program and I dont know what to do. This is my code:

.data
bin: .asciiz "hundredints.bin"
size: .space 4096
.text
li $v0, 13
la $a0, bin
li $a1, 0
li $a2, 0
syscall
move $s0, $v0

li $v0, 14
move $a0, $s0
la $a1, size
li $a2, 4096
syscall

li $v0, 4
la $a0, size
syscall

li $v0, 16
move $a0, $s0
syscall

PS: sorry for my english

Upvotes: 1

Views: 2978

Answers (1)

Payas Krishna
Payas Krishna

Reputation: 123

Your code is correct. However, according to MARS syscall specifications, on issuing a syscall for opening a file, if $v0 gets a negative value, it means there was a problem in opening the file.

You can verify that by assembling the code in MARS, putting a breakpoint on

move $s0, $v0

and inspecting the contents of $s0 after this line executes (or alternatively print $s0 as an integer using syscall 1 in $v0).

If this is negative, (most likely in your case?) - it means the file couldn't be opened by the JAVA process that is MARS.

The above link also says "The underlying file I/O implementation uses java.io.FileInputStream.read() to read and java.io.FileOutputStream.write() to write."

Note that you are trying to open a relative path "hundredints.bin" (relative to the MARS Java process's home directory). Hence, JAVA process MARS should run from the same directory as your code and file that you are trying to open.

You can do this by issuing the following in your code-and-file directory (MAC or Linux, assuming java is installed!)-

java -jar Mars.jar

(Or an equivalent arrangement in Windows).

Verifying if the MARS JAVA process's home is indeed your code's home-

Just 'open' a file in MARS. It should directly show the contents of your directory. :)

I've tested this in Ubuntu. Hope this helps!

Upvotes: 2

Related Questions