Search code examples
assemblymips

MIPS assembly: instruction "beq" and the 'and' operator


Can I use the 'and' operator with 'beq' in MIPS conditional statements?

For example, in C, I can write if(arr[0]=='a' && arr[1]=='b' && arr[2]=='c'), but how can I write like this code in MIPS assembly?


Solution

  • Fundamentally, we combine flow of control with logic of the conditions we're testing.  As Jester is telling you, we can do/observe this in C, which is often friendlier for these transformations than assembly.

    You can see that:

    if(arr[0]=='a' && arr[1]=='b' && arr[2]=='c') { ... }
    

    is equivalent to:

    if (arr[0]=='a') {
        if (arr[1] == 'b') {
            if (arr[2] == 'c') {
                /* ... */
            }
        }
    }
    

    So, if you know how to do if (x == y) { ... } then just apply that three times.


    In assembly language our only decision control flow construct is "if-goto", and of course only simple conditions can be tested.

    So, to do if (x==y) { then-part } else { else-part }, using an "if-goto" style, we test x==y and upon that condition being false we branch around the then-part and to the else-part.  Since we are branching on condition false, then when the condition is true, we fail to branch and run the then-part that we program immediately following the condition test.

    Since we're branching on condition false, then for all practical purposes, we write, in "if-goto" style in C: if (x!=y) goto ElseLabel; followed by the then the then-part translation...


    For reference, see the following posts: