How Read file "Comma Separated file" using Mips Assembly line by line then Char by Char and convert it from String of ASCII to Int? - csv

enter image description here
the CSV file that i am trying to read
I am trying to read a CSV "Comma Separated file" In Mips assembly language, as string of ASCII then put it in Register or buffer as Integers .
If any one had the same problem or can help me with this .
I have tried to read a CSV file, put i couldn't continue .
.data
file: .asciiz "helloWorld.csv"
.word 0
buffer: .space 4
.text
main:
#open file
li $v0, 13
la $a0, file
add $a1, $zero, $zero
add $a2, $zero, $zero
syscall
add $s0, $v0, $zero
#read 4 bytes from file
li $v0, 14
add $a0, $s0, $zero
la $a1, buffer
li $a2, 4
syscall
#print
li $v0, 1
lw $a0, buffer
syscall
done:
li $v0, 16
add $a0, $s0, $zero
syscall
exit:
li $v0, 10
syscall

Related

MIPS: Trying to print a word from a file, always prints a letter

I'm pretty new to MIPS, hope you can help me. I need to write a program that gets some words from a file, which are separated by a character. I have written this code testing with just one word currently, but when it comes to printing the word (the print_word method), it's always printing the letter 'J', even if that letter is not on the word. What am I doing wrong here?
This is the code I currently have:
.data
origin: .asciiz "words.txt" # File containing the text, located on the same directory as the jar
messg: .asciiz "Please type a 1-character separator (, . |, etc): "
line: .space 1024 # Hard coded line size with max 1024 byte long
copy: .space 1024 # Space for the copied word
seprtr: .space 1 # Space for the separator
chartr: .space 1 # Space for the current character being read
newline: .asciiz "\n"
.text
main:
la $a0, messg
li $v0, 4 # Print message
syscall
la $a0, seprtr
li $a1, 1
li $v0, 8 # System call to read char
syscall
addi $s7, $v0, 0 # Save separator on $s7
jal new_line
la $s1, line
la $s2, copy
la $s3, chartr
read_line:
li $v0, 13 # Syscall to open file
la $a0, origin # File name
li $a1, 0 # Read
syscall #
add $s6, $zero, $v0 # Save file descriptor
str_read:
# Read file byte by byte
li $v0, 14 # Syscall to read file
add $a0, $zero, $s6 #
la $a1, chartr #
la $a2, 1 #
syscall #
move $t1, $v0
sb $t1, ($s2) # Concat the current byte
addi $s2, $s2, 1 #
beq $t1, $zero, print_word # If there's no more characters to read, skip to print word
beq $t1, $s0, print_word # If the current byte is the separator, skip to print word
j str_read # Otherwise, continue reading the word
print_word:
la $a0, ($s2) # Load address of the 'copied' word
li $v0, 4 # Show word
syscall
j exit
new_line:
la $a0, newline # Print a new line
li $v0, 4
syscall
jr $ra
exit:
li $v0, 10 # Exit the program
syscall
This is
the answer it's always printing

MIPS printing strange empty square

I am writing a code to read a NxN array using macros in MIPS, it has no problems reading, but when the function printString is called after a few uses, it starts printing a strange square. I found that commenting the line sw $v0, ($t9) in readArray will make the function print as expected, however the contents that were read won't be saved anymore (and the function will be useless).
My code is the following:
.data
.macro printString(%string)
.data
string: .asciiz %string
.text
li $a0, 0
li $v0, 0
la $a0, string
li $v0, 4
syscall
.end_macro
.macro printInt(%int)
.text
li $a0, 0
li $v0, 0
add $a0, $zero, %int
li $v0, 1
syscall
.end_macro
.macro alloc(%size)
.text
li $a0, 0
li $v0, 0
add $a0, $zero, %size
li $v0, 9
syscall
.end_macro
.macro end()
.text
li $v0, 10
syscall
.end_macro
.macro readInt()
.text
li $v0, 5
syscall
.end_macro
.macro readArray(%array, %size)
.data
array: %array
.text
rAMain:
la $t4, array
add $t5, $zero, %size
li $t6, 0
li $t7, 0
rALoop:
arrayIndex($t4, $t5, $t6, $t7)
move $t9, $v0
printString("Write a value:[")
printInt($t6)
printString("][")
printInt($t7)
printString("]: ")
readInt()
sw $v0, ($t9)
addi $t7, $t7, 1
blt $t7, $t5, rALoop
li $t7, 0
addi $t6, $t6, 1
blt $t6, $t5, rALoop
.end_macro
.macro arrayIndex(%array, %columnNumber, %i, %j)
.data
array: %array
.text
la $t0, array
add $t1, $zero, %i
add $t2, $zero, %j
add $t3, $zero, %columnNumber
mul $v0, $t1, $t3
add $v0, $v0, $t2
sll $v0, $v0, 2
add $v0, $t0, $v0
.end_macro
.text
main:
jal readN
alloc($s1)
move $s0, $v0
readArray($s0, $s1)
end()
readN: ######
printString("Write the value of N: ")
readInt()
move $s1, $v0
jr $ra
Picture of the error:
Usage of:
.data
array: %array
in your readArray macro does not allocate any storage when the corresponding parameter is a register — in fact, array: $t0 doesn't make any sense to me, but somehow MARS allocated zero bytes of storage after the array label without complaining of syntax error.
The data declarations .data ... array: are global declarations, which are performed at compile/assemble time, rather than dynamic allocations performed at runtime.  This is one reason I'm surprised that MARS accepts a register name as an argument here, but none-the-less, it surely isn't doing what you want.

MIPS : Printing n time a number in a file

I'm currently working on a project for school. I'm asked to generate a random list of number in a text file, and first of all I tried to print n times a number in a test file, 31 for example. However, no matter the value of n, I always get my test.txt file with "31 " printed in but only one time. Here's my code (I'm a beginner btw) :
.data
question: .asciiz "\nEnter a number : ?\n"
mynumber: .asciiz "31 "
file: .asciiz "test.txt"
.text
.globl __start
__start:
li $v0, 4
la $a0, question
syscall
li $v0, 5
syscall
move $t0 $v0
li $t1 0
Loop:
blt $t0 $t1 exit
addi $t1 $t1 1
jal open_file
jal fill_file
jal close_file
j Loop
open_file:
li $v0, 13
la $a0, file
li $a1, 1
li $a2, 0
syscall
jr $ra
fill_file:
move $a0, $v0
li $v0, 15
la $a1, mynumber
li $a2, 3
syscall
jr $ra
close_file:
li $v0, 16
syscall
jr $ra
exit:
li $v0, 10
Does somebody have a solution for my problem? Would appreciate ! (Sorry for my english, not a native speaker)

MIPS: writing on file, encoding problems

whenever I'm trying to write something on a file. I want the user to write the string that will be then saved into this file instead of having one previously prepared. But, when I do this, every word saved into the file ends with ^# so my usual text editor (gedit) won't read it for encoding reasons (but that's not a problem, emacs does the work successfully) and those characters marks somehow the end of the text (even if there's other following).
How can I remove it?
.data
fout: .asciiz "text.txt"
buffer: .space 100
cont: .asciiz "reading file... "
stringname: .space 10
nl: .asciiz "\n"
.text
main:
create:
#input a name
move $s6, $v0
li $v0, 8
la $a0, stringname
li $a1, 5
syscall
#open
li $v0, 13
la $a0, fout
li $a1, 9
li $a2, 0
syscall
move $s6, $v0
#write on file
li $v0, 15
move $a0, $s6
la $a1, stringname
li $a2, 5
syscall
#close
li $v0, 16
move $a0, $s6
syscall
exit:
li $v0, 10
syscall

What does the following QtSPIM/MIPS code do

What does the following QtSPIM/MIPS code do. Describe by referring to the functions of various blocks of the code (Block1, Block2, …).
Answer the questions in front of certain instructions.
Block1:
.text
main:
li $s4, 0
la $a0, input
li $v0, 4
syscall
li $v0, 5
syscall
move $t0, $v0 #Why do we need this?
Block2:
la $a0, output
li $v0, 4
syscall
move $a0, $t0
li $v0, 1
syscall
la $a0, newline # How will the output change if newline is replaced by newspace defined as “ “?
li $v0, 4
syscall
Block3:
Do:
move $a0, $s4
li $v0, 1
syscall
la $a0, newline
li $v0, 4
syscall
slt $s1, $s4, $t0
addi $s4, $s4, 1
bgt $s1, $zero, Do # Why can’t we use “beq $s1, 1, Do “?
Block4:
Exit:
li $v0, 10
syscall
.data
input: .asciiz "Input a number: "
output: .asciiz "Let me count till "
newline: .asciiz "\n "
So I need help with identifying what the code does... I have the concept I just want o make sure Im properly figuring this out... So I have this so far:
move $t0, $v0 #Why do we need this? --- to store number in t0
la $a0, newline # How will the output change if newline is replaced by newspace defined as “ “?
--- load address of string to be printed into $a0 and adds the space if replaced by newspace
the last one I cant quite figure out...