2

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.

Daniel Monteiro
  • 67
  • 1
  • 1
  • 8
  • http://stackoverflow.com/questions/4147952/reading-files-with-mips-assembly , http://stackoverflow.com/questions/30477204/mips-write-and-read-a-file , http://stackoverflow.com/questions/23146234/when-reading-file-in-mips-it-reads-last-line-twice – Jose Manuel Abarca Rodríguez May 26 '16 at 19:31
  • I'd edit your post and put all your code in a single code block as I imagine it's not too much larger. Also, how you define things in `.data` is important. This would allow someone to run it. Also, I'd indent it a bit and add comments explaining your logic flow (ie. intent). I don't have a "read file" example [of mine] handy, but here's an answer of mine for writing to a file that may help: http://stackoverflow.com/a/35930028/5382650 The `open/read/write/close` are similar just like in C – Craig Estey May 26 '16 at 19:32
  • 1
    thanks Jose, i will check those links – Daniel Monteiro May 26 '16 at 20:23
  • I've made several edits, I think the question is more specific now. – Daniel Monteiro May 26 '16 at 23:00
  • The question is more specific, but, now, lacks your code ;-) However, the four relevant syscalls, `open/close/read/write` [by design] match their C counterparts in `libc` almost exactly. So, for syscall 14, it is `int read(int fd,void *buffer,int rlen)`. Do `man 2 read`. So, in a way, what you said _is_ correct. It will function the same way. But, I'd describe it thus: Put the file descriptor in `$a0`, the buffer address [_where you want the data to be read into_] in `$a1`, and the byte length of the buffer in `$a2`. You will get back the number of bytes actually read in `$v0`. – Craig Estey May 27 '16 at 05:23

1 Answers1

0

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
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
  • 1
    Yes, thanks, with this I can make what I wanted. But this code does not produce that output, I am using Mars and the output this code makes is a char representing music ♫ . – Daniel Monteiro May 30 '16 at 13:38
  • Are you sure the file is there? I can try again. – Niklas Rosencrantz May 30 '16 at 13:39
  • 1
    Yes, the .asm file, file.txt and Mars files are all in the same dir – Daniel Monteiro May 30 '16 at 13:41
  • I try it again and it prints `dlroW olleH` (in the MARS console). I use Ubuntu and I can try it with MS.Windows 10. I can update the answer with a screenshot. – Niklas Rosencrantz May 30 '16 at 13:43
  • 1
    strange, im using debian, not Windows. here is a link to a screenshot:[link](http://ctrlv.in/763313) – Daniel Monteiro May 30 '16 at 14:16
  • 1
    I also noticed that your PC register is different than my own – Daniel Monteiro May 30 '16 at 14:20
  • If our machine codes are identical it must be some external thing with operating system or file permissions. If we really want to know we can have a C program that does the same thing and compare the generated assembly. It might be a trivial error with file permissions though. I want to learn more MIPS so I can use this example to see if I can reproduce your problem. It is good to work on an actual problem instead of just theory. I need to know MIPS for an exam next week. I'll se if I can export my machine code or my register contents to you so that we can compare our results. – Niklas Rosencrantz May 30 '16 at 14:20
  • 1
    completely agree, I am studding for a class in my college, and my task is to use a file, and encrypt the text in the file with a cipher called vigenere. – Daniel Monteiro May 30 '16 at 14:43
  • I will take this example and study it for some hours. I need to know MIPS for an exam next week at my university (www.KTH.se). I'm going to look in the debugger and inspect what differences you and I had in our registers. There is a [reference sheet](https://www.kth.se/social/files/54948c82f276540590491ed4/mips-ref-cheat-sheet.pdf) that we can use if we don't understand. If all else fails we can write the program in C and translate it to MIPS line by line and see where it fails. Right know I think it's time for us to learn the MIPS debugging and if you and I had some different settings. – Niklas Rosencrantz May 30 '16 at 14:47
  • I use delayed branch off and default settings in MIPS. I didn't change any setting. I'm not familiar with the "Reset" error msg – Niklas Rosencrantz May 30 '16 at 14:48