MIPS programming file I/O problem

In summary: I usually find myself thinking about register values as hex addresses, with the leftmost digit being the register number and the rightmost digit being the byte within the register. (I don't know if that's actually how the MIPS is designed, but it works well enough for me. And, of course, the MIPS has 32-bit registers; I just don't think about anything bigger than 8 bits very often.)In summary, the conversation is about a program that prompts for a file name, echoes the input, checks if a file by that name already exists, and creates it if it doesn't exist or if the user approves of overwriting. The individual is having trouble with the
  • #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
 
Physics news on Phys.org
  • #2
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/.
 
  • #3
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.
 
  • #4
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.
 
  • #5

#######################################################################################################################################


As a scientist, my expertise may not be in computer programming, but here are some suggestions and possible solutions to your problem:

1. In order to determine if a file name already exists, you can use the "stat" system call to get information about the file. This will return a struct containing the file's information, including the file size. If the file size is greater than 0, then the file exists.

2. Your approach of prompting the user for input and then checking if the file exists is a feasible one. However, you may want to consider adding some error handling for cases where the user enters an invalid file name or if the file is unable to be opened.

3. To debug your program, you can use a debugger tool such as GDB. This will allow you to step through your code line by line and see the values of different variables as the program is running. This can help you identify where the program is getting stuck or where there may be errors in your logic.

4. It may also be helpful to break down your code into smaller functions, such as one for prompting the user, one for checking if the file exists, and one for writing to the file. This can make it easier to identify and fix any errors.

5. Additionally, you may want to add comments to your code to explain what each section is doing. This can help others (and even yourself) understand your code better and make it easier to identify any potential issues.

Overall, programming can be a challenging and iterative process, so don't be discouraged if your code doesn't work perfectly on the first try. Keep testing, debugging, and refining your code until it meets all the necessary requirements. Good luck!
 

FAQ: MIPS programming file I/O problem

What is a MIPS programming file I/O problem?

A MIPS programming file I/O problem refers to an issue that arises when trying to read from or write to a file within a MIPS assembly program. This can occur due to errors in the code, incorrect file permissions, or other factors.

2. How do I read from a file in MIPS?

To read from a file in MIPS, you can use the syscall instruction with the appropriate system call number for reading, which is 14. You will also need to specify the file descriptor and the address where you want to store the data that is read from the file.

3. How do I write to a file in MIPS?

To write to a file in MIPS, you can use the syscall instruction with the system call number for writing, which is 15. You will need to specify the file descriptor, the address of the data you want to write, and the number of bytes to write.

4. What are some common errors when using file I/O in MIPS?

Some common errors when using file I/O in MIPS include forgetting to open the file before attempting to read from or write to it, not checking for errors after each system call, and not properly handling the end of the file or other special cases.

5. How can I troubleshoot a MIPS programming file I/O problem?

To troubleshoot a MIPS programming file I/O problem, you can use a debugger or print statements to track the flow of your program and identify potential errors. You can also consult the MIPS assembly language reference manual for more information on the specific system calls and their parameters.

Similar threads

Replies
4
Views
4K
Replies
2
Views
4K
Replies
1
Views
5K
Replies
1
Views
2K
Replies
1
Views
4K
Replies
4
Views
9K
Replies
12
Views
11K
Back
Top