According to the c code the value in the base address of array p is supposed to be equal value of base address of array str but they doesn’t match I am still new to mips assbley so excuse me and feel free to do any edits thanks in advance
I think the problem is in loading bytes and storing bytes in the array but still can’t figure it out,
I left extra parts of my assembly code in order to make it simple for the viewer of how the code is about but the part that I am asking about in specific is within the while loop
this is the psudo code
i=0;
j=0;
k=0;
while(j < my_strlen(str))
{
if (str[j] == ' ')
{
p[i][k] = '';
i++;
k = 0;
}
else
{
p[i][k++] = str[j];
}
j++;
}
this is my attempted mips code
.data
msg: .asciiz "Enter a string n"
str: .space 30 # get the paragraph from the user
p: .space 400 #p[20][20]
stringlen: .asciiz "String length is "
blankSpace: .ascii " "
.text
main:
la $a3,blankSpace
lb $s4,0($a3) # $s4 =blankspace
la $s5,str # address start of str
la $s6,p # address start of p
la $s7,ptr1 # address start of ptrl
li $t9,20 # width =20
li $t0,0 # i=0
li $t2,0 # j=0
li $t3,0 #k=0
WHILE:
bge $t2,$s3,End_While # branch if j<=strlength
lbu $t7,($s5) # $t7 = str[j]
beq $t7,$s4,L # if str[j]== ' ' branch L
mul $t8,$t9,$t0 # width *i
add $t8,$t8,$t3 # width * i+k
add $t8,$t8,$s6 # base array (width *i+k)
sb $t7,($t8) # p[i][k++]=str[j]
addi $t3,$t3,1 # k++
L2: addi $t2,$t2,1 # j++
addi $s5,$s5,1 # str[j+1]
j WHILE
L: mul $t8,$t9,$t0 # array width * i
add $t8,$t8,$t3 # width * i+k
add $t8,$t8,$s6 # base array +(width * i+k)
sb $zero,($t8) # p[i][k++]=''
addi $t0,$t0,1 # i++
addi $t3,$zero,0 # k=0
j L2
End_While:
The whole programe
.data
msg: .asciiz "Enter a string n"
str: .space 30 # get the paragraph from the user
ptr1 : .space 400 #ptr1[20][20]
p: .space 400 #p[20][20]
stringlen: .asciiz "String length is "
blankSpace: .ascii " "
next: .asciiz "n Next while"
.text
main:
li $v0,4
la $a0,msg
syscall # printing the message to the user
li $v0,8
la $a0,str
li $a1,30
syscall # getting a paragraph from the user
li $v0,4
la $a0,stringlen #"String length is "
syscall
la $a0,str
jal strlen
addi $a0,$v0,0
addi $s3,$a0,0 #strlen value $s3
li $v0,1
syscall # calling the strlen function to count the string lengt
la $a3,blankSpace
lb $s4,0($a3) # $s4 =blankspace
la $s5,str # address start of str
la $s6,p # address start of p
la $s7,ptr1 # address start of ptrl
li $t9,20 # width =20
li $t0,0 # i=0
li $t2,0 # j=0
li $t3,0 #k=0
WHILE:
bge $t2,$s3,End_While # branch if j<=strlength
lbu $t7,($s5) # $t7 = str[j]
beq $t7,$s4,L # if str[j]== ' ' branch L
mul $t8,$t9,$t0 # width *i
add $t8,$t8,$t3 # width * i+k
add $t8,$t8,$s6 # base array (width *i+k)
sb $t7,($t8) # p[i][k++]=str[j]
addi $t3,$t3,1 # k++
L2: addi $t2,$t2,1 # j++
addi $s5,$s5,1 # str[j+1]
j WHILE
L: mul $t8,$t9,$t0 # array width * i
add $t8,$t8,$t3 # width * i+k
add $t8,$t8,$s6 # base array +(width * i+k)
sb $zero,($t8) # p[i][k++]=''
addi $t0,$t0,1 # i++
addi $t3,$zero,0 # k=0
j L2
End_While:
li $v0,4
la $a0,p
syscall
li $v0 ,10
syscall # Exit terminating the program
strlen:
addi $sp,$sp,-4
sw $ra,0($sp)
addi $t0,$0,0 # i=0
addi $t1,$0,0 # len=0
l: add $t2,$a0,$t0 # add base address to offset
lbu $t3,0($t2) # load base unsigned for char array
beq $t3,$0,finish # if value of array[i] = ''
addi $t0,$t0,1 # i++
addi $t1,$t1,1 # len++
j l
finish:
subi $s0,$s0,1
add $v0,$t0,$s0
lw $ra,0($sp) # restore old $s0
addi $sp $sp 4
jr $ra