如何检查文件是否已打开?

时间:2019-05-11 23:06:37

标签: assembly file-io mips

我自己编写了以下代码:

#===========================================
# 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  
#===========================================

如何检查文件是否正确打开?

我应该检查$v0中的非零内容吗?

0 个答案:

没有答案