Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program to read the following text file line by line, detect which words are palindromes(use the palindrome previous assignment), writes them to another

Write a program to read the following text fileimage text in transcribedimage text in transcribed line by line, detect which words are palindromes(use the palindrome previous assignment), writes them to another file and counts them.

The following code shows how to write a file in the MARS assembler simulator (Links to an external site.):

# Sample MIPS program that writes to a new file. # by Kenneth Vollmar and Pete Sanderson

.data fout: .asciiz "testout.txt" # filename for output buffer: .asciiz "The quick brown fox jumps over the lazy dog." .text ############################################################### # Open (for writing) a file that does not exist li $v0, 13 # system call for open file la $a0, fout # output file name li $a1, 1 # Open for writing (flags are 0: read, 1: write) li $a2, 0 # mode is ignored syscall # open a file (file descriptor returned in $v0) move $s6, $v0 # save the file descriptor ############################################################### # Write to file just opened li $v0, 15 # system call for write to file move $a0, $s6 # file descriptor la $a1, buffer # address of buffer from which to write li $a2, 44 # hardcoded buffer length syscall # write to file ############################################################### # Close the file li $v0, 16 # system call for close file move $a0, $s6 # file descriptor to close syscall # close file ###############################################################

A quick tip is a procedure that reads the file a character at a time, each character is assumed to be in ASCII and takes up a byte. You should test your code with the file containing one thousand wordsimage text in transcribedimage text in transcribed. Please assume that the file is in the current directory where the MIPS assembly file exists. Please submit only .asm files. if you submit any other type of file, I will not grade it. Include test cases in your code with the one thousand words file.

The following code helps as well: assuming file.txt contains only "hello world". Please note

.data fin: .asciiz "C:/Users/trouble/Downloads/file.txt" # filename for input buffer: .space 128 buffer1: .asciiz " " val : .space 128 newline: .asciiz " " ans: .asciiz " The String reversed is " .text

################################################ fileRead:

# Open file for reading

li $v0, 13 # system call for open file la $a0, fin # input file name li $a1, 0 # flag for reading li $a2, 0 # mode is ignored 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 buffer from which to read li $a2, 11 # hardcoded buffer length syscall # read from file

#li $v0, 4 # la $a0, buffer # buffer contains the values syscall # print int

lb $t1 , buffer

la $a0, buffer #calling opening prompt #li $v0, 4 syscall la $a0, buffer #initial string syscall la $a0, newline #newline syscall la $a0, ans #initial text for reversed string syscall li $t2, 0 strLen: #getting length of string lb $t0, buffer($t2) #loading value add $t2, $t2, 1 bne $t0, $zero, strLen

li $v0, 11 #load immediate - print low-level byte Loop: sub $t2, $t2, 1 #this statement is now before the 'load address' la $t0, buffer($t2) #loading value lb $a0, ($t0) syscall #This is where the sub statement used to be, which caused the loop to terminate too early bnez $t2, Loop li $v0, 10 #program done: terminating syscall

# Close the file

li $v0, 16 # system call for close file move $a0, $s6 # file descriptor to close syscall # close file

Here's the palindrome function:

is_palindrome: move $t0, $a0 #get the address of the string addi $sp,$sp, -12 #make space for return address and other variables sw $fp, 8($sp) #save the old frame pointer move $fp, $sp #mark end of frame. sw $t0, 4($fp) #store the address of the string sw $ra 0($fp) #store the return address jal strlen move $t1,$v0 #get the length of the string lw $t0, 4($sp) #get the address of the first character srl $t2, $t1, 1 # divide the length by 2(integer division) addi $t1,$t1, -1 #decrement to the actual address of the last byte of the string in position n-1, first byte is in position 0 add $t1,$t0,$t1 #get the address of the end of the string addi $v0,$zero, 1 #true initially, covers the case of the empty string is_palindrome_loop: #test the counter first! bge $t0,$t1, end_is_palindrome lb $t4,0($t0) lb $t5,0($t1) beq $t4,$t5,is_pal_continue #check if the two characters are equal move $v0,$zero #failed not a palindrome j end_is_palindrome #cleanup is_pal_continue: addi $t0,$t0,1 addi $t1,$t1,-1 addi $t2,$t2,-1 bgt $t2,$zero,is_palindrome_loop end_is_palindrome: lw $ra,0($fp) # get the return address move $sp,$fp # restore stack pointer sw $fp, 8($sp) add $sp,$sp,12 #clean up the stack jr $ra #return to caller strlen: addi $sp,$sp, -8 sw $fp, 4($sp) #store frame pointer sw $ra, 0($sp) #store the return address move $fp,$sp #save stack pinter move $t0,$a0 #get the address of the first character move $t2,$zero # initialise the length to 0 strlen_loop: #begining of while loop lb $t1,0($t0) beqz $t1,end_strlen_loop #make sure we do not have the end of addi $t0,$t0, 1 #advance the address by a byte! addi $t2,$t2,1 #increment the counter j strlen_loop # jump back to the beginning end_strlen_loop: ### do cleanup strlen_end: move $v0,$t2 #store the length of the string move $sp,$fp #restore stackpointer lw $fp, 4($sp) #restore frame pointer lw $ra, 0($sp) #restore the return address addi $sp,$sp,8 jr $ra #return

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database System

Authors: Elmasri Ramez And Navathe Shamkant

7th Edition

978-9332582705

More Books

Students also viewed these Databases questions

Question

3. Job rotation is used for all levels and types of employees.

Answered: 1 week ago