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.
Related
So my test case is 7 for the multiplicand and 9 for the multiplier, I am trying to use Recursive Multiplication by using repeated addition, recursively. I'm a beginner in stacks in regards to MIPS so what could be the reason why nothing is wanting to output?
I am required to use $a0 -> Multiplicand, $a1 -> Multiplier, and $v0 -> Product
.text
main:
#Printing out prompt1
li $v0, 4
la $a0, prompt1
syscall
#Getting user input -> Multiplicand
li $v0, 5
syscall
move $a0, $v0
#Printing out prompt2
li $v0, 4
la $a0, prompt2
syscall
#Getting user input -> Multiplier
li $v0, 5
syscall
move $a1, $v0
#Loop
mult:
addiu $sp,$sp, -8
sw $a0, 4($sp)
sw $ra, 0($sp)
bne $a0, 1, else
add $v0, $v0, 0
j exit
else:
sub $a1, $a1, 1
jal mult
exit:
lw $a0, 4($sp)
add $v0, $v0, $a0
sw $v0, 4($sp)
addiu $sp, $sp, 8
jr $ra
lw $v0, multiplicand
li $v0, 1
la $a0, multiplicand
syscall
#Ending Program
li $v0, 10
syscall
.data
prompt1: .asciiz "Enter the multiplicand: "
prompt2: .asciiz "Enter the multiplier: "
multiplicand: .space 101
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
I am trying to take user input of three integers and pass them to a function, fme, which should return x^k mod n. When attempting to load the input from save registers to argument registers in order to run the function I get the error:
spim: (parser) syntax error on line 38 of file C:/Users/tamar/OneDrive/Documents/MIPS Projects/project2.s
lw $a0, $s0
Any suggestions for how I can execute this are appreciated. Here is my main.
main:
li $v0, 4
la $a0, out_string_x
syscall #print string (X)
li $v0, 5
syscall
move $s0, $v0 #take user input
li $v0, 4
la $a0, out_string_x
syscall #print string (K)
li $v0, 5
syscall
move $s1, $v0 #take user input
li $v0, 4
la $a0, out_string_x
syscall #print string (N)
li $v0, 5
syscall
move $s2, $v0 #take user input
#call fme function
lw $a0, $s0
lw $a1, $s1
lw $a2, $s2
jal fme
sw $v0, answer
#Display result
li $v0, 4
la $a0, result_message
syscall
li $v0, 1
lw $a0, answer
syscall
#end program
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)