Question

I'm testing a simple assembly function (SPARC). The function is below and should take two parameters, x and *str, and count the number of times x occurs in *str. The function is, however, resulting in an infinite loop. I'm using C to call the assembly function, and that is also below. What could be causing the infinite loop? To clarify, the assembly function repeatedly moves to eq and continually increments l0.

Assembly:

        .global occurs
occurs: mov 0, %l0           !l0 will be counter
loop:   ldsb [%o1], %o2      !get current value, store in o2
        cmp %o2, 0           !if current value is terminating 0, end program
        be end               
        nop
        cmp %o0, %o2         !if two are equal, increment l0
        be eq
        nop
        inc %o1              !increment o1 to check next address
        ba loop
        nop

eq:     inc %l0
        ba loop
        nop

end:    mov %l0, %o0         !store final result in o0
        retl                 !return value
        nop

C function call:

char x = 'A';
char str3[64] = "AaAbBbA";
int oc = occurs(x, str3);
printf("%d", oc);
Was it helpful?

Solution

Every time you find a matching letter, you do not increment %o1. Thus you will always only advance to the first match, and then you are stuck.

If your eq will contain inc %o1, it should work.

The function will return on a "no match" string. Move the counter increment above the o1 increment and add a jump to "next:"

      cmp %o0, %o2
      bne next
      nop
eq:   inc %l0
      nop
next: inc %o1
      ba loop

OTHER TIPS

I'm not fluent in SPARC assembly, but shouldn't the call parameters be in the %iX registers?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top