从 MIPS 中的二进制文件读取

Reading from a binary file in MIPS

我有一张 PGM 照片,我正尝试在 MIPS 中打开和阅读。 当 PGM 文件中的值为 ASCII 时,一切都按预期工作。当它们是 HEX 格式时,系统调用 14(读取)只读取几个值然后停止,我最终只在缓冲区中 部分文件。

左边的 HEX - 不工作,右边的 ASCII - 工作。同一个文件。

这就是我的代码(缓冲区在数据部分声明并且有足够的 space 来分配文件)

#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

答案是它实际上读取了整个文件,控制台只是因为 EOF 标志而不会显示所有内容。保存文件后,您需要再次指定文件的确切长度,以便它能够保存所有内容,而不仅仅是 EOF 符号之前的数据。