【问题标题】:MIPS/MARS using symbols in a directive?MIPS/MARS 在指令中使用符号?
【发布时间】:2013-02-21 12:07:48
【问题描述】:

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

    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 包含的地址处的内容。

我误会了什么?

谢谢, 凯文

【问题讨论】:

    标签: assembly mips


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2018-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多