1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MIPS programming file I/O problem

  1. Apr 9, 2012 #1
    1. The problem statement, all variables and given/known data

    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?

    2. Relevant equations



    3. 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
     
  2. jcsd
  3. Apr 10, 2012 #2

    Mark44

    Staff: Mentor

    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/.
     
  4. Apr 10, 2012 #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.
     
  5. Apr 11, 2012 #4

    Mark44

    Staff: Mentor

    If you do much assembly programming, hex tends to become the more natural mode of display.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: MIPS programming file I/O problem
  1. Short MIPS Program (Replies: 9)

  2. C++: File I/O (Replies: 1)

Loading...