Trouble tracking return pointer from fopen in MIPS on a ci20 machine

358 views Asked by At

I am currently working on a program that reads in a file and finds the palindromes in the file in MIPS on a ci20 machine. I am having trouble understand where the return pointer is from my jal to my fopen function. Some of my current code

.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

When I run the current program through gdb I get a seg-fault from my fgets function. After stepping through my code it seems that the fopen is returning a 0 in register $v0. Thus the file wasn't opened since fopen return a char* or a NULL if file was not opened. When I then pass the $s0 (What I think is the file pointer) to my fgets it seg-faults.

GDB Results

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

I am not understanding why the fopen is returning the NULL. Besides the obvious answer of because it didn't open the file. Any help or suggestion would be greatly appreciated.

0

There are 0 answers