1

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);
4

2 回答 2

2

每次找到匹配的字母时,都不会增加 %o1。因此,您将始终只进入第一场比赛,然后您就被卡住了。

如果您的 eq 将包含 inc %o1,它应该可以工作。

该函数将返回“不匹配”字符串。将计数器增量移动到 o1 增量上方,并添加跳转到“下一个:”

      cmp %o0, %o2
      bne next
      nop
eq:   inc %l0
      nop
next: inc %o1
      ba loop
于 2013-10-28T00:15:18.480 回答
0

我不精通 SPARC 汇编,但调用参数不应该在 %iX 寄存器中吗?

于 2013-10-28T00:14:01.707 回答