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
Related
I have the following mips code (running it in QTSPIM), that is supposed to count the number of characters in a string and print them.
The logic behind it is very simple but is does not work as it should. Everything goes well until it reaches the end of the string and then it continues counting even though I compare each element to $zero to find the end of string (\0).
Is there something wrong with my condition to exit the loop, or my_string does not contain \0 in the end so it won't exit?
.data
endl: .asciiz "\n"
my_string: .asciiz "thisisastring"
star: .asciiz "*"
str_end: .word 0
space: .asciiz " "
.text
.globl main
main:
la $a0, my_string
li $v0, 4
syscall
la $a0, endl
li $v0, 4
syscall
la $t0, my_string # load mystring to $t0
li $t1, 0 # make $t1 = 0, character counter
lb $t2, ($t0) # make $t2 point to the first character of "my_string"
li $t3, 1 # $t3 is the ++ register to go to the next character
li $t4, 0 # character counter
la $t5, str_end
cont:
beqz $t0, print # if \0 is found print and exit
addi $t4, $t4, 1 # increase the counter
lbu $a0, ($t0) # print current character
li $v0, 11
syscall
addi $t0, $t0, 1 # go to next char
#move $t2, $t0
j cont
print:
move $a0, $t4
li $v0, 1
syscall
j exit
exit:
li $v0, 10
syscall
The problem is in the order of the instructions, the logic of the code.
Here is the corrected version with no redundant code:
.data
endl: .asciiz "\n"
my_string: .asciiz "thisisastring"
str_end: .word 0
.text
.globl main
main:
la $a0, my_string
li $v0, 4 # print the string
syscall
la $a0, endl # print endl
li $v0, 4
syscall
la $t0, my_string # load mystring to $t0
li $t1, 0 # make $t1 = 0, character counter
lb $t2, 0($t0) # make $t2 point to the first character of "my_string"
la $t5, str_end
cont:
lb $a0, 0($t0) # print current character
beqz $a0, print # if \0 is found print and exit
addi $t1, $t1, 1 # increase the counter
addi $t0, $t0, 1 # go to next char
li $v0, 11
syscall
j cont
print:
move $a0, $t1
li $v0, 1
syscall
j exit
exit:
li $v0, 10
syscall
I want to write a program which finds the maximum value in an array.
When I try to start my programm I get the error "Exception occurred at PC=0x0040004c". I have no idea what this means and I have no idea whats wrong with my code.
.data
liste: .word 1, 2 ,3 ,41, 5, 6, 7
.text
main:
la $a0 liste #adresse des ersten Elements in $a0 gespeichert
la $a1 28($a0) #adresse des letzten Elements in $a1 gespeichert
li $v0 0 #speichere 0 in v0
tester:
beq $a0 $a1 exit #test, if we are at the end of the array
j findmax
findmax:
la $t0 ($a0) #copy a0 in t0
speichermax:
la $v0 ($t0) #adress of t0 (Maximum) in v0
lw $v1 ($v0) #value of the max in v1
loop:
la $t0 4($t0) #go to the next field content
beq $v0 $a1 exit #end of field is reached
lw $t1 ($t0)
sub $t1 $t1 $v1 # t1 = t1 - v1
bgtz $t1 speichermax
#bgt $t1 $v1 speichermax #test if t1>v1 -> yes: save the new max in v1 (über speichermax)
j loop
exit:
lw $a0 ($t0) #print the maximum
li $v0 1
syscall
li $v0 10 #exit
syscall
Skipping the rest of the program I believe that this: la $a1, 28($a0)
is wrong. Try this: addi $s1, $a0, 28.
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
I am trying to get an input from user and compare it with (+ , - , *, /, =) and then make the corresponding arithmetic operation. But Mips cant figure out each symbol.it continues loop2 for ever.... I give a part of code... any idea what' s wrong? (Registers are all correct)
.data
op1: .asciiz "+"
op2: .asciiz "-"
op3: .asciiz "*"
op4: .asciiz "/"
op5: .asciiz "%"
op6: .asciiz "="
.text
loop2 :
li $v0, 4
la $a0, operator #Εμφάνισε μήνυμα "Τελεστής:"
syscall
addi $v0, $zero, 5 #Διάβασε τον τελεστή
syscall
move $s2 , $v0 #Βάλε τον τελεστή στον καταχωρητή $s2
#---------------------------------
sw $t1 ,op1 #Αν ο τελεστής είναι '+'
beq $s2, $t1, ADDITION
ADDITION:
li $v0, 4 #Εμφάνισε μήνυμα "Αριθμός:"
la $a0, number
syscall
addi $v0, $zero, 5 #Διάβασε τον αριθμό
syscall
move $s3 , $v0 #βάλε τον αριθμό στον καταχωρητή $s3
add $s4 , $s1 , $s3 # Πράξη πρόσθεσης των αριθμών.Το αποτέλεσμα αποθηκεύεται στον $s4
j loop2 #Εφόσον δεν ήταν "=" ο τελεστής, ζήτα τον επόμενο τελεστή
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.