Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following code shows how to write a file in the MARS assembler simulator (Li

ID: 3855148 • Letter: T

Question

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
###############################################################

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

Explanation / Answer

Data Segment

str1 db 'MADAM','$'

strlen1 dw $-str1

strrev db 20 dup(' ')

str_palin db 'String is Palindrome.','$'

str_not_palin db 'String is not Palindrome.','$'

Data Ends

Code Segment

Assume cs:code, ds:data

Begin:

mov ax, data

mov ds, ax

mov es, ax

mov cx, strlen1

add cx, -2

lea si, str1

lea di, strrev

add si, strlen1

add si, -2

L1:

mov al, [si]

mov [di], al

dec si

inc di

loop L1

mov al, [si]

mov [di], al

inc di

mov dl, '$'

mov [di], dl

mov cx, strlen1

Palin_Check:

lea si, str1

lea di, strrev

repe cmpsb

jne Not_Palin

Palin:

mov ah, 09h

lea dx, str_palin

int 21h

jmp Exit

Not_Palin:

mov ah, 09h

lea dx, str_not_palin

int 21h

Exit:

mov ax, 4c00h

int 21h

Code Ends

End Begin

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote