Swap procedure not working in MIPS - 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

Related

Memory address out of bounds error in MIPS but works in a different function

I am making a program in MIPS that will compare two strings but I can't seem to access the proper locations. I have a function that calculates the lenght of the strings which works fine, but when I try to use a different function (in this case its just a test to see if it works), it says that i'm acessing the wrong memory. I am using QTSpim to test it. The function that works is the duzinaStringa, the one that does not is the krajStringa. Currently it's just trying to print out the first character of the string so I can make sure it works
.text
krajStringa:
li $v0,0
li $t0,0 #brojac1
move $t2, $a0 #adresa prvog
move $t3, $a1 #adresa drugog
lb $a0,0($t2)
li $v0,4
syscall
jr $ra
duzinaStringa:
move $t1,$a0
li $t0,0
petlja:
lb $t2,0($t1)
beqz $t2,krajDuzine
addi $t1,$t1,1
addi $t0,$t0,1
j petlja
krajDuzine:
addi $t0,$t0,-1
move $v0,$t0
jr $ra
main:
la $a0,str1
li $v0, 4
syscall
la $a0, arr
li $a1,200
li $v0, 8
syscall
la $a0,str1
li $v0, 4
syscall
la $a0, arr2
li $a1,200
li $v0, 8
syscall
la $a0,arr
jal duzinaStringa
move $a2,$v0
la $a0,arr2
jal duzinaStringa
move $a3,$v0
la $a0,arr
la $a1,arr2
jal krajStringa
li $v0,10
syscall
.data
arr: .space 200
arr2: .space 200
str1: .asciiz "Unesi string: \n"
As #ErikEidt pointed out, I used the wrong system call when printing the first character of a string.

How can I load multiple saved value registers to argument registers to be used in a function?

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

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)

What does the following QtSPIM/MIPS code do

What does the following QtSPIM/MIPS code do. Describe by referring to the functions of various blocks of the code (Block1, Block2, …).
Answer the questions in front of certain instructions.
Block1:
.text
main:
li $s4, 0
la $a0, input
li $v0, 4
syscall
li $v0, 5
syscall
move $t0, $v0 #Why do we need this?
Block2:
la $a0, output
li $v0, 4
syscall
move $a0, $t0
li $v0, 1
syscall
la $a0, newline # How will the output change if newline is replaced by newspace defined as “ “?
li $v0, 4
syscall
Block3:
Do:
move $a0, $s4
li $v0, 1
syscall
la $a0, newline
li $v0, 4
syscall
slt $s1, $s4, $t0
addi $s4, $s4, 1
bgt $s1, $zero, Do # Why can’t we use “beq $s1, 1, Do “?
Block4:
Exit:
li $v0, 10
syscall
.data
input: .asciiz "Input a number: "
output: .asciiz "Let me count till "
newline: .asciiz "\n "
So I need help with identifying what the code does... I have the concept I just want o make sure Im properly figuring this out... So I have this so far:
move $t0, $v0 #Why do we need this? --- to store number in t0
la $a0, newline # How will the output change if newline is replaced by newspace defined as “ “?
--- load address of string to be printed into $a0 and adds the space if replaced by newspace
the last one I cant quite figure out...

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