我一直得到坏地址异常,知道为什么吗?

时间:2012-11-15 22:21:41

标签: mips

我正在尝试一个简单的程序来计算字符串中的字符数。我似乎遵循了这个结构,但我不断得到同样糟糕的地址错误,有谁知道为什么?

    .data
array:  .space 100
prompt1: .asciiz " Enter the string that you would like to reverse and calculate character: "
prompt2: .asciiz " "

    .text
main:
    la  $a1, array  # allocate the array space      
ask:        
    li  $v0, 4      # print String
    la  $a0, prompt1    # load prompt1
    syscall
    li  $v0, 8      # read the string
    syscall
    move    $a1, $v0    # move input to $a1
    li  $t0 ,0      # $t0 will hold the actual numbers of character in the string        
loopCount:        
    lbu $t1, 0($a1)     # load the next character into t1
    beqz    $t1, print  # check for the null character
    addi    $a1, $a1, 1     # increment the string pointer
    addi    $t0, $t0, 1     # increment the count
    b   loopCount   # return to the top of the loop
print:    
    li  $v0, 1
    move    $a0, $t0
    syscall

1 个答案:

答案 0 :(得分:1)

您应该在$a0中加载缓冲区的地址,并在系统调用之前在$a1中加载缓冲区的大小以读取字符串。