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.
Related
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
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.
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?
.data
mstring: .asciiz "0123456789"
nstring: .asciiz "abcdefghij"
istring: .asciiz "--------------------"
endl: .asciiz "\n"
# i want to print istring=0a1b2c3d4e5f6g7h8i9j
.text
.globl __start
__start:
la $t1, mstring
la $t2, nstring
la $s1, istring
addi $t3,$zero,0 #counter for M
addi $t4,$zero,0 #counter for N
addi $t5,$zero,0 #counter for I
li $t6,20 # max i =20
li $t2,2 #compare for div
li $s4,1 #compare for beq
j Loop
Loop:
beq $t5,$t6,Print
addi $s3,$zero,0 #$s3==0
div $t5,$t2
mflo $s3 # $s3=$t5mod$t6
beq $t3,$zero,Mtogo #if $t5mod$t6==1 go to N
beq $t3,$s4,Ntogo #if $t5mod$t6!=1 go to M
j Loop
Mtogo:
add $s1,$s1,$t5
add $t1,$t1,$t3
lb $s7,0($t1)
sb $s7,0($s1)
addi $t3,$t3,1
addi $t5,$t5,1
Ntogo:
add $s1,$s1,$t5
add $t2,$t2,$t4
lb $s7,0($t2)
sb $s7,0($s1)
addi $t4,$t4,1
addi $t5,$t5,1
Print:
la $a0,0($s1)
li $v0,4
syscall
j Exit
Exit:
li $v0,10
syscall
Your code seems too difficult for such a job.
What about...
.data
mstring: .asciiz "0123456789"
nstring: .asciiz "abcdefghij"
istring: .asciiz "--------------------"
endl: .asciiz "\n"
.text
.globl __start
__start:
la $t1, mstring
la $t2, nstring
la $t3, istring
loop:
lb $t4, 0($t1) # load a byte from mstring
addi $t1, $t1, 1 # increment mstring
sb $t4, 0($t3) # store the byte at istring
beq $t4, $zero, end # if loaded byte == 0, the string was terminated
lb $t4, 0($t2) # load a byte from nstring
addi $t2, $t2, 1 # increment nstring
sb $t4, 1($t3) # store the byte at istring+1
addi $t3, $t3, 2 # increment istring by two, one for each byte we loaded
j loop # loop again
end:
li $v0, 4
la $a0, istring
syscall
I am converting a C code into MIPS that finds the location(s) of a sub-string in a string and i can't figure out what is causing the address out of range error at line "strloop:
lb $t2, ($t1)" (line 76). How can i fix the error??
My code is:
.data
Text : .space 28
Key : .space 5
msg1 : .asciiz "Please enter the text:"
msg2 : .asciiz "\nPlease enter the key:"
msg3 : .asciiz "\n NOT FOUND"
msg4 : .asciiz "The key found at:"
.text
main:
la $a0, msg1
li $v0,4
syscall
la $a0, Text
li $a1, 28
li $v0,8
syscall
la $t0, Text
la $a0, msg2
li, $v0,4
syscall
la $a0, Key
li $a1,5
li $v0,8
syscall
la $t1, Key
add $a0, $t1 ,$zero
jal mstrlen
add $t2, $v0, $t2
add $a0, $t0, $zero
add $a1, $t1, $zero
add $a2, $t2, $zero
jal findPos
add $t0, $t0, $v0
add $a0, $v0, $zero
beq $a0, $zero,K
li $v0, 4
la $a0, msg4
syscall
li $t0, 1
move $a0, $t0
syscall
K: la $a1, msg3
li $v0,4
syscall
li $v0, 10
syscall
mstrlen:
addi $sp, $sp, -8
sw $t0,4($sp)
sw $t1,($sp)
li $t0,0 #$t0=0
add $t1,$a0,$zero
strloop:
lb $t2, ($t1)
beq $t2,28,endloop #$t2\n
addi $t0,$t0,1 #$t0++
addiu $t1,$t1,1
b strloop #next i
endloop:
add $v0, $t0, $0
lw $t0, 4($sp)
lw $t1,($sp)
addi $sp,$sp,20 #stack pointer restored
jr $ra
######################## MAIN FUNCTION
# $a0 text, $a1 key, $a2 keyInt (strleng(key)
findPos:
addi $sp, $sp, -20
sw $t0, 16($sp)
sw $t1, 12($sp)
sw $t2, 8($sp)
sw $t3, 4($sp)
sw $t4, ($sp)
loop:
add $t2,$a2, $zero
lb $t1,($a1)
lb $t0,($a0)
beq $t0,28,endfP
beq $t0, $t1, L
add $t1, $zero, $zero
beq $t1,$t2,M
addi $t0,$t0,1
b loop
L: addi $t1,$t1,1
M: add $t3, $t3,$t0
addi $t3, $t3,1
sub $t3,$t3,$t2
endfP: add $v0, $t3, $0
lw $t0,16($sp)
lw $t1,12($sp)
lw $t2,8($sp)
lw $t3,4($sp)
lw $t4,($sp)
addi $sp,$sp,20
jr $ra