Question

In: Computer Science

Write a MIPS Assembly language program to request a file name from the user, open the...

Write a MIPS Assembly language program to request a file name from the user, open the file, read the contents, and write out the contents to the console.

This is what I have so far:

.data
   fileName:   .space 100
   prompt1:   .asciiz "Enter the file name: "
   prompt2:    .asciiz ""
   prompt3:   .asciiz "\n"
   buffer:    .space 4096
.text
   main:
       #
       li $v0, 4
       la $a0, prompt1
       syscall
      
       #
       li $v0, 8
       la $a0, fileName
       li $a1, 50
       syscall
      
       jal remove
      
       #
       li $v0, 13
       la $a0, fileName
       li $a1, 0
       li $a2, 0
       syscall
       move $s0, $v0
      
       #
       li $v0, 14
       move $a0, $s0
       la $a1, buffer
       li $a2, 11
       syscall
      
       #
       li $v0, 4
       la $a0, buffer
       syscall
      
       #
       li $v0, 10
       syscall

Solutions

Expert Solution

Note: My jar file and text file located in the same folder, please input full path for getting results

FileOperation.asm:

.data
fileName: .space 100
prompt1: .asciiz "Enter the file name: "
buffer: .space 40966
.text
main:
#
li $v0, 4
la $a0, prompt1
syscall
  
#(the file name must be full path or path from which Mars jar file is located)
li $v0, 8
la $a0, fileName
li $a1, 50
syscall


# converting the string into a null-terminated string
#replacing last character of fileName with 0 instead of \n

    la   $s0, fileName   # $s0 contains base address of str
   add   $s2, $0, $0   # $s2 = 0
   addi   $s3, $0, '\n'   # $s3 = '\n'
loop:
   lb   $s1, 0($s0)   # load character into $s0
   beq   $s1, $s3, end   # Break if byte is newline
   addi   $s2, $s2, 1   # increment counter
   addi   $s0, $s0, 1   # increment str address
   j   loop
end:
   sb   $0, 0($s0)   #replace newline with 0
  
   #now that input file name is converted to null-terminated string the remaining code is same
  
  
   # Open file for reading

   li $v0, 13 # system call for open file
   la $a0, fileName # address of null-terminated string containing filename
   li $a1, 0 # flags
   li $a2, 0 # mode
   syscall # open a file
   move $s0, $v0 # save the file descriptor
  
   # reading from file just opened

   li $v0, 14 # system call for reading from file
   move $a0, $s0 # file descriptor
   la $a1, buffer # address of input buffer
   li $a2, 100 # maximum number of characters to read
   syscall # read from file

   # Printing File Content
   li $v0, 4 # system Call for PRINT STRING
   la $a0, buffer # buffer contains the values
   syscall # print int

li $v0, 10 # Finish the Program
syscall
Output:


Related Solutions

MIPS Assembly LanguageWrite a MIPS assembly language program that asks the user toinput an...
MIPS Assembly LanguageWrite a MIPS assembly language program that asks the user to input an integer and then prints out a string that shows how that integer should be encoded using 16 bits. Your program should handle both positive and negative valued inputs. Your program should also print out an error message if the given input cannot be expressed as a 16 bit signed integer.As an example, if the input is 12, your program should output “0000000000001100”. If the input...
Write a mips assembly language program to ask the user to enter two integers A and...
Write a mips assembly language program to ask the user to enter two integers A and B and then display the result of computing the expression: A + 2B - 5.
Write a mips assembly language program that asks the user to enter an unsigned number and...
Write a mips assembly language program that asks the user to enter an unsigned number and read it. Then swap the bits at odd positions with those at even positions and display the resulting number. For example, if the user enters the number 9, which has binary representation of 1001, then bit 0 is swapped with bit 1, and bit 2 is swapped with bit 3, resulting in the binary number 0110. Thus, the program should display 6.
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers...
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers from the console (Dn and Up), Call a recursive function to compute the result int RecurseFunc( int Dn, int Up ) { if( Dn < 1 ) return 0; return Dn * Up + RecurseFunc( Dn - 1, Up + 1 ); } Print out the results
Write a mips assembly language program. Ask the user whether he wants to repeat the program:...
Write a mips assembly language program. Ask the user whether he wants to repeat the program: "\nRepeat [y/n]? ". Use service code 12 to read a character and the branch instruction to repeat the main function if the user input is character 'y'.
Write a mips assembly language program that asks the user to enter an alphabetic character (either...
Write a mips assembly language program that asks the user to enter an alphabetic character (either lower or upper case)and change the case of the character from lower to upper and from upper to lower and display it.
Using MARS write a MIPS assembly language program to prompt the user to input two 32-bit...
Using MARS write a MIPS assembly language program to prompt the user to input two 32-bit integers X and Y (X and Y can be prompted separately or at the same time), get them from the user then store them in memory locations labeled X and Y respectively. The program then loads X and Y from the main memory to registers, calculates the sum of them (i.e. X + Y) and store the sum into a memory location labeled S....
Write a MIPS assembly program that prompts the user first for a string, then for a...
Write a MIPS assembly program that prompts the user first for a string, then for a character. The program should then search the string for the character and print out the number of times the character appears in the string. You can use as many restrictions as you want, just be sure to list them. You must allocate space in the .data segment of your program for the user string.
Write a MIPS assembly language program to read an arbitrary number of integer pairs from a...
Write a MIPS assembly language program to read an arbitrary number of integer pairs from a file. Each pair will then be multiplied together with the results being accumulated (added together) forming a sum-of-products operation. Submit your report and code here.
Assembly Language Coding Using MARS (MIPS) 1)Write a program that prints your name in a Triangle....
Assembly Language Coding Using MARS (MIPS) 1)Write a program that prints your name in a Triangle. (NAME = John Doe) 2)Write a Program that intializes X to 10, Y to 20 and Z to -50, adds X and Y and Z and prints the following the value of each variable, for example value of x is 10 as well as the result of the addition.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT