Question

IN MIPS PLEASE. Need help writing a MIPS program that reads from a text file named...

IN MIPS PLEASE.

Need help writing a MIPS program that reads from a text file named "input.txt" and places it in a buffer. A buffer of 80 bytes is more than enough. Before you call your function, set $a0 equal to the address of the filename and $a1 to the address of the buffer where data is stored. The function should return the number of bytes read in $v0. In the main program, print an error message and terminate the program if $v0 <= 0.

Next I need help writing a function to extract integers from the text input buffer and store them in an array of 20 words. Before the function, set $a0=address of the array, $a1=20, $a2 the address where the buffer starts.

Helpful hints: The input buffer is just a series of ASCII bytes. You will loop through it byte by byte. As you load a byte from the buffer, ignore the byte if it is <48 (ASCII for 0) or >57 (ASCII for 9). If it is within this range, it is a digit. Subtract 48 to convert it from ASCII to int. Multiply the register you are using as an accumulator by 10, then add this new digit. When you load in a byte that is equal to 10, this is newline, so you are done with the integer you were converting; save the integer to the next array element. When you load a byte that is equal to 0 you have reached the end of the data.

Write a function to print the array of ints as shown in the sample output below. You will print the array before you call the sort, and after it is sorted, with appropriate text messages.

Write a function to sort the array by a selection sort.

In the remaining functions, before calling them, set $a0 to be the start of the array and $a1 to be the length of the array. Return integer values in $v0 and float values in one of the $f registers.

Write a function to calculate the mean. Use single precision for the mean. Store the mean in memory as a float.

Write a function to calculate the median. Write the function so that if the length is odd, it returns the middle value as an integer. If the length is even, average the two middle values and return the median as a float. Set $v1 to be a flag to indicate whether the result was int or float so that you can use the appropriate syscall in main to print the median.

Write a function to calculate the standard deviation using the formula below. Note that there is a sqrt.s instruction in MARS. From main, save the sd and print it.

Thank you in advance.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

Sample output:

input.txt:

Code to copy:

################################### stdDev.asm ######################################
.data
inputfile : .asciiz "input.txt"
prompt: .asciiz "Enter input text filename: "
msg1: .asciiz "The array before:\t"
msg2: .asciiz "The array after: \t"
msg3: .asciiz "the mean is: "
msg4: .asciiz "The median is: "
msg5: .asciiz "The standard deviation is: "
newline : .asciiz "\n"
space : .asciiz " "
buffer: .space 80
intarray: .word 20

.text
main:
   # Pass file name and address of the buffer to the procedure readFile
   la $a0,inputfile
   la $a1, buffer                # address of buffer where dat is to be stored
   jal readFile                  # Call the procedure readFile
   beq $v0,$0,exit                  # Exit, if error occured in opening file
   # Extract integers from ASCII string to integer array  
   la $a0, intarray                # address of integer array
   li $a1, 20                  # maximum number of integers can be stored
   la $a2, buffer                # address input buffer
   jal extractInts                  # Call the procedure extractInts
   move $t7,$v0                  # $t7= number of integers extracted or
                    # $t7= size of the array
   li $v0,4               # print label "The array before:"
   la $a0,msg1
   syscall
   # print the original integer array
   la $a0,intarray                # address of integer array
   move $a1,$t7                  # $a1= length of the array
   jal print                      # Call the procedure print
   # sort the integer array using selection sort
   la $a0,intarray                # address of integer array
   move $a1,$t7                  # $a1= length of the array
   jal selectionSort              # Call the procedure sort
   # pritn the sorted integer array
   li $v0,4               # print label "The array after:"
   la $a0,msg2
   syscall
   la $a0,intarray                # address of integer array
   move $a1,$t7                  # $a1= length of the array
   jal print                      # Call the procedure print
# Calculate and print mean
   la $a0,msg3               # print label "The mean is:"
   li $v0,4
   syscall
   la $a0,intarray                # address of integer array
   move $a1,$t7                  # $a1= length of the array
   jal calMean
   li $v0,2               # print mean
   syscall
   la $a0,newline
   li $v0,4
   syscall
   la $a0,msg4               # print label "The median is:"
   li $v0,4
   syscall
# Calculate and print median
   la $a0,intarray                # address of integer array
   move $a1,$t7                  # $a1= length of the array
   jal calMedian    
   bltz $v1, printFloat           # if $v1 is negative mean median is a float
                    # value (average of middle two numbers)
   # if $v1 is positive means, median is a an integer( middle integer)
   move $a0,$v0
   li $v0,1                 # print median(integer value)
   syscall
   j stdDev               # go to calcualte standard deviation
printFloat:
   li $v0,2
   syscall               # print median(float value)
# Calculate and print standard deviation
stdDev:
   li $v0,4
   la $a0,newline
   syscall
   li $v0,4              
   la $a0,msg5  
   syscall               # print label "The standard deviation is:"
   la $a0,intarray                # address of integer array
   move $a1,$t7                  # $a1= length of the array
   jal calStdDev
   li $v0,2
   syscall                   # print standared deviation
exit:   li $v0, 10                  # Exit the program
   syscall
################################   readFile #####################################
# reads data into a buffer of maximum length 80                              #
#################################################################################
readFile:
   move $t1,$a1               # tempararily store address of buffer in $t1
   # Open file for reading
   li   $v0, 13                     # system call for open file
   li   $a1, 0                      # flag for reading(0-read only)
   syscall                          # open a file

   blt $v0,$0 returnReadFile          # If failed to open input file, return to main
   move $s0, $v0                    # save the file descriptor in $s0
   # Reading from file just opened
   li   $v0, 14                     # system call for reading from file
   move $a0, $s0                    # file descriptor
   move $a1,$t1                  # address of buffer
   li   $a2, 80                    # hardcoded buffer length
   syscall                          # read from file
   # Close input file
   li   $v0, 16                     # system call for reading from file
   move $a0,$s0               # #a0 file descripter
   syscall
   move $v0,$s0                  # return number of character read
returnReadFile:
   jr $ra                      # Return to main program
################################## extractInts ##################################
# Traverses the buffer byte by byte and convertes into a decimal             #
# number and stores the converted numbers into an array of integers             #
#################################################################################
extractInts :
   # read buffer(ASCII string) byte by byte
   li,$s1,-1                      # To store the integer decimal
   li $t0,0
loop1:   lb $t1,($a2)              # Load the address of first byte into $t2
   beq $t1,10,storeintoarray         # if byte is new line(new line means one complete
                    # decimal integer is formed) save the integer into
                              # array
   beq $t1,$zero,returnextractInts      # If $t1 is 0, end of file is reached
                              # thus return to main
   # if byte is not a digit(0-9), ignore it
   blt $t1,48,ignoreNnext          # ignore the byte if $t1<48
   bgt $t1,57,ignoreNnext          # ignore the byte if $t1>57
   # other wise consider it for convertion
   addi $t1,$t1,-48              # Convert charcter digit to decimal digit
                              # by subtracting 48 from ASCII integer.
   bne $s1,-1,multiply10      
   li $s1,0               # if $s1=-1, current byte is the starting digit
                    # thus set $s0 to 0
multiply10:
   li $t3,10        
   mul $s1,$s1,$t3                  # Multiply $s1 with 10
   add $s1,$s1,$t1                  # Add converted decimal digit to $s1
ignoreNnext:            
   add $a2,$a2,1                  # go to next byte
   j loop1               # go to next iteration
# if complete decimal integer is formed, save it into integer array
storeintoarray:                    
   beq $s1,-1,skipStoring         # -1 means , no integer is formed
   sll $t2,$t0,2                  # multiply index with 4
   add $t2,$t2,$a0                  # #t2=address to store integer
   sw $s1,0($t2)                  # Store decimal integer into array
   li $s1,-1                      # Re set $s1 to -1(for fresh integer)
skipStoring:
   addiu $t0,$t0,1                   # increment the index of integer array
   add $a2,$a2,1                  # go to next byte
   beq $t0,20,returnextractInts         # only allow maximum 20 integer
   j loop1
returnextractInts:
   move $v0,$t0                  # return number of integers read(converted)
   jr $ra                      # Return to main program
####################################### print ###################################
# Prints the integer array                                           #
#################################################################################
print:
   move $s0,$a0
   li $t0,0
loop2:
   beq $t0,$a1,returnPrint         # if $t0 = size of the array, return
   li $v0,1
   sll $t1,$t0,2
   add $t1,$t1,$s0
   lw $a0,0($t1)           # load the integer
   syscall               # print integer
   li $v0, 4                        # 4 for printing a string
   la $a0, space                   # Print space
   syscall
   add $t0,$t0,1                  # Move to next integer
   j loop2
returnPrint:
   li $v0, 4                        # 4 for printing a string
   la $a0, newline                   # Print new line
   syscall
   jr $ra                      # Return to main program
################################## selectionSort ################################
# Sorts the integer array using selection sort                              #
#################################################################################
selectionSort:
   li $t0,0                      # $t0= the starting index(j) of intarray
   sub $s0,$a1,1                  # $s1=n-1
outerloop:
   beq $t0,$s0,returnSSort
   move $s1,$t0                  # iMin=j
   add $t1,$t0,1                  # i=j+1
innerloop:
   beq $t1,$a1,check4swap
   sll $t2,$t1,2
   sll $t3,$s1,2
   add $t2,$t2,$a0
   add $t3,$t3,$a0
   lw $t4,0($t2)                  # $t4= intarray[i]
   lw $t5,0($t3)                  # $t5= intarray[iMin]
   blt $t4,$t5,udateI              # Swap $t0 and $t1, if $t0<$t1
   j nextinner

udateI:   move $s1,$t1              # iMin=i
nextinner:
   add $t1,$t1,1
   j innerloop               # go to next iteration of inner loop
check4swap:
   bne $s1,$t0,swap              # swap intarray[j] and intarray[iMin],
                              # if intarray[i]<intarray[iMin]
   j nextouter
# swap intarray[j] and intarray[iMin]
swap:
   sll $t2,$t0,2
   sll $t3,$s1,2
   add $t2,$t2,$a0        
   add $t3,$t3,$a0
   lw $t4,0($t2)        
   lw $t5,0($t3)        
   sw $t4,0($t3)
   sw $t5,0($t2)
nextouter:
   add $t0,$t0,1
   j outerloop               # go to next iteration of outer loop
returnSSort:     
   jr $ra
####################################### calMean #################################
# returns calMean of the given array of integers                           #
#################################################################################
calMean:
   li $t0,0
   mtc1 $t0,$f12                  # sum=0 (keep track sum int $f12)
   mtc1 $t0,$f0
# sum all the integers in the array
calMeanloop:
   beq $t0,$a1,returnCalMean       # if $t0 = size of the array, return to main
   sll $t1,$t0,2
   add $t1,$t1,$a0
   lwc1 $f0,0($t1)           # load integer as float value into $f0
   add.s $f12,$f12,$f0           # add it to $f12
   add $t0,$t0,1           # advance $t0 by 1
   j calMeanloop           # go to next iteration
returnCalMean:
   mtc1 $a1,$f0                  # $f0= n
   div.s $f12,$f12,$f0         # mean=$f12=sum/n
   jr $ra                      # return mean in $f12
#################################### calMedian ##################################
# returns median of the given array of integers                             #
#################################################################################
calMedian:
   div $t0,$a1,2           # $t0=index of middle integer
   mfhi $t1
   beqz $t1,calaverage           # if $t1=0 means, number of integers is even
                    # then calculate average of middle two integers
   sll $t2,$t0,2
   add $t2,$t2,$a0
   lw $v0,0($t2)           # other wise, number of integers is odd
                    # and thus return the middle integer as median
   li $v1,0               # return 0 in $v1, means result is integer
   j returnCalMedian
calaverage:
   sub $t1,$t0,1
   sll $t2,$t0,2
   sll $t3,$t1,2
   add $t2,$t2,$a0        
   add $t3,$t3,$a0
   lw $t4,0($t2)               # load middle two integers    
   lw $t5,0($t3)
   add $t4,$t4,$t5           # sum of middle two integers
   mtc1 $t4,$f12           # $f12= sum of middle two integers
   li $t5,2
   mtc1 $t5,$f0               # $f0= 2
   div.s $f12,$f12,$f0           # $f12= $f12/2
   li $v1,-1                  # return negative value $v1
                    # negative value indicates that result is a float
returnCalMedian:      
   jr $ra               # return to main
#################################### calStdDev ##################################
# returns standard deviation of the given array of integers                   #
#################################################################################
calStdDev:
   add $sp,$sp,-4
   sw $ra,4($sp)                  # save return address
   jal calMean               # $f12= mean
   mov.s $f0,$f12           # copy mean into $f0
   li $t0,0
   mtc1 $t0,$f12                  # sun=0 ( keep track sum in $f12)
loopStdDev:
   beq $t0,$a1,returnStdDev        # if $t0 = size of the array, return to main
   sll $t1,$t0,2
   add $t1,$t1,$a0
   lw $t2,0($t1)           # load integer
   mtc1 $t2,$f1              
   cvt.s.w $f1,$f1           # convert it into single precission value
   sub.s $f2,$f1,$f0             # $f2=ri-ravg
   mul.s $f3,$f2,$f2             # $f3=(ri-ravg)^2
   add.s $f12,$f12,$f3         # $f12=Sum of (ri-ravg)^2
   add $t0,$t0,1
   j loopStdDev               # go to next iteration
returnStdDev:
   sub $t2,$a1,1                  # $t3=n-1
   mtc1 $t2,$f4                  # $f4=n-1
   cvt.s.w $f4,$f4             # CONVERT $t2 into single precission value
   div.s $f12,$f12,$f4         # $f12=sum/n-1
   sqrt.s $f12,$f12               # sqrt($f12)
   lw $ra,4($sp)
   add $sp,$sp,4                  # re store the return address
   jr $ra               # return to main
#################################################################################

Add a comment
Know the answer?
Add Answer to:
IN MIPS PLEASE. Need help writing a MIPS program that reads from a text file named...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Subroutines in MIPS Determines the minimum of two integers Functions within the MIPS slides describe how...

    Subroutines in MIPS Determines the minimum of two integers Functions within the MIPS slides describe how one can use subroutines (also called procedures, functions, and methods) in MIPS. Because of the importance of subroutines in modern programming, most hardware designers include mechanisms to help programmers. In a high-level language like C or Java, most of the details of subroutine calling are hidden from the programmer. MIPS has special registers to send information to and from a subroutine. The registers $a0,...

  • Q-1: Write a program in Assembly language using MIPS instruction set that reads 15 integer numbers...

    Q-1: Write a program in Assembly language using MIPS instruction set that reads 15 integer numbers from user and stores all the numbers in the array intArray. Now, read another integer number N from the user, find the total number of array elements that are greater or equal to the number N, and the total number of array elements that are lower than the number N You must have two procedures: i. ReadIntegerArray: this procedure should read integer array elements...

  • MIPS Insertion program.........I could really use some help ASAP

    I have this MIPS program and I'm having trouble with it. This program is user inputs numbers until zero and sorts and print the numbers in order. Please soove this issue. You can use any sorting algorithm except bubble sort.  Need it as soon as possible. Here is the code:.datanum: .word 0space: .byte ' ' .text main:  # la $t0, val # loads val into a register  # li $t1, 0      #keeps track of how many numbers entered  la $a0,...

  • MIPS ASSEMBLY LANGUAGE Write MIPS code to convert binary to decimal: (01110001)2 to (113)10 .data   binary_digit:  ...

    MIPS ASSEMBLY LANGUAGE Write MIPS code to convert binary to decimal: (01110001)2 to (113)10 .data   binary_digit:       .word       0 1 1 1 0 0 0 1       # is 113 in decimal .globl main main: # Load arguments to argument registers # Call convert()      # Print return value # (it's in $v0, make sure to copy it before overwriting to print)        # Properly end program    convert: # This label is where conversion of the current...

  • Need to implement an IterativeMax function in assembly language using the MIPS calling convention. IterativeMax take...

    Need to implement an IterativeMax function in assembly language using the MIPS calling convention. IterativeMax take two arguments: The address of an array of word-long (four byte) signed integers, held in $a0. The length of the array as an unsigned integer, held in $a1. The main purpose of the function is to determine which element of the provided array is the largest (i.e., the maximum element in the array). This is achieved by going through the array one element at...

  • Write the assembly code that will transform a squared image into a negative print. (HARD-CODED OUTPUT...

    Write the assembly code that will transform a squared image into a negative print. (HARD-CODED OUTPUT WILL RESULT IN ZERO.) That is, every pixel whose unsigned value is 0 should change to 255, 1 should change to 254, 2 should change to 253, ... 255 should change to 0. $a0 contains the address of the input buffer, $a1 contains the address of the output buffer address, and $a2 contains the image dimension. Look at the below image for your reference:...

  • MIPS - Takes two inputs from the user, which are the lengths of two sides of...

    MIPS - Takes two inputs from the user, which are the lengths of two sides of a polygon. It adds those two lengths and prints the sum. Your task is modify the program to make it specific to a triangle, and to print the perimeter of that triangle. See blue highlighted. preamble: prompt1: prompt2: answer: endline: .data ascii .asciiz asciiz .asciiz asciiz .asciiz "\nThis program, written by <YOUR NAME>," " can be used to add the length of two sides...

  • # File: mlRemoveBlanks # Author: ************ # Date: mm/dd/yyyy # Purpose: Practice rotate & AND to...

    # File: mlRemoveBlanks # Author: ************ # Date: mm/dd/yyyy # Purpose: Practice rotate & AND to remove spare blanks #-------------------------------------------------------------- # Write a MIPS assembler program to remove the extra blanks from # a string: # INPUT: "Two bee ore knot too Bea that" # PATTERN:00011000100011000010001100010000 # ROTATE: 00110001000110000100011000100000 # AND: 00010000000010000000001000000000 # RESULT:"Two bee ore knot too Bea that " .data .eqv SYS_PRINT_WORD 1 #word, byte, character .eqv SYS_PRINT_FLOAT 2 #float .eqv SYS_PRINT_DOUBLE 3 #double .eqv SYS_PRINT_TEXT 4 #text...

  • How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++...

    How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++ .data # Defines variable section of an assembly routine. array: .word x, x, x, x, x, x, x, x, x, x # Define a variable named array as a word (integer) array # with 10 unsorted integer numbers of your own. # After your program has run, the integers in this array # should be sorted. .text # Defines the start of the code...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT