1

我正在尝试创建一个指向字符串的指针数组,但它的行为不符合预期。该数组包含字符串的内容,而不是地址。这是源代码:

    showhelp:
        .data
    help1:  .asciiz "\nHELP FOR HW4_2b\n"
    help2:  .asciiz "Purpose: Displays the output from a one-bit full adder with inputs provided on the command line.\n"
    help3:  .asciiz "HW4_2b c a b\n"
    help4:  .asciiz "c\t\tThe carry in bit: 0|1\n"
    help5:  .asciiz "a\t\tOne of the bits to be added: 0|1\n"
    help6:  .asciiz "c\t\tThe other bit to be added: 0|1\n"
    helpar: .word help1, help2, help3, help4, help6
    helpsiz:.word 6
            .text
            lw          $t0, helpsiz    # load size of help array
            lw          $t1, helpar     # load address of address of first help string
    nxthlp: la          $a0, ($t1)      # specify string to print
            li          $v0, 4          # specify print string 
            syscall                     # print it
            addi        $t1, $t1, 4     # increment pointer to next string
            subi        $t0, $t0, 1     # decrement counter
            bgtz        $t0, nxthlp     # if not last string loop

            jr $ra          # return

当它运行时,这是输出:

    HELP FOR HW4_2b
    P FOR HW4_2b
    R HW4_2b
    4_2b

    rpose: Displays the output from a one-bit full adder with inputs provided on the command line.

如您所见,helpar: 包含所有连接的字符串,而不是地址。另外,"la $a0, ($t1)" 中的间接寻址是加载$t1 的内容,而不是$t1 包含的地址处的内容。

我有什么误解?

谢谢,凯文

4

1 回答 1

1

这是问题所在:

        lw          $t0, helpsiz
        lw          $t1, helpar     # load address of 1st string
nxthlp: la          $a0, ($t1)      # NOT SURE IF THIS IS EVEN VALID
        li          $v0, 4
        syscall
        addi        $t1, $t1, 4     # skip 4 chars in string; must be unsigned add!
        subi        $t0, $t0, 1
        bgtz        $t0, nxthlp

这是你应该做的:

    lw      $t0, helpsiz
    la      $t1, helpar # t1 points to element 0 of helpar
nxthlp:
    lw      $a0, 0($t1) # fetch element of helpar that t1 points to
    li      $v0, 4
    syscall
    addu    $t1, $t1, 4 # t1 points to next element of helpar
    subi    $t0, $t0, 1
    bgtz    $t0, nxthlp
于 2013-02-21T04:07:32.803 回答