【问题标题】:MIPS - Runtime exception at 0x004001e8: fetch address not aligned on word boundary 0x10010001MIPS - 0x004001e8 处的运行时异常:获取地址未在字边界 0x10010001 上对齐
【发布时间】:2013-11-30 19:44:45
【问题描述】:

我正在尝试在 MIPS 中实现 puts。我有一个程序putchar 在屏幕上打印一个字符:

    .text
putchar:
    lui $t0, 0xffff     # base address of memory map
XReady:
    lw $t1, 8($t0)      #read from transmitter control register
    andi $t1, $t1, 0x1  # extract ready bit
    beqz $t1, XReady       # if 1, store char ; else, loop
    sw $a0, 12($t0)     # send character to display
    jr $ra                 # return to place in program before function call

在我的主子程序中,我将 $a0 设置为我要打印的字符串,然后我调用 puts

la $a0, array       # defined in .data as -- array: .space 2000 --
gal puts

puts 的第 242 行(标有 *)上,我收到一个错误提取地址未与字边界对齐

    .text
puts:
    addi $sp, $sp, -24  # make room for 6 registers
    sw $ra, 20($sp)     # save $ra on the stack
    sw $s0, 16($sp)     # save $s0 on the stack
    sw, $s1, 12($sp)    # save $s1 on the stack
    sw, $s2, 8($sp)     # save $s2 on the stack
    sw, $s3, 4($sp)     # save $s3 on the stack
    sw, $s4, 0($sp)     # save $s4 on the stack

    move $s0, $a0       # copy parameter $a0 into $s0
    move $s1, $a1       # copy parameter $a0 into $s1
    move $s2, $a2       # copy parameter $a0 into $s2
    move $s3, $a3       # copy parameter $a0 into $s3
    move $s4, $zero     # s4 is a character counter. $s4 = 0
getsLoop:
    addi $t0, $zero, 0x00   # Put NULL ascii character inside $t0
    sll $t1, $s4, 2     # create buffer storing address ($t1 = $s1 * 4)
    add $t2, $s0, $t1   # register #t2 now holds buffer address
*   lw $t3, ($s0)       # load char into #t3
    beq $t3, $t0, exitPuts  # exit puts if the current character is the NULL character

    move $a0, $t3       # put the character to print inside $a0, accessible by putchar
    jal putchar     # print char using putchar
    addi $s0, $s0, 1    # character count += 1
    j getsLoop      # Loop to print next character
exitPuts:
    lw $s4, 0($sp)      # restore stack
    lw $s3, 4($sp)      # -
    lw $s2, 8($sp)      # -
    lw $s1, 12($sp)     # -
    lw $s0, 16($sp)     # -
    lw $ra, 20($sp)     # -
    addi $sp, $sp, 20   # pop from stack
    jr $ra          # return

我不知道为什么会出现这个错误...$s0 不是在 main 中定义的array 的地址吗?

【问题讨论】:

    标签: mips


    【解决方案1】:

    当您使用lw 时,您会加载整个单词(4 个字节,不要与自然语言中的单词混淆),并且该单词必须在单词边界上对齐(两个地址的最低有效位必须为 0)。

    ASCII 字符通常每个字符使用一个字节存储,因此您应该使用lbu 指令而不是lw 来加载它们。

    【讨论】:

    • 谢谢,您的修改成功了。不过我很好奇......我的getchar 正在读取内存中的一个单词,我逐字寻址数组。你是说即使我在getchar 中使用sw,它也将ASCII 保存为一个字节?
    • 由于我没有看到我不知道的代码。也许它将 4 个字符分组到一个寄存器中,然后使用 sw 存储它们。
    猜你喜欢
    • 2016-08-24
    • 2017-06-25
    • 2014-05-27
    • 2012-03-05
    • 2016-08-06
    • 1970-01-01
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多