assembly Mips read text from File and buffer

19,050

This program reads a file and reverses the text in the file. I hope it can be of use for you.

.data  
fin: .asciiz "file.txt"      # filename for input
buffer: .space 128
buffer1: .asciiz "\n"
val : .space 128
newline: .asciiz "\n"
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

file.txt

Hello World

Output

dlroW olleH

enter image description here

The machine code is:

00100100000000100000000000001101
00111100000000010001000000000001
00110100001001000000000000000000
00100100000001010000000000000000
00100100000001100000000000000000
00000000000000000000000000001100
00000000000000101000000000100001
00100100000000100000000000001110
00000000000100000010000000100001
00111100000000010001000000000001
00110100001001010000000000001001
00100100000001100000000000001011
00000000000000000000000000001100
00111100000000010001000000000001
00110100001001000000000000001001
00000000000000000000000000001100
00111100000000010001000000000001
10000000001010010000000000001001
00111100000000010001000000000001
00110100001001000000000000001001
00000000000000000000000000001100
00111100000000010001000000000001
00110100001001000000000000001001
00000000000000000000000000001100
00111100000000010001000000000001
00110100001001000000000100001011
00000000000000000000000000001100
00111100000000010001000000000001
00110100001001000000000100001101
00000000000000000000000000001100
00100100000010100000000000000000
00111100000000010001000000000001
00000000001010100000100000100001
10000000001010000000000000001001
00100001010010100000000000000001
00010101000000001111111111111011
00100100000000100000000000001011
00100000000000010000000000000001
00000001010000010101000000100010
00111100000000010001000000000001
00110100001000010000000000001001
00000001010000010100000000100000
10000001000001000000000000000000
00000000000000000000000000001100
00010101010000001111111111111000
00100100000000100000000000001010
00000000000000000000000000001100
00100100000000100000000000010000
00000000000101100010000000100001
00000000000000000000000000001100
Share:
19,050
Daniel Monteiro
Author by

Daniel Monteiro

Updated on June 04, 2022

Comments

  • Daniel Monteiro
    Daniel Monteiro almost 2 years

    How do I do, to read text from file.txt and save in the data segment like a string? For example, if the text in the file is "Hello World!", how to read this and modify it?

    In syscall 14, the buffer what is it for? I though it was to save the pointer to the file text.