Is this MIPS strlen correctly converted from the corresponding C loop?

前端 未结 3 1955
长情又很酷
长情又很酷 2021-01-24 21:26

I have a simple question for a Comp Sci class I\'m taking where my task is to convert a function into MIPS assembly language. I believe I have a correct answer but I want to ver

3条回答
  •  鱼传尺愫
    2021-01-24 22:05

    (Editor's note: the add of -1 after the loop corrects for off by 1 while still allowing an efficient do{}while loop structure. This answer proposes a more literal translation from C into an if() break inside an unconditional loop.)

    I think the while loop isn't right in the case of *s == 0.

    It should be something like this:

        ...
        lbu    $t0, 0($a0)
    
    loop:
        beq    $t0, $zero, s_end    # *
        ...
        b   loop
    
    s_end:
        ...
    

    *You could use a macro instruction (beqz $t0, s_end) instead of beq instruction.

提交回复
热议问题