How can I check if a file was opened or not?

2019-08-20 08:56发布

问题:

I have written the following code myself:

#===========================================
# OpenFile - opens a file for reading
# arguments: 
#   $a0 - file name
# return:
#   $v0 - file's descriptop/pointer
#-------------------------------------------
OpenFile:
    # backup return address
    addiu $sp, $sp, -12 #  create space for 3 words
               # (4*3=12 bytes) on the stack 
               # (push) for $ra            
    sw $ra, 0($sp) # backup return address $ra  
    # protect arguments from change
    sw $a1, 4($sp) # protect string address
    sw $a2, 8($sp) # protect char 

    # actual file open code
    li   $a1, 0        # Open for reading
    li   $a2, 0
    li   $v0, 13       # system call for open file
    syscall            # open a file (file descriptor returned in $v0)

    # restore registers
    lw $ra, 0($sp) # restore $ra
    lw $a1, 4($sp) # load address counter
    lw $a2, 8($sp) # load char to be replaced

    # restore stack pointer     
    addiu $sp, $sp, 12 # return the space on the stack(pop)

    # return 
    jr $ra  
#===========================================

How can I check if the file is properly opened or not?

Should I check for a non-zero content in $v0?