2017-02-07 6 views
0

저는 어셈블리 프로그래밍과 프로그래밍에 익숙하지 않고 배우기 위해 최선을 다하고 있습니다. 변수에 입력을 저장하는 데 어려움을 겪고 있습니다. 제발 도와 주실 수 있습니까?MIPS - 단어 경계에 정렬되지 않은 주소 가져 오기

문제는 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 

당신의 시간과 도움을 주셔서 감사합니다

여기 내 코드입니다. syscall 5 일 후

답변

0

몇 가지 문제 ...

사용자의 값을 얻기 위해, 그것은 $v0입니다. lw 명령어를 사용하여 $t1 번으로 전송하려고했습니다. 그러나 lw의 기본 주소로 $v0을 사용하고있었습니다. 사용자가 입력 한 값이라면 [당신이 가지고로] 하지 4의 배수, 이것이 당신이 원한 무엇

정렬 예외를 야기 move와 메모리에서 하지 부하 [등록 (등록)]. 그러나 사실, 당신은 정말로 이것을 할 필요가 없었습니다.

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

관련 문제