MIPS programming file I/O problem

Click For Summary
The discussion revolves around a MIPS programming assignment that requires creating a file I/O program in PC SPIM. The main issues highlighted include difficulties in checking if a file name already exists and handling user input for overwriting files. The user experiences a loop of invalid entry messages when attempting to input 'Y' or 'N' for file overwrite confirmation. Suggestions for debugging include using the QtSpim or MARS simulators to step through the code and monitor register changes. Effective debugging techniques are emphasized to resolve the persistent input validation issue.
JAO012
Messages
3
Reaction score
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
 
Physics news on Phys.org
PCSpim and the newer QtSpim that replaces it, have a debugger that you can use to single-step through your code. Set a breakpoint at a location just before where you're having the problem. As you single-step through the code you can see how the various registers change, and that should give you some insight into why the problem is occurring.

The most recent version of QtSpim is 9.1.7, available at http://sourceforge.net/projects/spimsimulator/files/.
 
For debugging, I always liked using MARS. Google for MARS MIPS simulator. If I remember correctly, you can change its register display to show base 10, which is often a more natural viewpoint when debugging.
 
RoshanBBQ said:
If I remember correctly, you can change its register display to show base 10, which is often a more natural viewpoint when debugging.
If you do much assembly programming, hex tends to become the more natural mode of display.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 22 ·
Replies
22
Views
5K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
10K
  • · Replies 12 ·
Replies
12
Views
11K
  • · Replies 1 ·
Replies
1
Views
3K