How to store 2 chars in one byte in MIPS? - mips

My question is, it is possible to store 2 chars in one byte in mips?
If yes, how can i do that? I've already searched alot in the internet and i cannot find how to do that, i also tryied to copy to an empty array of strings, but it doesnt work.
.data
string: .asciiz "ola"
string2: .asciiz ""
a: .ascii "a"
a1: .ascii "b"
e: .ascii "e"
e1: .ascii "f"
i: .ascii "i"
i1: .ascii "j"
o: .ascii "o"
o1: .ascii "p"
u: .ascii "u"
u1: .ascii "v"
tam: .word 3
.text
main:
lw $t6, tam #string length
lb $t1, string($t0) #read bit by bit
la $a0, string
la $a1, string2
lb $s0, a #save in register the char that we want to search
lb $s1, a1 #save in register the char that we want to replace
beq $t0, $t6, done
beq $t1, $s0, continua #if the char of (bit by bit) its like the char of chars, swap it
bne $t1, $s0, else #if not, saves
else:
lb $s0, e
lbu $s1, e1
beq $t1, $s0, continua
bne $t1, $s0, else2
else2:
lb $s0, i
lb $s1, i1
beq $t1, $s0, continua
bne $t1, $s0, else3
else3:
lb $s0, o
lb $s1, o1
beq $t1, $s0, continua
bne $t1, $s0, else4
else4:
lb $s0, u
lb $s1, u1
beq $t1, $s0, continua
bne $t1, $s0, store
continua:
move $v0, $a0
sb $s1, string($t0) #do the swap
addi $t0, $t0, 1 #+1 in the index
j main
store:
move $v0, $a0
sb $t1, string($t0) #saves
addi $t0, $t0, 1 #+1 in the index
j main
done:
move $a1, $v0
la $a0, string
li $v0, 4
syscall
li $v0, 10
syscall
Kind regards.

Related

Why is my array comparison not printing in MIPS?

Here is my code that calculates the sum of two arrays and then prints them in a message to the the console. I would now like to compare my two array sums and return the correct message prompt depending on which array is larger. I feel like I'm very close because I tried to follow the logic from the first printed statements but how can I compare register $s7 & $s8 and then declare one the larger array through my message prompt I have set up? I'm using this emulator: https://cpulator.01xz.net/?sys=mipsr5-spim
.data
x: .word 1,2,3,4,5,6,7,8,9,10
iterator: .word 0
size: .word 9
prompt: .asciiz "The total sum of the array is equal to: "
.text
main:
la $s0, prompt
la $t0, x
lw $t1, iterator
lw $t2, size
begin_loop:
bgt $t1, $t2, exit_loop
sll $t3, $t1, 2
addu $t3, $t3, $t0
lw $t6, 0($t3)
addu $s7, $s7, $t6
addi $t1, $t1, 1
j begin_loop
exit_loop:
li $v0, 4
la $a0, prompt
syscall
li $v0, 1
la $a0, ($s7)
syscall
.data
x2: .word 5,6,7,8,9,10,11,12,13,14
iterator2: .word 0
size2: .word 9
prompt2: .asciiz "The total sum of array 2 is equal to: "
prompt3: .asciiz "The larger array is array 1"
prompt4: .asciiz "The larger array is array 2"
.text
main2:
la $s0, prompt2
la $t0, x2
lw $t1, iterator2
lw $t2, size2
begin_loop2:
bgt $t1, $t2, exit_loop2
sll $t3, $t1, 2
addu $t3, $t3, $t0
lw $t6, 0($t3)
addu $s8, $s8, $t6
addi $t1, $t1, 1
j begin_loop2
exit_loop2:
li $v0, 4
la $a0, prompt2
syscall
li $v0, 1
la $a0, ($s8)
syscall
slt $t1,$s7,$s8 # checks if $s0 > $s1
beq $t1,1,prompt3 # if $s7 > $s8, goes to prompt3
beq $t1,$zero,prompt4 # if $s8 < $s7, goes to prompt4
li $v0, 4
la $a0, prompt3
syscall
li $v0, 4
la $a0, prompt4
syscall

MIPS bubble sort algorithm not printing

I am currently beginning to study the MIPS language and I am trying to write a bubble sort code in MIPS, but I guess I have made a mistake somewhere and i can't seem to find it. I believe I am storing elements properly but I don't know why it is not working. Here is a picture of my code:
.data
array: .space 100
i: .word 0
j: .word 0
temp: .word
.text
la $s2, i
la $s3, j
la $s4, temp
li $v0,5
syscall
move $s0, $v0 #array size
li $v0, 8
la $a0, array
li $a1, 100
move $s1, $a0 #actual array(with elements)
syscall
move $a0, $s0 #size becomes argument
move $a1, $s1 #array becomes argument
jal BubbleSort
li $v0, 4
move $a0, $v0
syscall
li $v0, 10
syscall
BubbleSort:
move $s0, $a0
move $s1, $a1
j ExternalLoop
ExternalLoop:
slt $t0,$s2, $s0 #for(i=0;I<N;i++)
beq $t0, $zero, ExitLoop
j InternalLoop
addi $s2, $s2,1 #i++
li $s3, 0 #reset j after every iteration
InternalLoop:
slt $t1, $s3, $s0 #for(j=0;J<N;j++)
beq $t1, $zero, ExitLoop
#array[j]=s1+4*j :
#sll $t2, $s3, 2
#add $s1, $s1, $t2
lb $t3, 0($s1) #array[j]
lb $t4, 4($s1) #array[j+1]
#swapping:
move $s4, $t3
move $t3, $t4
move $t4, $s4
sb $t5, 0($t3)
addi $s3, $s3, 1 #j++
j InternalLoop
move $v0, $t5
ExitLoop:
jr $ra
Maybe my mistake is somewhere at the end where I am storing the return values but I don't know. Can anyone help?

Add some control instructions for a mips program

I wrote a very simple program for mips that counts how many times a default word (in this case the word is "esame") appears in a default phrase that is "Esame: Programma di riconoscimento parole, realizzato per l'esame orale di calcolatori elettronici".
Here is the code:
.data # 0x10000000 (indirizzo di partenza)
frase: .asciiz "Esame: Programma di riconoscimento parole, realizzato per l'esame orale di calcolatori elettronici"
param: .asciiz "esame"
out1: .asciiz "Programma per il riconoscimento di una parola in una frase \n\n"
out2: .asciiz "La parola inserita come parametro appare "
out3: .asciiz " volta/e \n"
.text # 0x00400000 (indirizzo di partenza)
la $s0, frase
la $s1, param
li $t0, 0x20 # backspace
li $t8, 0x27 # apostrophe
add $s5, $zero, $zero
li $v0, 4
syscall
la $a0, out1
syscall
car1:
lbu $t1, 0($s0)
lbu $t2, 0($s1)
ori $t1, $t1, 0x20
ori $t2, $t2, 0x20
beq $t1, $t2, ok
bne $t1, $t2, dive
ok:
addi $s0, $s0, 1
addi $s1, $s1, 1
lbu $t1, 0($s0)
lbu $t2, 0($s1)
beq $t1, $zero, stamp
ori $t1, $t1, 0x20
beq $t2, $zero, ctrl
ori $t2, $t2, 0x20
ctrl:
beq $t1, $t2, ok
beq $t1, $t0, risul
bgt $t1, 0x7a, risul
blt $t1, 0x61, risul
bne $t1, $t2, dive
#beq $t1, $zero, stamp
dive:
addi $s0, $s0, 1
lbu $t1, 0($s0)
beq $t1, $t0, nuova
beq $t1, $zero, stamp
beq $t1, $t8, nuova
j dive
nuova:
la $s1, param
addi $s0, $s0, 1
#beq $s0, $s1, stamp
j car1
risul:
addi $s5, $s5, 1
j nuova
stamp:
li $v0, 4
la $a0, out2
syscall
li $v0, 1
move $a0, $s5
syscall
li $v0, 4
la $a0, out3
syscall
j fine
fine:
j fine
In this case the word "esame" appears twice within the
sentence.
Now I would like to add a control like this: if the default word is formed by an apostrophe, as es'ame or esame', this word should not be counted. In this second case the word "esame" would appear only once in the sentence.
What instructions should I write to implement this control? Thank You.

Count number of lowercase letters

So I have created this program to count the number of lowercase letters in a string. The problem I am having is that when I reach the end of the string and the nl character is reached, the line beq $t0, $t1, end is not being executed; it just continues indefinitely. I'm not sure what I'm doing incorrectly.
.data
msg1: .word 0:24
.text
.globl main
main:
addu $s0, $0, $ra
li $v0, 8
la $a0, msg1
la $a1, 100
syscall
loop:
lb $t0, 4($a0)
li $t1, 0x0a
beq $t0, $t1, end
continue:
li $t1, 'a'
blt $t0, $t1, count
li $t1, 'z'
bgt $t0, $t1, count
count:
addi $t4, $t4, 1
j loop
end:
li $v0, 1
addu $a0, $t2, $0
syscall
jr $ra
You compare 4($a0) with 0x0a on each iteration of the loop, but you never change $a0 in the loop, so you are not advancing through your string and never look at the \n at the end of the string.
There also are a few other bugs in your code.
Use this at the start of your loop:
loop:
lb $t0, 0($a0)
addiu $a0, $a0, 1
...

How to concatenate string in MIPS?

How do I concatenate a string in MIPS? I think I will somehow have to know the length of the string?
Quick and dirty:
# String concatenate
.text
# Copy first string to result buffer
la $a0, str1
la $a1, result
jal strcopier
nop
# Concatenate second string on result buffer
la $a0, str2
or $a1, $v0, $zero
jal strcopier
nop
j finish
nop
# String copier function
strcopier:
or $t0, $a0, $zero # Source
or $t1, $a1, $zero # Destination
loop:
lb $t2, 0($t0)
beq $t2, $zero, end
addiu $t0, $t0, 1
sb $t2, 0($t1)
addiu $t1, $t1, 1
b loop
nop
end:
or $v0, $t1, $zero # Return last position on result buffer
jr $ra
nop
finish:
j finish
nop
.data
str1:
.asciiz "Hello "
str2:
.asciiz "world"
result:
.space 200
If you don't understand something, don't hesitate to ask.
Have fun :)