2015-10-27 28 views
1

我目前正在研究一個項目(使用MARS),我需要使用syscall 14來閱讀多行文件。我有一個設置的緩衝區長度,我用於長度爲80個字節的系統調用。我在_readLine函數中設置了循環,以便緩衝區的內容在每次迭代時都應該打印到控制檯。這就是我意識到我的循環只能讀取文件的第一個80 -bytes,而沒有其他。在MIPS中,如何使用系統調用14來讀取長文件?

我一直在搜索互聯網,試圖找到爲什麼我不能讀取文件的下一個80字節的提示。我認爲它可能與文件描述符有關,但是當我嘗試增加文件描述符時,沒有任何改變,它仍然只輸出syscall 14文件的第一個80-bytes。

有人可以幫助我找出什麼是錯的,或給我一個想法,如何指向我的文件的下一個80字節?我的代碼如下(fileBuff包含用戶輸入的文件名的地址,_printBuffer是稍後在我的程序中使用的函數,但您可以忽略)。

main: 
    # Open file for reading 
    addi $v0, $zero, 13 
    la $a0, fileBuff 
    add $a1, $zero, $zero # pass in flags 
    add $a2, $zero, $zero # pass in mode 
    syscall    # open a file (file descriptor returned in $v0) 
    add $s6, $zero, $v0 # store descriptor in $a0 

    jal _readLine # call _readFile function 
    jal _printBuffer 

    addi $v0, $zero, 10 # prepare to exit the program 
    syscall    # exit 

_readLine: 
    readLoop: 
     add $a0, $zero, $s6 # setup file descriptor 
     la $a1, buffer  # address of buffer 
     addi $a2, $zero, 80 # read 80 bytes 
     addi $v0, $zero, 14 # read from the file (descriptor already in $a0, buffer address in $a1, buffer length in $a2) 
     syscall    # write to the file 

     beq $v0, $zero, doneReading 
     slt $t0, $v0, $zero  # if end of file, then close file 
     beq $t0, 1,  doneReading # if error, then close file 

     la $t0, buffer  # load buffer address into $t0 
     add $s6, $zero, $v0 # save file length in $s0 
     add $s6, $s6, $t0 # change descriptor to where last left off in the file 

     #### remove... eventually 
     addi $v0, $zero, 4 
     la $a0, buffer 
     syscall 
     #### 

     j readLoop 

    doneReading: 

    addi $v0, $zero, 16 # syscall to close the file 
    add $a0, $zero, $s6 # file descriptor to close 
    syscall    # close the file 

    jr $ra 

回答

1

系統調用14讀取連續字符串。

當您告訴它讀取80個字節時,它將讀取80個字節。 當你再次調用它(不關閉它)時,它將讀取下一個80字節,依此類推。

所以我建議你移動的代碼塊:

addi $v0, $zero, 16 # syscall to close the file 
add $a0, $zero, $s6 # file descriptor to close 
syscall    # close the file 

到主塊系統退出調用之前。