Replacing repeated consecutive characters with X - mips

I have some trouble with my code.
I need this in/output on the console.
Input String : aaabcccdeefggg
Input char: X
Replace:XXXbXXXdXXfXXX <--I want this output
But i get this output-->XXabXXcdXefXXg
I dont know where the bug is, i am searching for hours for this bug.
.data
enterString: .asciiz "Input String > "
enterChar: .asciiz "Input char> "
changedString: .asciiz "\nReplace: "
numberOfAdditions: .asciiz "\Total Replaces: "
userInput: .space 555
.text
main:
li $v0, 4
la $a0, enterString
syscall
li $v0, 8
la $a0, userInput
li $a1, 554
syscall
li $v0,4
la $a0,enterChar
syscall
li $v0,12
syscall
move $t5,$v0 # $t0 = character to be replaced
li $t0, 0
li $t1, 1
j findDoubleCharacters
findDoubleCharacters:
lbu $s0, userInput( $t0 )
lbu $s1, userInput( $t1 )
addi $t0, $t0, 1
addi $t1, $t1, 1
beq $s0, $s1, found
beq $s1, 0, end
j findDoubleCharacters
found:
subi $t0, $t0, 1
subi $t1, $t1, 1
sb $t5, userInput($t0)
addi $s2, $s2, 1
j findDoubleCharacters
end:
li $v0, 4
la $a0, changedString
syscall
li $v0, 4
la $a0, userInput
syscall
li $v0, 4
la $a0, numberOfAdditions
syscall
li $v0, 1
move $a0, $s2
syscall
...

Related

How to count the number of spaces in MIPS?

I need to write a program in which the output will be the number of sentences and average number of words. The code is working perfectly for counting the sentences, but for the second task, it doesn't work. I'm using branch if equal with the current char and the register in which I've declared the ascii value for space which is 32. With this code, the output for the average words is the total number of characters from the whole input. I can't understand how it can count every character when the beq is clearly incorrect. (I've also tried with emptySpace: .asciiz " ", but it's not working)
This is what I got so far:
.data
str_input: .space 256
dot: .asciiz "."
msg1: .asciiz "Number of sentences: "
msg2: .asciiz "\nAverage number of words: "
.text
li $v0, 8 #read string
la $a0, str_input #address of str_input vo $a0
li $a1, 256 #256 max
la $a2, dot
lb $a2, 0($a2)
li $a3, 32
syscall
addi $t4, $zero, 0 #counter for words
addi $t5, $zero, 0 #counter for sentences
or $t0, $a0, $zero #$t0 pointer to array
start:
lb $t1, 0($t0) #$t1 current char
beqz $t1, end
jal check
addiu $t0, $t0, 1
j start
chech:
beq $t1, $a2, IsDot
beq $t1, $a3, IsEmptySpace
IsDot:
addi $t5, $t5, 1
jr $ra
IsEmptySpace:
addi $t4, $t4, 1
jr $ra
end:
la $a0, msg1 #address of msg1 in $a0
li $v0, 4 #load string in $v0
syscall
add $a0, $t5, $zero
li $v0, 1
syscall #print number of sentences
addi $t4, $t4, 1 #add the last word
div $t4, $t5 #divide number of words with number of sentences
mflo $t6
la $a0, msg2 #address of msg2
li $v0, 4 #load string in $v0
syscall
add $a0, $t6, $zero
li $v0, 1
syscall

Why this MIPS code don't show me the "a" occurrences?

I have a little problem with my MIPS code, because this not work with "a", I explain better... If I write:
Heeeeello world --> I obtain this output "The 'e' has the biggest number of occurrences, 5"
hi Maaaaaark --> I obtain this output "The '' has the biggest number of occurrences, "
Can someone give me a solution? this is my code:
.data
m1: .asciiz "Give me a string: "
m2: .asciiz "\nThe letter '"
m3: .asciiz "' has the biggest number of occurrences, is present "
m4: .asciiz " times."
.align 2
myArray: .space 104
.text
.globl main
main:
li $v0, 4
la $a0, m1
syscall
addi $sp, $sp, -256
move $s0, $sp
move $a0, $sp
li $a1, 255
li $v0, 8
syscall
move $a0, $s0
jal check_case
move $a0, $v0
move $a1, $v1
jal analizza_stringa
move $a0, $v0
move $a1, $v1
jal stampa_risultato
end_program:
li $v0, 10
syscall
check_case:
move $t0, $a0
while:
lb $t1, ($t0)
beq $t1, 32, ignore_value_and_continue
beq $t1, 10, exit_check_case
bge $t1, 97, continue_check_case
bge $t1, 65, change_case
change_case:
addi $t2, $t1, 32
sb $t2, ($t0)
ignore_value_and_continue:
continue_check_case:
addi $t2, $t2, 1
addi $t0, $t0, 1
j while
exit_check_case:
move $v0, $t0
move $v1, $t2
jr $ra
analizza_stringa:
li $t1, 0
li $t2, 0
li $t3, 0
li $t4, 0
move $t7, $a0
sub $a0, $a0, $v1
sub $t7, $t7, $v1
while_string:
lb $t0, ($a0)
beq $t0, $zero, check_best
beq $t1, 32, ignore_value_and_continue2
ignore_value_and_continue2:
beq $t0, 10, check_best
subi $t3, $t0, 97
mul $t4, $t3, 4
move $t6, $t7
while_occorrenza:
beq $t1, 10, continue
lb $t1,($t6)
bne $t1, $t0, continue2
addi $t5, $t5, 1
continue2:
addi $t6, $t6, 1
j while_occorrenza
continue:
sw $t5, myArray($t4)
li $t5, 0
li $t3, 0
li $t1, 0
addi $a0, $a0, 1
j while_string
check_best:
li $t0, 0
lw $t1, myArray($t0)
addi $t0, $t0, 4
while_check_best:
beq $t0, 104, exit_check
addi $t5, $t5, 1
lw $t4, myArray($t0)
bge $t4, $t1, scambia
j continue_check
scambia:
blt $t4, $t2, continue_check
move $t2, $t4
add $t3, $t5, 97
continue_check:
addi $t0, $t0, 4
j while_check_best
exit_check:
move $v0, $t3
move $v1, $t2
jr $ra
stampa_risultato:
move $t0, $a0
li $v0, 4
la $a0, m2
syscall
li $v0, 11
move $a0, $t0
syscall
li $v0, 4
la $a0, m3
syscall
li $v0, 1
move $a0, $a1
syscall
li $v0, 4
la $a0, m4
syscall
jr $ra

Exception 4 on mips bubblesort

So i have to write a bubblesort programm in mips using QtSpim for a class but i get an Exception 4 [Adress error in inst/data fetch]. I have searched in other topics and use the .align 2 directive before defining an array for 5 integers but it still isn't getting fixed.
Here's the code:
.text
.globl main
main:
la $t1, array #sets the base adress of the array to t1
la $a0, in_prompt
li $v0, 4
syscall
li $t2, 0 #init to 1
read_loop:
beq $t2, 5, read_key #break if t2 = 5
la $a0, num_prompt
li $v0, 4
syscall #"Give number"
#move $a0, $t2
#li $v0, 1
#syscall #current number to be read
li $v0, 5 #read int. 5 times
syscall
sw $v0, ($t1) #move input from v0 to the array
addi $t1, $t1, 4 #move t1 to the next position in the array
addi $t2, $t2, 1 #increment counter (t2) by 1
j read_loop
read_key:
la $a0, search_q
li $v0, 4
syscall #print query prompt
li $v0, 5
syscall
move $t3, $v0 #number we're looking for (KEY)
move $a0, $t1 #array to pass as arguement
jal bubblesort
move $a0, $v0 #move into a0 the sorted array
move $a1, $t3 #move the KEY into a1 as arguement
jal binarysearch
move $t1, $a0 #move into t1 the array
move $t0, $a1 #move into t0 the KEY
move $t3, $v0 #move into t3 the result of binarySearch
beq $t3, -1, not_found #if key was not found
move $a0, $t0
li $v0, 1
syscall
la $a0, found_pr
li $v0, 4
syscall
j print_array
not_found:
move $a0, $t0
li $v0, 1
syscall
la $a0, not_found_pr
li $v0, 4
syscall
j print_array
print_array:
li $t2, 1 #init to 1
print_loop:
beq $t2, 5, EXIT
lw $a0, ($t1)
li $v0, 1
syscall
addi $t1, $t1, 4
addi $t2, $t2, 1
j print_loop
EXIT:
li $v0, 10
syscall
##############binarysearch#################
binarysearch:
addi $sp, $sp, -24 #reserve space for 6 elements
sw $a0, 0($sp) #push a0 (array)into stack
sw $a1, 4($sp) #push a1(KEY) into stack
sw $s0, 8($sp) #push s0
sw $s1, 12($sp) #push s1
sw $s2, 16($sp) #push s2
sw $ra, 20($sp) #push ra
sw $s3, 24($sp) #push s3
li $s0, 0 #low = 0
li $s1, 4 #high = 4
while:
bgt $s0, $s1, exit_search #if low > high branch
move $a1, $s0 #move into a1 low
move $a2, $s1 #move into a2 high
jal calc_middle #jump to calc_middle
move $s2, $v0 #move into s2 the return value of calc_middle
lw $a0, 0($sp) #restore into a0 the 1st stack el(array)
lw $a1, 4($sp) #restore into a1 the 2nd stack el(KEY)
add $a0, $a0, $s2 #move the array to middle
lw $s3, ($a0) #load into s3 the a[middle]
beq $a1, $s3, exit_search_found #break if KEY == a[middle]
blt $a1, $s3, less_t_middle #if the key is less than the middle element
addi $s0, $s2, 1 #if the key is greater than the middle element set new low
j while
less_t_middle:
addi $s1, $s2, -1 #new high
j while
exit_search_found:
move $v0, $s2 #return found
lw $s0, 8($sp)
lw $s1, 12($sp)
lw $s2, 16($sp)
lw $ra, 20($sp)
addi $sp, $sp, 24
jr $ra
exit_search:
li $v0, -1 #return -1
lw $s0, 8($sp)
lw $s1, 12($sp)
lw $s2, 16($sp)
lw $ra, 20($sp)
addi $sp, $sp, 24
jr $ra
##############calc_middle##################
calc_middle:
add $a1, $a1, $a2
sra $a1, $a1, 1
move $v0, $a1
jr $ra
##############bubblesort###################
bubblesort:
addi $sp, $sp, -12
sw $s0, 0($sp)
sw $s1, 4($sp)
sw $s2, 8($sp)
li $s2, 4 #j = 5
li $s1, 0 #i = 0
outer_loop:
addi $s2, $s2, -1 #i = j - 1
blt $s2, $zero, exit_sort
inner_loop:
bgt $s1, $s2, outer_loop #if i > j - 1
lw $s3, 0($a0) #load into s3 the a[i]
lw $s4, 4($a0) #load into s4 the a[i+1]
bgt $s3, $s4, swap #if a[i] > a[i+1]
addi $s1, $s1, 1 #i++
j inner_loop
swap: move $s0, $s3 #tmp = a[i]
move $s3, $s4 #a[i] = a[i+1]
move $s4, $s0 #a[i+1] = tmp
addi $a0, $a0, 4 #point to the next element ????
addi $s1, $s1, 1 #i++
j inner_loop
exit_sort:
lw $s0, 0($sp)
lw $s1, 4($sp)
lw $s2, 8($sp)
addi $sp, $sp, 8
move $v0, $a0 #pass into v0 the sorted array
jr $ra
.data
.align 2
array: .space 20
in_prompt: .asciiz "Enter 5 numbers:\n"
num_prompt: .asciiz "Give number: "
search_q: .asciiz "what are you looking for?\n"
not_found_pr: .asciiz " not found in array: \n"
found_pr: .asciiz " found in array: \n"

Trouble with MIPS array

i'm trying to write some code that take as input integers and strings, stores them into an array and then prints them in order (int-string, int-string etc.). I will eventually add some sorting code for the integer, when i get this to work. The problem is i can't get the code to work right, i can't manage to have the output come out correctly. I'm assuming the problem lies in the array, since i'm not sure how it is supposed to work (MIPS is definitely not my forte). The code is the following:
.data
array: .space 400 #array
in_name:
.asciiz "\nInsert name: "
in_date:
.asciiz "\nInsert date (mmdd): "
appt:
.asciiz "\nList: "
spaz: .asciiz " "
.text
main:
la $s0, array #load array in s0
addi $t0, $zero, 0 #t0=0 counter
addi $s1, $zero, 0 #s1=0 array size counter
j Input
Input:
li $v0, 4
la $a0, in_date
syscall #ask date
li $v0, 5
syscall #read date
add $t1, $zero, $t0 #offset in t1
add $t1, $t1, $t1 #t1*2
add $t1, $t1, $t1 #t1*4
add $s2, $t1, $s0 #array with offset in s2
sw $v0, 0($s2) #save date
addi $t0, $t0, 1 #t0++
addi $s1, $s1, 1 #array size counter +1
li $v0, 4
la $a0, in_name
syscall #ask name
li $a0, 4
li $v0, 9
syscall #space for new word (4bytes)
la $a0, array
li $a1, 4
li $v0, 8
syscall #read name
add $t1, $zero, $t0 #offset in t1
add $t1, $t1, $t1 #t1*2
add $t1, $t1, $t1 #t1*4
add $s2, $t1, $s0 #array with offset in s2
sw $v0, 0($s2) #save name
addi $s1, $s1, 1 #array size counter +1
addi $t0, $t0, 1 #t0++
beq $s1, 10, print #if array size=10 go to print
j Input #start over until s1=10
print:
la $a0, appt
li $v0, 4
syscall #print list
addi $t0, $zero, 0 #t0=0 counter
res:
add $t1, $zero, $t0 #offset in t1
add $t1, $t1, $t1 #t1*2
add $t1, $t1, $t1 #t1*4
add $s2, $t1, $s0 #array with offset in s2
lw $a0, 0($s2) #load date
li $v0, 1
syscall #print data
addi $t0, $t0, 1 #t0++
la $a0, spaz #load space
li $v0, 4
syscall #print space
add $t1, $zero, $t0 #offset in t1
add $t1, $t1, $t1 #t1*2
add $t1, $t1, $t1 #t1*4
add $s2, $t1, $s0 #array with offset in s2
lw $a0, 0($s2) #load name
li $v0, 4
syscall #print name
addi $t0, $t0, 1 #t0++
la $a0, spaz
syscall #print space
addi $t0, $t0, 1 #t0++ counter
bne $t0, $s1, res #start over until t0=s1
j end
end:
li $v0, 10
syscall #the end
Depending on which program i use to test it i get an error or the program ends correctly but with a wrong output (just some numbers).
Can someone point out to me how should i correct it to make it work properly?
Thanks
I changed the program a little. I created 2 arrays for numbers and strings. As I read date and name I save them to num_array and str_array respectively. This makes it easier to deal with addressing, and might be easier for processing later. Comments in the code should be helpful.
.data
num_array: .space 400 #array
str_array: .space 400
in_name: .asciiz "\nInsert name: "
in_date: .asciiz "\nInsert date (mmdd): "
appt: .asciiz "\nList: "
spaz: .asciiz " "
.text
main:
la $s0, num_array #load array in s0
la $s1, str_array
addi $t0, $zero, 0 #t0=0 counter
addi $s2, $zero, 0 #s1=0 array size counter
j Input
Input:
# prompt for date
li $v0, 4
la $a0, in_date
syscall
# read date
li $v0, 5
syscall
# store date in the num_array
sw $v0, 0($s0)
# increment counter and move to next position in the array
addi $t0, $t0, 1
addi $s2, $s2, 1
addi $s0, $s0, 4
# prompt for name
li $v0, 4
la $a0, in_name
syscall #ask name
# store name (max 4 bytes) to str_array ($s1)
move $a0, $s1
li $a1, 4
li $v0, 8
syscall #read name
# move to the beginnig of next string in $s1
# increment by 4 because length of each input word is 4
addi $s1, $s1, 4
beq $s2, 10, print #if array size=10 go to print
j Input #start over until s1=10
print:
# print "List:"
la $a0, appt
li $v0, 4
syscall #print list
addi $t0, $zero, 0 #t0=0 counter
la $s0, num_array # address of num_array
la $s1, str_array # address of str_array
res:
# get number from num_array and print it
lw $a0, 0($s0)
li $v0, 1
syscall
# move to the next element, increment by 4 because numbers take 1 word = 4 bytes
addi $s0, $s0, 4
# get string from str_array and print it
la $a0, 0($s1)
li $v0, 4
syscall
# print space
la $a0, 32
li $v0, 11
syscall
# move to the next element, increment by 4 because strs have 4 bytes length (in your case)
addi $s1, $s1, 4
# increment loop counter
addi $t0, $t0, 1
blt $t0, $s2, res # $s2 is size of the array
end:
li $v0, 10
syscall #the end
Here is the output:
Insert date (mmdd): 1201
Insert name: aaa
Insert date (mmdd): 1202
Insert name: bbb
Insert date (mmdd): 1203
Insert name: ccc
Insert date (mmdd): 1204
Insert name: ddd
Insert date (mmdd): 1205
Insert name: eee
Insert date (mmdd): 1206
Insert name: fff
Insert date (mmdd): 1207
Insert name: ggg
Insert date (mmdd): 1208
Insert name: hhh
Insert date (mmdd): 1209
Insert name: iii
Insert date (mmdd): 1210
Insert name: jjj
List: 1201aaa 1202bbb 1203ccc 1204ddd 1205eee 1206fff 1207ggg 1208hhh 1209iii 1210jjj
-- program is finished running --

function in mips that calculates the absolute value [duplicate]

This question already has answers here:
Integer absolute value in MIPS?
(5 answers)
Closed 2 years ago.
hello i have an exercise in mips that must call a function to calculates the absolute value of a number
i have write 2 codes but if you have any other solution write it
.data
message:.asciiz "give number: "
.text
main:
li $v0, 4
la $a0, message
syscall
li $v0, 5
syscall
add $t1, $v0, $zero
jal absolute
add $v0,$t2,$zero
li $v0, 1
syscall
li $v0, 10
syscall
absolute: ori $t2,$t1,0 #copy r1 into r2
slt $t3,$t1, $zero #is value < 0 ?
beq $t3,$zero,gg #if r1 is positive, skip next inst
sub $t2,$zero, $t0 #r2 = 0 - r1
jr $ra
gg:
#t2
and the second code is this
.data
question: .asciiz "give number"
.text
main:
li $v0, 4
la $a0, question
syscall
li $v0, 5
syscall
jal absolute
li $v0, 1
syscall
li $v1, 1
syscall
li $v0, 10
syscall
absolute:
slti $t0,$a0,0
bne $t0,$zero,g1
add $v0,$a0,$zero
jr $ra
g1:
sub $t2,$a0,$a0
sub $v1,$t2,$a0
j absolute
Try this:
.data
message: .asciiz "Enter the number: "
.text
.globl main
main:
# print message
li $v0, 4
la $a0, message
syscall
# read integer
li $v0, 5
syscall
slt $t0, $v0, $0 # $t0 = ( $v0 < 0 ? 1 : 0 )
bne $t0, $0, NEGATIVE # if($t0 != 0) goto NEGATIVE
j POSITIVE # goto POSITIVE (and dose nothing)
NEGATIVE:
# ~$v0 + 1
nor $v0, $v0, $0 # NOR with zero = NOT
addi $v0, $v0, 1 # $v0 =+ 1
POSITIVE:
# print $v0
move $a0, $v0
li $v0, 1
syscall
# print new line '\n'
li $v0, 11
addi $a0, $0, 10
syscall
jr $ra
Test:
Enter the number: 10
10
Enter the number: -5
5