0

i'am new at StackOverFlow, i get into trouble and i need your help. I'am student and i need to write a MIPS program that checks if one string is symmteric. *sample symmetric strings : ana, asddsa, fillif and so on.

This is my first line of code where i am reading string into an array, but i stucked at the symmetric part.

.data

array: .space 50 # char a[50];
.text

readText:

    li $v0,8 # input
    la $a0,array # loadiraj memorija vo adresata
    li $a1,20 #obezbedi memorija za string
    move $t0,$a0 #zachuvaj string vo $t0
    syscall

symmetry:

Please give me an opinion, how i should start with symmetry part. Thanks

  • Can you do it in C? If not, then you need help with an algorithm. If so, then tell us what C construct you're struggling to translate into MIPS. – Erik Eidt Apr 12 '20 at 19:23
  • I have problem at this line `if(string1[i] != string1[length-i-1]){` i don't know how to get i-element of an array then to check if they are same or not. Thanks – Filip Gorgievski Apr 12 '20 at 21:45

1 Answers1

0

Array references are done with pointer arithmetic.  First we have to know the location of the variables string1 and i.  Let's assume string1 is in $a0 and i is in $t0.  We will need to add these two variables together.  Whenever we do an arithmetic operation we have to send the result somewhere, and here the idea is to a send the result to a new as-yet-unused register, say $t1.  ($a0 and $t0 in this scenario would be a bad place to send the result since those registers hold values we'll need later on in the current or next iteration of the loop.)

add $t1, $a0, $t0

Next dereference that temporary pointer using lbu:

lbu $t2, 0($t1)

again targeting an otherwise unused register.

The C version using three address code would look like this:

char *p;
char ch;

p = string1 + i;
char ch = *p;

Comparison is done with either the beq or bne instruction, both of which take two registers to compare (for equality or inequality, respectively) and a branch target in the form of a label.

We use conditional branches to skip ahead for if-then.  The idea is to reason when to skip the then part — and when to skip is the opposite of the if condition as we would write it in C.  In assembly: skip this if that, whereas in C: do this if that.  Thus, the opposite condition is tested in assembly in order to skip around the then-part.

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53