I have added a mips code but when I try to plug in 100 it says grade is incorrect but i need to say incorrect when if(grade>100 ||grade<0) - mips32

I have added a mips code but when I try to plug in 100 it says grade is incorrect but i need to say incorrect when if(grade>100 ||grade<0)
please let me know how can I do my if else-statement.
the question is if(grade>100 || grade<0) output should be grade is incorrect
.data
enter: .asciiz "Enter an integer : "
name: .asciiz "\n Number is incorrect. Try Again:"
gradeA: .asciiz "\n Grade is A"
gradeB: .asciiz "\n Grade is B"
gradeC: .asciiz "\n Grade is C"
gradeD: .asciiz "\n Grade is D"
gradeF: .asciiz "\n Grade is F"
.text
.globl main
main:
li $v0, 4
la $a0, enter
syscall
li $v0, 5
syscall
add $s0, $v0, $0
li $t1,100
blt $s0,$t1,l1
bgt $s1,0,l1
j exit
l1:
la, $a0,name
blt $s0,90,l2
la $a0,gradeA
j exit
l2:
blt $s0,80,l3
la $a0,gradeB
j exit
l3:
blt $s0,70,l4
la $a0,gradeC
j exit
l4:
blt $s0,60,l5
la $a0,gradeD
j exit
l5:
la,$a0,gradeF
exit:
li $v0, 4
syscall
li $v0, 10
syscall

The code for loop L1 is having a line to print the non accepting cases, you can simply tackle it in a separate loop. blt check if input is less than 100 and the other loops follows as they were before. The other bgt loop which is mentioned in the code below check if the input is greater to hundred and diverts it to the rejecting loop L6.
.data
enter: .asciiz "Enter an integer : "
name: .asciiz "\n Number is incorrect. Try Again:"
gradeA: .asciiz "\n Grade is A"
gradeB: .asciiz "\n Grade is B"
gradeC: .asciiz "\n Grade is C"
gradeD: .asciiz "\n Grade is D"
gradeF: .asciiz "\n Grade is F"
.text
.globl main
main:
li $v0, 4
la $a0, enter
syscall
add $s2, $zero, -1
li $v0, 5
syscall
add $s0, $v0, $0
li $t1,100
blt $s0,$t1,l1
bgt $s0,$t1,l6 ##checks if input is grater than hundread diverts to l6
ble $s0,$s2,l6
bgt $s0,0,l1
j exit
l1:
blt $s0,90,l2
la $a0,gradeA
j exit
l2:
blt $s0,80,l3
la $a0,gradeB
j exit
l3:
blt $s0,70,l4
la $a0,gradeC
j exit
l4:
blt $s0,60,l5
la $a0,gradeD
j exit
l5:
la,$a0,gradeF
j exit
l6:
la,$a0,name ##prints the case is incorrect
j exit
exit:
li $v0, 4
syscall
li $v0, 10
syscall
The code show incorrect number for numbers greater than 100. And for numbers less than zero it assume the grade to be F. If you want the code to indicate incorrect number for negative numbers or float numbers less than zero you can visit :
http://www.cs.jhu.edu/~jorgev/cs333/reference.html
and
https://s3-eu-west-1.amazonaws.com/downloads-mips/documents/MIPS_Warrior_M5150_SoftwareUserManual_MD00980_01.05.pdf
For this you have to change the code to float input and use sltu to have an hi or lo register and compare them to get the required error displayed on console.
You can also compare a negative lower limit to the input using ble.

Related

Multiplying and Combining Matrices

Hi i am trying to write a code in mips assembly. The purpose of the code is to multiply two matrices. the user will be asked to enter two matrix parameters, the program will combine them and then the user will be asked to enter values into the matrix. finally the program will print the matrix. eg: user enters 3 x 2, 2 x 7 the program combines them to get 3 x 7, the user enters 21 values and then the matrix is printed
.text
start:
li $v0 4
la $a0 msg1
syscall
li $v0 8
la $a0 matrix1
li $a1 3
syscall
li $v0 4
la $a0 msg2
syscall
li $v0 8
la $a0 matrix2
li $a1 3
syscall
li $s0 2
lb $t0, matrix1($s0)
lb $t1, matrix2($0)
beq $t1 $t0 values
li $v0 4
la $a0 msg5
syscall
exit: li $v0 10
syscall
values: lb $t0, matrix2($s0)
sb $t0 matrix1($s0)
lb $t1, matrix1($0)
li $v0 4
la $a0 msg3
syscall
mult $t0 $t1
mflo $s0
loop: beq $s1 $s0 print_matrix
li $v0 5
syscall
sb $v0 matrix_values($t2)
addi $s1 $s1 1
addi $t2 $t2 1
j loop
print_matrix: li $v0 4
la $a0 msg4
syscall
li $v0 4
la $a0 matrix1
syscall
print: beq $s2 $s0 exit
li $v0 1
lb $a0 matrix_values($t3)
syscall
li $v0 4
la $a0 spaces
syscall
addi $s2 $s2 1
addi $t4 $t4 1
bne $t0 $t4 print
li $v0 4
la $a0 newline
syscall
li $t4 0
j print
.data
msg1: .asciiz "please enter first matrix parameters"
msg2: .asciiz "please enter second matrix parameters"
msg3: .asciiz "please enter matrix values"
msg4: .asciiz "the result is"
msg5: .asciiz "error"
spaces: .asciiz " "
newline: .asciiz "\n"
matrix1: .space 3
matrix2: .space 3
matrix_values: .byte
.align 0

Subtracting two user input numbers, with a move and display the result

I'm new to MIPS and I'm using MARS. I can't get my move right and when I execute it gives me some nuts o number. Here is what I have so far, any help would be appreciated.
.data
message1: .asciiz "Enter the any number to subtract :"
message2: .asciiz "\nEnter the any number to subtract :"
n1 : .word 0
n2 : .word 0
message3: .asciiz "\nThe subtraction of the two numbers is "
.text
main:
li $v0 4 #print out message1
la $a0 message1
syscall
li $v0 5 #read message1 as number1
syscall
sw $v0 n1 #store number
li $v0 4 #print out message2
la $a0 message2
syscall
li $v0 5 #read message2 as number2
syscall
sw $v0 n2 #store number
li $v0 4
la $a0 message3
syscall
lw $t0 n1
lw $t1 n2
sub $t0, $v0, $v0 # t0 = number1 s1 - number2 s2
li $v0, 1 # print integer
move $t0, $a0 # move t0 to a0
syscall # run
Your code is doing well until here sub $t0, $v0, $v0.When you subtract you should put the the result in the argument register $a0then you can use move to put that result in return register $v0 for printing.
change them as following it will work.
sub $t2, $t0, $t1 # t2 = t0 - t1
move $a0, $t2 # copy t2 to a0
li $v0, 1 # print integer
syscall #
Another way of doing this subtraction is that you don't need .word and lwat all. As following
.data
message1: .asciiz "Enter the any number to subtract :"
message2: .asciiz "\nEnter the any number to subtract :"
message3: .asciiz "\nThe subtraction of the two numbers is "
.text
main:
li $v0 4 #print out message1
la $a0 message1
syscall
li $v0 5 #read message1 as number1
syscall
move $t0,$v0 # set $t0 to the content of $v0
li $v0 4 #print out message2
la $a0 message2
syscall
li $v0 5 #read message2 as number2
syscall
move $t1,$v0
li $v0 4
la $a0 message3
syscall
sub $a0, $t0, $t1 # t0 = number1 t1 = number2
li $v0, 1 # print integer
syscall # run

How to design a program that finds the sum of the integers from 1 to N, where N is a value read on the keyboard?

This is my first attempt at MIPS and I designed a pseudocode description of a possible algorithm. The code outline is as follows:
main: Print: "Please input a value for N ="
read v0
If(v0 >0)
{
t0 = 0
while(v0 > 0) do
{
t0 = t0 + v0
v0 = v0 +v0 -1
}
print: The sum of integers from 1 to N is = ", t0
go to main
}
else
print: "Honest Abe"
so, with this outline, I made somewhat of an attempt to translate this into MIPS but the whole storing variables and reading them in is confusing me. My attempt of the real code is:
.data
Prompt: .asciiz "\n Please input a value for N="
Result: .asciiz "\n The sum of integers from 1 to N is "
Bye: .asciiz "\n ***Honest Abe***"
.text
main:
li $v0,4 #load $v0 with print_string code
la $a0, Prompt #load $a0 with the message to be displayed
syscall
bgz $v0, else
li $t0, 0
while:
bgz $v0
add $t0,$t0,$v0
addi $v0,$vo -1
From here, I do not understand how the for loops work in MIPS and I do not understand if this is even the correct approach. The whole idea of reading in numbers and then storing the integers isn't processing in my head. Also, I am unsure if I am using the appropriate commands for the loop. Help to any of these questions would be great!
Your algorithm seems a bit odd and too much of a hassle, if I got right what you wanted to do.
You can compute a sum until a certain number with a simple for loop. As in C:
scanf("%d", &num);
for(i=0;i<num;i++)
sum=sum+i;
like an array.
With a little help from the reference card of MARS, you can end up with something like this:
.text
main:
li $v0, 5 #read a number from the keyboard
syscall
addi $s0, $v0, 1 #s0= the number from the keyboard + 1 (the limit)
li $t0, 1 #counter set to 1
loop:
beq $t0, $s0, exit #if the counter becomes equal tothe limit, exit the loop
add $t1, $t1, $t0 #add the counter to the sum to this point
addiu $t0, $t0, 1 #add 1 to the counter for each loop
j loop #go to the "loop" label above
exit:
li $v0, 1
move $a0, $t1
syscall
li $v0, 10
syscall

comparing a string in MIPS

Could someone tell me what is wrong with my code so far. I am trying to make a program that takes input from the user (roman numerals) and then converts it to integers. So far this is what I have:
.data
buffer: .space 20
onlyCaps: .asciiz "Please enter only Capital Numbers\n"
enter1: .asciiz "Number out of range. Please enter another number\n"
enter2: .asciiz "Please enter your roman numeral: "
debug: .asciiz "reach"
M: .asciiz "1000"
D: .asciiz "500"
C: .asciiz "100"
L: .asciiz "50"
X: .asciiz "10"
V: .asciiz "5"
I: .asciiz "1"
.text
main:
la $a0, onlyCaps # "Enter Only Capital numbers"
li $v0, 4
syscall
la $a0, enter2 #prompt user with "Please enter a roman numeral:"
li $v0, 4
syscall
la $a0, buffer #load byte space into address
li $a1, 3 # allot the byte space for string
li $v0, 8 #read user input, j
syscall
li $t4, 1
loop:
lb $t0, 0($a0) # Save $v0 value to $t0
beqz $t4, done # if it is equal to zero end the loop
add $a0, $a0, $t4 #increment the address
j mCheck
#while loop ends here
mCheck:
beq $t0, 'M', mChar # if $t0 equal M go to mChar
bne $t0, $t1, dCheck # move to see if equals D
dCheck:
beq $t0, 'D', dChar # if $t0 equal D go to dChar
bne $t0, $t1, cCheck
cCheck:
beq $t0, 'C', cChar # if $t0 equal C go to cChar
bne $t0, $t1, lCheck
lCheck:
beq $t0, 'L', lChar # if $t0 equal L go to lChar
bne $t0, $t1, xCheck
xCheck:
beq $t0, 'X', xChar # if $t0 equal X go to xChar
bne $t0, $t1, vCheck
vCheck:
beq $t0, 'V', vChar # if $t0 equal V go to vChar
bne $t0, $t1, iCheck
iCheck:
beq $t0, 'I', iChar # if $t0 equal I go to iChar
bne $t0, $t1, error
mChar:
la $a0, M #puts one hundred in $t3
li $v0, 4
syscall
j loop
dChar:
la $a0, D # prints out 500
li $v0, 4
syscall
j loop
cChar:
la $a0, C # prints out 500
li $v0, 4
syscall
j loop
lChar:
la $a0, L # prints out 500
li $v0, 4
syscall
j loop
xChar:
la $a0, X # prints out 500
li $v0, 4
syscall
j loop
vChar:
la $a0, V # prints out 500
li $v0, 4
syscall
j loop
iChar:
la $a0, I # prints out 500
li $v0, 4
syscall
j loop
error:
la $a0, enter1 # Print error meg. then back to main
li $v0, 4
syscall
j done
done:
li $v0, 10 # Exit
syscall
My question is how can you take input from the user and put it in $a0, and then use a while loop after that? I can't get the bytes to go to the next spot (i.e. MXX to go from M to X.) currently the program reads M, but not X. Eventually I will switch the program to sum these numbers and to also check to see if the number before is less (i.e. IV) to account for those roman numerals but I need help with the while loop first.

MIPS Program with Integer and Argument Example

I'm stuck on an exercise, and am unsure how to proceed. This is the exercise:
Write a MIPS assembly language procedure, Test, that accepts 2 integers as arguments and
returns 0 if the integers are equal, 1 if the first is less than the second, and 2 if the first is greater
than the second.
Write a MIPS assembly language program that reads in 2 integers, calls the procedure Test, then
outputs one of the following messages:
The integers are equal
The first integer is less than the second
The first integer is greater than the second
What would be an example to carry this out? Mips is very confusing to me, as I'm used to Java. Thank you.
EDIT: Here is the program I am using as a foundation, since I am unsure where to start:
.data
str1: .asciiz "Please Enter Integer 1: " # a
str2: .asciiz "Please Enter Integer 2: " # a
str3: .asciiz "The sum is " # a
newline: .asciiz "\n" # g
.text
main: addi $v0, $zero, 4
la $a0, str1
syscall
addi $v0, $zero, 5
syscall
add $s0, $zero, $v0
addi $v0, $zero, 4
la $a0, str2
syscall
addi $v0, $zero, 5
syscall
add $s1, $zero, $v0
L1: beq $s1, $zero, cont
addi $v0, $zero, 1
add $a0, $s0, $zero
addi $s1, $s1, -1
syscall
j L1
cont: addi $v0, $zero, 4
la $a0, newline
syscall
addi $v0, $zero, 10
syscall
jr $ra
To check whether one register's value is smaller than another register's value, we can use the
set-on-less-than instruction, which also has a set-on-less-than-immediate counterpart.
slt $r0, $r3, $r4
()if r3 < r4, r0 is set to 1
else r0 is set to 0
slti $r0, $r3, 10
()if r3 < 10, r0 is set to 1
else r0 is set to 0
slt and slti are similar to beq or bne, however, there are two differences. First, they test
whether one value is smaller than another value and, second, they don't branch to some
address, but, instead, set a flag, stored in the first operand.

Resources