【问题标题】:MIPS - Fetch address not aligned on word boundaryMIPS - 获取地址未在字边界上对齐
【发布时间】:2017-06-25 09:43:48
【问题描述】:

我对汇编编程和一般编程很陌生,我正在努力学习。我在将输入存储到变量时遇到问题,您能帮帮我吗?

当我加载 n1 和 n2 的地址或类似的东西时,似乎会出现问题。我曾尝试查看类似的问题,但无法很好地翻译它们。如果有人也有任何在线学习参考资料,将不胜感激。

这是我的代码:

.data
prompt_n1: .asciiz "Enter first integer n1: "
prompt_n2: .asciiz "Enter second integer n2: "

debug_print: .asciiz "Your numbers are: "

n1: .word 0
n2: .word 0

.text

main:
#Prompt the user for n1.
li $v0, 4
la $a0, prompt_n1
syscall

#Store console: n1
li $v0, 5
syscall

#Attempts to load value of v0 into n1 - Problem Here
la $t0, n1
lw $t1, 0($v0)
sw $t0, 0($t1)

#Prompt the user for n2.
li $v0, 4
la $a0, prompt_n2
syscall

#Store console: n2
li $v0, 5
syscall

#Attempts to load value of v0 into n1 - Problem Here
la $t0, n2
lw $t4, 0($v0)
sw $t0, 0($t4)

j print_statement

print_statement:
li $v0, 4
la $a0, debug_print
syscall

li $v0, 1
lw $a0, n1
syscall

li $v0, 1
lw $a0, n2
syscall

感谢您的时间和帮助。

【问题讨论】:

    标签: mips


    【解决方案1】:

    几个问题...

    在做syscall 5 得到用户的值后,在$v0。您试图使用 lw 指令将其转移到 $t1。但是,您使用$v0 作为lw 的基地址。如果用户输入的值不是四的倍数,这将导致对齐异常 [as you got]

    你想要的是move [注册到注册] 并且不是从内存中加载。但是,实际上,您真的不需要这样做。

    相反,只需将$v0 存储到n1 中,$t0 指向它。所以,你的sw 也是不正确的。

    另外,因为n1n2 不是.data 指令之后的第一项,它们可能没有对齐到四字节边界,因为他们必须在他们身上使用lw/sw。所以,你需要一个.align 指令。

    n2 的输入重复了这些问题。

    而且,在打印之后,没有任何东西可以停止程序,所以它会继续运行,尝试使用内存中的 任何 随机值获取和执行指令。该程序需要syscall 才能在完成后正确终止执行。

    我已经注释了你的代码,显示了之前和之后,并修复了错误[请原谅无偿的风格清理]:

        .data
    prompt_n1:  .asciiz     "Enter first integer n1: "
    prompt_n2:  .asciiz     "Enter second integer n2: "
    
    debug_print:    .asciiz "Your numbers are: "
    space:          .asciiz " "
    
    # NOTE/BUG: because of the variable length byte arrays above (i.e. the .asciiz
    # directives), these may not be aligned to a four byte boundary. there are two
    # solutions:
    # (1) place n1 and n2 immediately after the .data directive
    # (2) add a .align directive [as below]
                .align      4
    n1:         .word       0
    n2:         .word       0
    
        .text
    
    main:
        # Prompt the user for n1.
        li      $v0,4
        la      $a0,prompt_n1
        syscall
    
        # Store console: n1
        li      $v0,5
        syscall
    
        # Attempts to load value of v0 into n1 - Problem Here
        la      $t0,n1
    
        # NOTE/BUG: this is incorrect for several reasons:
        # (1) $v0 has a value input by the user -- it could be 0, 1, etc.
        # (2) these are not valid data addresses
        # (3) if the user enters a value that is _not_ a multiple of four, it
        #     generates an alignment exception
        # (4) this instruction is just not needed and is just wrong
        ###lw       $t1,0($v0)
    
        # NOTE/BUG: $t1 has _not_ been set -- this should use the [correctly] set
        # register $t0 [which has the address of n1] and should store the value
        # read from the user
        ###sw       $t0,0($t1)
        sw      $v0,0($t0)
    
        # Prompt the user for n2.
        li      $v0,4
        la      $a0,prompt_n2
        syscall
    
        # Store console: n2
        li      $v0,5
        syscall
    
        # Attempts to load value of v0 into n1 - Problem Here
        # NOTE/BUG: the lw is commented out for the same reason as above and has
        # a similar mistake as above
        la      $t0,n2
        ###lw       $t4,0($v0)
        ###sw       $t0,0($t4)
        sw      $v0,0($t0)
    
        j       print_statement
    
    print_statement:
        li      $v0,4
        la      $a0,debug_print
        syscall
    
        li      $v0,1
        lw      $a0,n1
        syscall
    
        # NOTE/BUG: added this to separate the numbers
        li      $v0,4
        la      $a0,space
        syscall
    
        li      $v0,1
        lw      $a0,n2
        syscall
    
        # NOTE/BUG: there was no more code here so it just "falls off the end of
        # the world" -- we need to terminate program execution correctly with the
        # exit syscall
        li      $v0,10
        syscall
    

    一些资源:

    http://www.mrc.uidaho.edu/mrc/people/jff/digital/MIPSir.html

    http://courses.missouristate.edu/kenvollmar/mars/help/syscallhelp.html

    http://logos.cs.uic.edu/366/notes/mips%20quick%20tutorial.htm

    http://chortle.ccsu.edu/assemblytutorial/index.html

    【讨论】:

      猜你喜欢
      • 2016-08-24
      • 2014-05-27
      • 2012-03-05
      • 2016-08-06
      • 2013-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多