MIPS BMI calculator print issue - mips

.data
wpr:.asciiz "Weight (whole pounds): "
hpr:.asciiz "Height (whole inches): "
bpr:.asciiz "\nCalculated BMI: "
weight: .word
height: .word
bmi: .float
.text
main:
li $v0 33
li $a0 52
li $a1 20
li $a2 22
li $a3 127
syscall
# Prompt weight
la $a0 wpr
li $v0 4
syscall
# Load input into saved register $s0
li $v0 5
syscall
move $s0 $v0
# Prompt height
la $a0 hpr
li $v0 4
syscall
# Load input into saved register $s1
li $v0 5
syscall
move $s1 $v0
# Calculations
mul $s0 $s0 703
mul $s1 $s1 $s1
mtc1 $s0 $f20
cvt.s.w $f20 $f20
mtc1 $s1 $f21
cvt.s.w $f21 $f21
div.s $f12 $f20 $f21
# Output BMI
li $v0 4
syscall
la $a0 bpr
syscall
li $v0 2
syscall
When I run this code Everything works fine but I get the height prompt printing twice. I dont know what the issue is. Heres an example of the output
Weight (whole pounds): 150
Height (whole inches): 71
Height (whole inches):
Calculated BMI: 20.918468

Change your last block of code (with "# Output BMI") to
# Output BMI
la $a0 bpr
li $v0 4
syscall
li $v0 2
syscall
Currently you tell the system to output to the prompt first, which still contains your previous height prompt.

Related

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)

Swap procedure not working in MIPS

The objective of this program is to swap two numbers without using temporary variables. I came up with a solution by just using math but when I assemble, the numbers don't swap and I can't figure out why. Can anyone explain my mistake?
.text
main:
la $a0,n1
la $a1,n2
jal swap
li $v0,1 # print n1 and n2; should be 27 and 14
lw $a0,n1
syscall
li $v0,11
li $a0,' '
syscall
li $v0,1
lw $a0,n2
syscall
li $v0,11
li $a0,'\n'
syscall
li $v0,10 # exit
syscall
swap: #this is my code and where I'm having problems
sub $a0, $a0, $a1
add $a1, $a1, $a0
sub $a0, $a1, $a0
jr $ra
.data
n1: .word 14
n2: .word 27

MIPS multiplication using addition

sorry, I try to multiply two integers but it doesn't work. I can't find where the problem is.Maybe because of register' names but I don't know how to correct it. I correct many times but it is not successful. Could anyone give me some points?
.data
prompt1: .asciiz "Please enter the first signed (decimal) integer: "
prompt2: .asciiz "Please enter the second signed (decimal) integer: "
result_msg: .asciiz "The result of these two 16-bit integers\' multiplication is: "
.text
.globl main
main:
li $v0, 4 #print prompt
la $a0, prompt1
syscall
li $v0, 5 #read multiplicand
syscall
move $s0, $v0
li $v0, 4 #print prompt
la $a0, prompt2
syscall
li $v0, 5 #read multiplier
syscall
move $s1, $v0
Mult: ori $t0,$zero,1 #mask
move $s3, $0 #initialize the result register
move $t1, $0
loop: beq $s1, $zero, end #if the multiplier is 0 then finished
and $t1, $t0, $s1 #mask
beq $t1, 1, mult_add
beq $t1, 0, shift
mult_add: addu $s3, $s3, $s0 #add to get product
shift:
sll $s0, $s0, 1 #shift multiplicand left
srl $s1, $s1, 1 #shift multiplier right
j loop
end:
jr $ra
result: #input the print_string
li $v0, 4
la $a0, result_msg
syscall
exit:
li $v0, 1 #input result
move $a0, $s3
syscall
li $v0, 10 #exit
syscall
Inspecting your code I see that you jump to label end when you are done multiplying.
The instruction at that label issues a jr $ra which "returns from a function", but I guess you just want to print the result and exit.
Therefore I'd suggest you remove that instruction so as to print the result and exit and maybe remove label result as it is not used anywhere in your code.

Sum of two numbers in MIPS

I'm trying to practice my coding skill in MIPS (this is my first time ever learning an assembly language). I wrote this code below to sum up two user's inputs, and it is correct. However, the code is quite long..so, is there any way to optimize this code so it will be shorter? i need some suggestions. Thanks
.data
n1: .asciiz "enter your first number: "
n2: .asciiz "enter your second number: "
result: .asciiz "result is "
.text
#getting first input.
la $a0, n1
li $v0, 4
syscall
li $v0, 5
syscall
move $t0, $v0
#getting second input.
la $a0, n2
li $v0, 4
syscall
li $v0, 5
syscall
move $t1, $v0
#calculate and print out the result.
la $a0, result
li $v0, 4
syscall
add $t3, $t0, $t1
move $a0, $t3
li $v0, 1
syscall
#end program.
li $v0, 10
syscall
I also have wrote a program to calculate a factorial number. There are better ways to do this?
.data
str: .asciiz "Enter a number: "
result: .asciiz "The result is: "
.text
la $a0, str
li $v0, 4
syscall
li $v0, 5
syscall
move $s0, $v0 # move N into s0
li $s2, 1 # c = 1
li $s1, 1 # fact = 1
LOOP:
blt $s0, $s2, PRINT # if (n < c ) print the result
mul $s1, $s1, $s2 # fact = fact * c
add $s2, $s2, 1 # c = c + 1
j LOOP
PRINT:
la $a0, result
li $v0, 4
syscall
add $a0, $s1, $zero
li $v0, 1
syscall
li $v0, 10
syscall
The programs look ok, except for the use of the temporary registers $t0,...,$t9. These registers are not guaranteed to be preserved when another function is called, or when a syscall is issued. The $s0,...,$s7 registers are preserved across calls.
You need to replace: move $t0, $v0 with move $s0, $v0; move $t1, $v0 with move $s1, $v0 and add $t3, $t0, $t1 with add $s3, $s0, $s1.

swap mips variables not being saved

.text
main:
la $a0,n1
la $a1,n2
jal swap
li $v0,1 # print n1 and n2; should be 27 and 14
lw $a0,n1
syscall
li $v0,11
li $a0,' '
syscall
li $v0,1
lw $a0,n2
syscall
li $v0,11
li $a0,'\n'
syscall
li $v0,10 # exit
syscall
swap:
xor $a0 $a0 $a1
xor $a1 $a0 $a1
xor $a0 $a0 $a1
jr $ra
L1:
.data
n1: .word 14
n2: .word 27
ok so my goal was to add the goal to swap
however, no matter what I do with the variables inside swap it doesn't seem to be saved
i am editing a0 and a1, shouldn't that work?
the code always prints 14 and 27