【问题标题】:Where to put a .txt file for MARS在哪里放置 MARS 的 .txt 文件
【发布时间】:2012-11-30 02:15:41
【问题描述】:

我正在使用 MARS 程序编写一些 MIPS 汇编代码,我正在编写的程序需要接收一个输入文件,然后遍历它以更改一些数字。我已经编写了所有代码主体,但我不确定如何实际接收文件。我有以下代码读取输入并存储地址:

.data 0x0
magicNum:       .asciiz "P2"  #magic number
zero:   .word 0
newLine:        .asciiz "\n"  #new line character

.text 0x3000

main:
        ori $v0, $0, 8          #8 is syscall to read string
        ori $a0, $0, 100        #stores address of input buffer
        ori $a1, $0, 3          #max character to read in
        syscall

#the rest of the code is down here

但是我实际上应该将文件放在 Windows 上的哪个位置以获取它呢?

【问题讨论】:

  • 现在我只是使用多个系统调用来一次输入一行

标签: assembly input io mips mips32


【解决方案1】:

您必须使用 syscall 13 打开文件,然后使用 syscall 14 读取文件并将其内容存储到缓冲区中。

这是一个帮助您入门的 sn-p,只需用您的代码填补空白:

.data
filename: .asciiz "file.txt"
buffer: .space 1024

.text

    la $a0, filename
    li $a1, 0       # readonly
    li $a2, 0
    li $v0, 13
    syscall         # open file
    bltz $v0, file_error
    move $a0, $v0    
    la $a1, buffer
    li $a2, 1024
read_file:
    li $v0, 14
    syscall
    beqz $v0, read_done
    bltz $v0, read_error
    addu $a1, $a1, $v0   # adjust buffer pointer
    subu $a2, $a2, $v0
    bnez $a2, read_file   # If buffer not full and not EOF, continue reading
read_done:
   # File copied to buffer
   # Your code goes here

file_error:
   # Code to take action if file errors occur (e.g. file not found)

read_error: 
   # Code to take action if read errors occur

如果您使用 MARS,该文件应位于当前目录(您启动 MARS 的位置)中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 2017-11-19
    • 2013-05-24
    相关资源
    最近更新 更多