- #1
JAO012
- 3
- 0
Homework Statement
Write a PC SPIM program that can (1) Prompt for a complete file name. [Note: Probably should be in the same directory as the program.] (2) Echo the input. (3) Test to see if a file by that name already exists. If it does, query the user to see if destruction of the old file is desired. If so, continue to the next step. Otherwise, give the user another chance to create a new file with a different name. (4) If the file does not exist or the user approves of destroying the old file, create it and put the name of the file in it and echo it.
The area commented out in the middle is the area I am having trouble with. First of all, I am not sure what to compare $a0 with to determine if a file name already exists, as I have denoted in the comments of my code. Second of all, I am not sure I even have the right idea to combat this problem, or if my approach is feasible. Right now, when I run the program, I get "A file by that name already exists. Would you like to overwrite it? (Y/N)" Then I do not input Y or N and the program continues with "Invalid entry. Please try again." Then it goes back to "A file by that name already exists. Would you like to overwrite it? (Y/N) Invalid entry. Please try again." repeatedly until the program is terminated. How can I debug my program so that I can get it running correctly?
Homework Equations
The Attempt at a Solution
My code thus far:
.data
ibuffer: .space 80
obuffer: .space 80
prompt: .asciiz "Enter full file name: "
oerrmsg: .asciiz "Error opening file"
werrmsg: .asciiz "Error writing file"
rerrmsg: .asciiz "Error reading file"
alert: .asciiz "A file by that name already exists."
alert_q: .asciiz "\nWould you like to overwrite it? (Y/N)"
store: .space 4
invalid: .asciiz "Invalid entry. Please try again."
LF: .asciiz "\n"
.text
main:
#prompt for and get file name
li $v0,4
la $a0,prompt
syscall
li $v0,8
la $a0,ibuffer
li $a1,80
syscall #file name & LF now in ibuffer
#remove LF
la $a0,ibuffer # remove LF
add $a0,$a0,79 # remove LF
rLFloop:
lb $v0,0($a0) # remove LF
bnez $v0,rLFdone# remove LF
sub $a0,$a0,1 # remove LF
j rLFloop
#change LF to zero
rLFdone:
sb $0,0($a0) # file name now in ibuffer
##################################################################################################################################
checkname: #checks to see if file name already exists
la $a0,ibuffer
beq $X,$a0,next #check ibuffer against other file names
#not sure what to compare $a0 too, so $X
next: #a file by that name already exists
li $v0,4
la $a0,alert #alerts that the file name is the same
syscall
li $v0,4
la $a0,alert_q #asks if user would like to try again
syscall
li $v0,8
la $a0,store #stores the word Y,y or N,n in store temporarily
beq $a0,0x59,cont #if $a0 is equal to Y cont
beq $a0,0x79,cont #if $a0 is equal to y cont
beq $a0,0x4E,term #if $a0 is equal to N term
beq $a0,0x6E,term #if $a0 is equal to n term
li $v0,4 #if it is not Y,y,N,n tell the user the input was invalid and prompt them to try again
la $a0,invalid
syscall
j checkname
cont: #continue with program name and overwrite
j fileopen
term: #terminate; go back to chose file name
j main
#########################################################################################################################################
#file open (create)
fileopen:
li $v0,13 # file open function code
la $a0,ibuffer # ->string with full path of file
li $a1, 0x4102
#flags: Text=0x4000 OR Create=0x100 OR R/W=0x2
#$a2,0x180
#permissions: Read=0x100 OR Write=0x80
syscall #returns file descriptor or -1 if error
beq $v0,-1,oerror
move $s0, $v0 #good file descriptor in $v0
#file_write
li $v0,15 #file write function code
move $a0,$s0 #file descriptor
la $a1,ibuffer #address of buffer
li $a2,80 #amount to write
syscall #returns amount written or -1 if error (0=EOF)
beq $v0,-1,werror
#close/reopen for reading (since file pointer at EOF)
li $v0,16 #file close function code
move $a0,$s0 #file descriptor
syscall #closed, now reopen (file ptr at beginning)
li $v0,13 #file open function code
la $a0,ibuffer # -> string with full path of file
li $a1,0x4000 # flags: Text=0x4000 OR Read=0x0
li $a2,0x100 #permissions: Read=0x100
syscall #returns file descriptor or -1 if error
beq $v0,-1,oerror
move $s0,$v0 #good file descriptor in $s0
#file_read
li $v0,14 #file read function code
move $a0,$s0 #file descriptor
la $a1,obuffer #address of buffer
li $a2,80 #amount to read
syscall #returns amount read or -1 if error (0=EOF)
beq $v0,-1,rerror
#echo - echo the file name
li $v0,4
la $a0, LF
syscall
li $v0,4
la $a0,ibuffer #write from ibuffer
syscall
li $v0,4
la $a0,obuffer
syscall #write from obuffer
#file_close
li $v0,16 #file close function code
move $a0,$s0 #file descriptor
syscall #file closed
exit:
li $v0,10
syscall
oerror: #open error
li $v0,4
la $a0,oerrmsg
syscall
j exit
werror: #write error
li $v0,4
la $a0,werrmsg
syscall
j exit
rerror: #read error
li $v0,4
la $a0,rerrmsg
syscall
j exit