Question
MIPS ASSEMBLY (using mars) (A) Using the template fileIO.asm as a starting point, write a MIPS procedure readfile that takes as its argument the address
MIPS ASSEMBLY (using mars)
(A) Using the template fileIO.asm as a starting point, write a MIPS procedure readfile that takes as its argument the address of a string that contains a valid filename, and then uses appropriate syscalls to read from that input file and then simply prints the content of that file to the screen. In the template there are two input files called test1.txt and test2.txt which you should test. To accomplish this task we allow you to create a large buffer, i.e., one that is larger than the expected number of ASCII characters (bytes) in the input file. The body of your code should work by calling the procedure you have written. The procedure should open the file, read its content as ASCII characters, store the content in the buffer, print the content to the screen, and then close the file.
Below is fileio.asm
.data
#Must use accurate file path.
#file paths in MARS are relative to the mars.jar file.
# if you put mars.jar in the same folder as test2.txt and your.asm, input: should work.
input: .asciiz "test2.txt" #used as input
output: .asciiz "copy.pgm" #used as output
buffer: .space 2048 # buffer for upto 2048 bytes
.text
.globl main
main: la $a0,input #readfile takes $a0 as input
jal readfile
la $a0, output #writefile will take $a0 as file location
la $a1,buffer #$a1 takes location of what we wish to write.
jal writefile
li $v0,10 # exit
syscall
readfile:
#Open the file to be read,using $a0
#Conduct error check, to see if file exists
# You will want to keep track of the file descriptor*
# read from file
# use correct file descriptor, and point to buffer
# hardcode maximum number of chars to read
# read from file
# address of the ascii string you just read is returned in $v1.
# the text of the string is in buffer
# close the file (make sure to check for errors)
writefile:
#open file to be written to, using $a0.
#write the specified characters as seen on assignment PDF:
#P2
#24 7
#15
#write the content stored at the address in $a1.
#close the file (make sure to check for errors)
Below is test1.txt
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 3 3 3 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0 0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 15 0 0 3 3 3 0 0 0 7 7 7 0 0 0 11 11 11 0 0 0 15 15 15 15 0 0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 0 0 0 3 0 0 0 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Below is test2.txt
The quick brown fox jumps over the lazy dog!
(B) We shall now build on the above example. Following the process of reading the file test1.txt, after which the ASCII characters have been read into the buffer, we shall call a second MIPS procedure called writefile. This procedure should open a file called copy.pgm, should then write the following information to that file:
P2 24 7 15 Then it should write out the content that was read into the buffer. It should then close the file.
If things are working properly when you view copy.pgm with a suitable image viewer, e.g., gimp, you should see something interesting.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started