Reading from a binary file in MIPS

1.8k views Asked by At

I have a PGM photo which I'm trying to open and read in MIPS. When the values inside the PGM file are ASCII, everything works as expected. When they are in HEX format, the syscall 14 (read) reads just couple of values and than stops and I end up with only part of the file in the buffer.

To the left HEX - not working, to the right ASCII - working. Same file To the left HEX - not working, to the right ASCII - working. Same file.

Thats my code (the buffer is declared in data section and has enough space to allocate the file)

#open a file
    li $v0, 13 #Syscall for loading files
    la $a0, image_file #Saving the address to $a0
    li   $a1, 0       # flag for reading
    li   $a2, 0       # mode is ignored
    syscall
    move $s6, $v0 #Placing the descriptor in $s6 for later-use  

#read from file
    li $v0, 14 #Syscall to read from files
    move $a0, $s6 #Moving descriptor to $a0
    la   $a1, buffer   # address of buffer to which to read
    li   $a2, 65555     # hardcoded buffer length
    syscall
1

There are 1 answers

0
boozi On BEST ANSWER

The answer is that its actually reading the whole file, the console just won't show everything because of the EOF sign. Once saving the file you will need to specify again the exact length of the file so it will be able to save everything and not just the data until the EOF sign.