从MIPS程序写入.txt文件时遇到问题

时间:2020-10-20 18:07:40

标签: assembly mips spim

我只是想写字符串“ It Works!”。进入文件“ myFile.txt”。 file_work.s和myFile.txt都在同一目录中。

我正在使用SPIM。

我尝试将.data中的文件名保存为绝对路径,但没有成功。

# file name: file_work.s
# 

.data
fout:   .asciiz "myFile.txt"      # filename for output
buffer: .asciiz "It Works!"

.text
main:
  # Open (for reading) a file
  li $v0, 13       # system call for open file
  la $a0, fout     # output file name
  li $a1, 0        # flags
  syscall          # open a file (file descriptor returned in $v0)

  move $t0, $v0    # save file descriptor in $t0

  # Write to file just opened
  li $v0, 15       # system call for read to file
  la $a1, buffer   # address of buffer from which to write
  li $a2, 32       # hardcoded buffer length
  move $a0, $t0    # put the file descriptor in $a0
  syscall          # write to file

  la $a0, buffer #load the address into $a0
  li $v0, 4     # print the string out
  syscall

  # Close the file
  li $v0, 16       # system call for close file
  move $a0, $t0    # Restore fd
  syscall          # close file

  li $v0, 10        # end the file
  syscall

0 个答案:

没有答案
相关问题