I created an array in MIPS and this language is very new to me. Im suppose to show the number at position one instead i get position 2 which is 3
#create a simple idea of an array
.data
num1: .word 1, 2, 3 #allows input for slot one in array
label1: .asciiz "Input the first number:"
label2: .asciiz "Input the second number:"
label3: .asciiz "Input the third number:"
label4: .asciiz "The number in slot 1 of the array is:"
.text
main:
#intiate the first label
li $v0, 4
la $a0, label1
syscall
#input first number into the array
li $v0, 5
syscall
sw $v0, num1
#initiate the second label
li $v0,4
la $a0, label2
syscall
#input second number into the array
li $v0, 5
syscall
sw $v0, num1
#initiate the third label
li $v0, 4
la $a0, label3
syscall
#input third number into the array
li $v0, 5
syscall
sw $v0, num1
#intiate the fourth label
li $v0, 4
la $a0, label4
syscall
#output position 1
li $v0, 1
lw $a0, num1
syscall
#end program
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).
I'm new to MIPS and I've written a program to add two user-selected numbers but it's not working. It says Instruction references undefined symbol at 0x00400014. Can someone please go through my code and explain what's wrong? Thank you.
.data
msg1: .asciiz "Enter addend 1: "
msg2: .asciiz "Enter addend 2: "
msg3: .asciiz "Sum = "
.text
li $v0, 4
la $a0, msg1
syscall
li $v0, 5
syscall
move $t0, $v0
li $v0, 4
la $a0, msg2
syscall
li $v0, 5
syscall
move $t1, $v0
add $t2, $t0, $t1
li $v0, 4
la $a0, msg3
syscall
li $v0, 1
move $a0, $t2
syscall
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 want to input an integer and have it be expressed in both binary and hex. Then I want to move bits 12, 13, 14, and 15 to the least significant bits of $a0, and have the output be expressed as a binary and hex. Here's my program:
.data
enter: .asciiz "Please enter your integer:\n"
binaryI: .asciiz "\nHere is the input in binary: "
hexI: .asciiz "\n\nHere is the input in hexadecimal: "
binaryO: .asciiz "\n\nHere is the output in binary: "
hexO: .asciiz "\n\nHere is the output in hexadecimal: "
.text
prompt:
li $v0, 4
la $a0, enter
syscall
li $v0, 5
syscall
add $s2, $0, $v0
li $v0, 4
la $a0, binaryI
syscall
li $v0, 35
move $a0, $s2
syscall
li $v0, 4
la $a0, hexI
syscall
li $v0, 34
move $a0, $s2
syscall
addi $t0, $0, 7
srl $s0, $s2, 12
and $s0, $s0, $t0
li $v0, 4
la $a0, hexO
syscall
li $v0, 35
move $a0, $s0
syscall
li $v0, 4
la $a0, binaryO
syscall
li $v0, 34
move $a0, $s0
syscall
li $v0, 1
add $a0, $0, $s0
syscall
li $v0, 10
syscall
For the integer 1006460, for example, the inputs and hex output work perfectly, but the binary output has an extra 5 at the end. The error I get is this:
Here is the output in binary: 0x000000055
What may have caused this extra 5 to be at the end of the output?
How silly of me. I should have deleted
li $v0, 1
add $a0, $0, $s0
syscall
from my code.
I am new in mips and i got this assignment that asks me to take 2 inputs from user and divide them and get a floating point output.
The problem is the output comes out like this 0.000000000, not the predicted output
this is my code
.data
str1: .asciiz "Enter a: "
str2: .asciiz "Enter b: "
str3: .asciiz " a/b = "
.text
main: li $v0, 4
la $a0, str1
syscall
li $v0, 6
syscall
add $s0, $v0, $zero
li $v0, 4
la $a0, str2
syscall
li $v0, 6
syscall
move $s1, $v0
div $s0, $s1
li $v0, 4
la $a0, str3
syscall
li $v0, 2
move $a0, $t0
syscall
li $v0, 10
syscall
what should i do?
This code works for me.
.data
str1: .asciiz "Enter a: "
str2: .asciiz "Enter b: "
str3: .asciiz " a/b = "
.text
.globl main
main:
#prompt for "a"
li $v0, 4
la $a0, str1
syscall
#User input for "a"
li $v0, 6 #The float value that is read is will be in $f0 register
syscall
mov.s $f3, $f0 #moving the value of "a" into f3 to reuse f0
#prompt for "b"
li $v0, 4
la $a0, str2
syscall
#user input for "b"
li $v0, 6 #The float value that is read is will be in $f0 register
syscall
mov.s $f4, $f0
#Dividing "a" and "b"
div.s $f12 $f3 $f4
#prompt for "a/b"
li $v0, 4
la $a0, str3
syscall
#Displaying contents of f12
li $v0, 2
syscall
li $v0, 10
syscall