Mips :verify if a matrix is symetric - mips32

How to verify if a matrix is symetric in mips ?
.data
string1: .asciiz "dati n \n"
string2: .asciiz "dati elem matrice \n"
n: .word 0
elem: .space 1024
.text
main:
li $v0,4
la $a0,string1
syscall
li $v0,5
syscall
sw $v0,n
lw $t0,n
mulo $t0,$t0,$t0
la $t1,elem
li $v0,4
la $a0,string2
syscall
read_matrix:
beq $t0,$0,end_citire
li $v0,5
syscall
sw $v0,0($t1)
addi $t0,-1
addi $t1,4
j read_matrix
end_citire:
li $v0,10
syscall
there is only the read of matrix, but i dont find a formula to acces the elements,please help

The matrix is stored in memory beginning at address elem. To access the matrix, first load the address into a register (la $t1, elem), then calculate the offset the beginning of the matrix and use lw.

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

Assigning ascii values in mips

I am a bit new to mips and am trying to set a .asciiz variable equal to another, then print it. This is what I have:
.data
s: .asciiz "I never could get the hang of Thursdays."
s2: .space 50
.text
main:
la $t0, s
la $t1, s2
lbu $t2, 0($t0)
sb $t2, 0($t1)
la $a0, s2
li $v0, 4
syscall
li $v0, 10
syscall
However, I am only able to print one byte at a time. How can I print the whole string?

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.

Load byte on mips32

I'm starting with mips32 and I'm getting stuck when trying to get a letter from a string to print it. The code should get the string, print it character by character and when it finds an i print iiing.
.data
msg: .asciiz "testing"
i: .asciiz "iiing"
.text
.globl main
main:
la $t0, msg
la $t1, i
li $t2, 0
loop:
bneq $t0, 105, end #$t0=i?
lb $a0, ($t0)
li $v0, 4
syscall
addi $t0, $t0, 1
b loop
end:
move $a0, $t1
li $v0, 4
syscall
Where is the problem?
You have a few problems.
You are comparing $t0, which is the address of the current character, not the character itself. Move that test below the lb line and test against $a0.
105 in ASCII is E, not i. Try 151 (or to be more normal, 0x69).
You want to compare with beq, not bneq.
Inside the loop, you should use syscall 11, which prints a single character, rather than the current syscall 4 you're using, which prints a string.
Your program doesn't make an exit syscall (10) at the end.
You can look at this link for a list of syscalls.
Here's a complete working program for reference:
.data
msg: .asciiz "testing"
i: .asciiz "iiing"
.text
.globl main
main:
la $t0, msg
la $t1, i
li $t2, 0
loop:
lb $a0, ($t0)
beq $a0, 0x69, end
li $v0, 11
syscall
addi $t0, $t0, 1
b loop
end:
move $a0, $t1
li $v0, 4
syscall
li $v0, 10
syscall

Resources