Trouble writing from a MIPS program to a .txt file

315 views Asked by At

I am simply trying to write the string "It Works!" into the file "myFile.txt". Both file_work.s and myFile.txt are in the same directory.

I am using SPIM.

I have tried saving the file name in .data as an absolute path with no success.

# 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

There are 0 answers