0

我目前正在开发一个读取文件并在机器MIPS上找到文件中的回文的程序ci20。我无法理解返回指针从我jal到我的fopen函数的位置。我当前的一些代码

.option pic0

.rdata
.align 2

    fileName: 
       .asciz "./words" 
       .align 2

    fileMode:
       .asciz "r"
       .align 2

    buffer:
       .asciz ""
       .space 64 # space for the string

    .global palindrome_fgets
palindrome_fgets:
la $a0, buffer # Passing string*?
la $a1, 64 # size of the buffer
la $a2, ($s0)  # address of the file poiner

addiu $sp, $sp, -4 # create space on the stack for $sp
sw $ra, ($sp)      # save $sp on the stack

jal fgets          #char* fgets (char* str, int num, FILE* stream);
lw $ra, ($sp)
addiu $sp, $sp, +4

jr $ra    

    .global palindrome_fopen
palindrome_fopen:
la $a0, fileName
la $a1, fileMode

addiu $sp, $sp, -4 # create space on the stack for $sp
sw $ra, ($sp)      # save $sp on the stack

jal fopen          # FILE* fopen (const char* filename, const char* mode);
lw $ra, ($sp)
addiu $sp, $sp, +4

jr $ra

.global main
 main:

jal palindrome_fopen # return file pointer is in $v0?

la $s0, ($v0)  # loading returned file pointer to $s0?
               # Would the pointer be in $fp
jal palindrome_fgets

li      $a0, 0
jal     exit

当我运行当前程序时gdb,我的fgets函数出现了段错误。单步执行我的代码后,似乎fopen正在返回一个0in register $v0。因此,由于fopen返回 achar*NULLif 文件未打开,因此文件未打开。然后当我将$s0(我认为是文件指针)传递给我的fgets它的段错误时。

GDB 结果

    Program received signal SIGSEGV, Segmentation fault.
    _IO_fgets (buf=0x400a00 "", n=64, fp=0x0) at iofgets.c:50

我不明白为什么fopen返回NULL。除了因为它没有打开文件的明显答案。任何帮助或建议将不胜感激。

4

0 回答 0