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
Related
I'm learning MI trying to write a factorial program in MIPS Assembly code for which
3! = 6 (my output shows it 36)(input next to output)
4! = 24(my output shows it as 424)
Here is my code. What should I do to get rid of printing the input?
.data
num: .asciiz "\nPlease enter a number: "
num2: .asciiz"\nPlease give your second number : "
respon: .asciiz "\nThe factorial of the entered number is: "
nl: .asciiz"\n"
.text
fact:
beqz $a0,return1
li $v0, 1
li $t0, 1
fact_loop:
bgt $t0, $a0, end_fact_loop
mul $v0, $v0, $t0
addi $t0, $t0, 1
j fact_loop
end_fact_loop:
jr $ra
return1:
li $v0, 1
jr $ra
main:
li $v0, 4
la $a0, num
syscall
li $v0, 5
syscall
move $t0, $v0
li $v0, 4
la $a0, respon
syscall
li $v0, 1
move $a0, $t0
syscall
jal fact
move $t0, $v0
li $v0, 1
move $a0, $t0
syscall
li $v0, 4
la $a0, nl
syscall
################################
li $v0, 4
la $a0, num2
syscall
li $v0, 5
syscall
move $t0, $v0
li $v0, 4
la $a0, respon
syscall
li $v0, 1
move $a0, $t0
syscall
jal fact
move $t0,$v0
li $v0, 1
move $a0, $t0
syscall
li $v0, 10
syscall
To get rid of printing the input you have to remove the syscall you are issuing to print them.
That is, before jal-ing fact (both times) you are issuing these instructions:
li $v0, 1
move $a0, $t0
syscall
Just, remove the first and third instruction and keep the move as you use it on your fact routine:
move $a0, $t0
A few suggestions:
Add a comment to each functional chunk of code.
Use a single-step to debug so you can see what's happening where.
Simplify the code to a minimal failing example. If you have two chunks of code both exhibiting the same problem, remove one of them and focus on the first without distractions and noise from the second.
Prune unnecessary code. For example, the return1: block and beqz $a0,return1 are superfluous because the main factorial loop will exhibit the same behavior automatically -- you don't need to handle $a0 == 0 specially.
Having followed these tips, I wound up with the following program:
.data
num: .asciiz "\nPlease enter a number: "
num2: .asciiz "\nPlease give your second number : "
respon: .asciiz "\nThe factorial of the entered number is: "
nl: .asciiz "\n"
.text
fact:
li $v0, 1
li $t0, 1
fact_loop:
bgt $t0, $a0, end_fact_loop
mul $v0, $v0, $t0
addi $t0, $t0, 1
j fact_loop
end_fact_loop:
jr $ra
main:
# print prompt "Please enter a number: "
li $v0, 4
la $a0, num
syscall
# get user input for first fact call
li $v0, 5
syscall
move $t0, $v0
# call fact(n)
move $a0, $t0
jal fact
move $t0, $v0
# print "factorial is..."
li $v0, 4
la $a0, respon
syscall
# print the result of fact(n)
li $v0, 1
move $a0, $t0
syscall
# print newline
li $v0, 4
la $a0, nl
syscall
# exit program
li $v0, 10
syscall
The problem of double-printing is caused by this code:
li $v0, 1
move $a0, $t0
syscall
The move $a0, $t0 is necessary to set up the fact call, but we don't want li $v0, 1 and syscall, which prints the argument without a newline before the result of fact, causing your unwanted output.
A good technique to prevent this in the future is ensuring your argument set-up for the function call to fact happens right before the jal.
I'll leave it as an exercise to adapt this to your second chunk of code, which has the same extra syscall to service 1 (print integer).
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
First i know ive asked tons of questions but ive come a long way since i last asked.
Im using mars simulator for some side project learning.
Ive always been interested in assembly and mips is my choice.
Anyways my program seems to crash after i try and move my values and add them up and i cant figure out why.
Thanks for looking and tips will be great.
Have a good day
.data
msg0: .asciiz "enter a number: \n"
msg1: .asciiz "enter another number: \n"
result: .asciiz "result is: \n"
.text
li $v0, 4
la $a0, msg0 #load first message
syscall
li $v0 5
move $t0, $v0 #user input
syscall #store number
li $v0, 4
la $a0, msg1 #second msg
syscall
li $v0, 5
move $t1, $v0 #second input and store number
syscall
li $v0, 4
la $a0, result #rsult message
syscall
add $t3,$t1,$t0 #and my problem is here for some reason?
syscall
You need to add a label that are function names. In the code I have provided below, the function name is main. Also, the last system call serves no purpose now. What you need to do is indicate that your code has finished before making the last system call by li $v0,10.
.data
msg0: .asciiz "enter a number: \n"
msg1: .asciiz "enter another number: \n"
result: .asciiz "result is: \n"
.text
.globl main
main:
li $v0, 4
la $a0, msg0 #load first message
syscall
li $v0 5
syscall
move $t0, $v0 #first input and store number
li $v0, 4
la $a0, msg1 #second msg
syscall
li $v0, 5
syscall
move $t1, $v0 #second input and store number
li $v0, 4
la $a0, result #result message
syscall
add $t3,$t1,$t0
move $a0, $t3
li $v0, 1
syscall #print answer
li $v0,10
syscall
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)
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...