2016-03-19 5 views
0

というタイトルでは、Mars MIPSシミュレータでこのコードを記述し、そこにコードを組み立てて実行してもエラーは発生しません。私はQtSpimに同じファイルを取ると、そこにそれを実行すると、私は次のエラーを取得する:ケースステートメントコードはMarsシミュレータで実行されますが、QtSpimのエラー

Instruction references undefined symbol at 0x00400014 
[0x00400014] 0x0c000000 jal 0x00000000 [main]   ; 188: jal main 

はQtSpimは私がCASEのラベルのアドレスを呼び出して、私は理由を見るには十分経験していないよとの問題を持っているようです。ここに私のコードです:

.data 
jumptable: .word isD, is1, is2, is3, is4 
currentvalue: .asciiz "Returned : i = "  #Used for result output 
space: .asciiz " \n"      #Used for result output 

.text 
MAIN: 
li $a0, 0    #testing use of the argument register toholdinput 
jal CASE    #set $ra and go to the case procedure 

li $v0, 4    #load syscall 4 to print text 
la $a0, currentvalue #load the string value of currentvalue into $a0 
syscall     #print out currentvalue 

li $v0, 1    #load syscall 1 to print integer 
move $a0, $t3   #move the contents of $t3 to $a0 
syscall     #print the integer in $a0 

li $v0, 10    #load syscall 10 to exit program 
syscall     #exit program 


CASE: 
    la $t3, ($a0)  #load 'i' into a temporary value since we will be using $a0 for syscalls shortly 

    blt $a0, 1, isD  #jump to default if i less than 1 
    bgt $a0, 4, isD  #jump to default if i greater than 4 

    sll $t0, $a0, 2  #multiplies 'i' by 2^2 
    la $t1, jumptable #load the base address of the jump table into $t1 
    add $t1, $t1, $t0 #$t1 = $t1(base address of table) + $t0('i' * 4) 
    lw $t2, ($t1)  #$t2 is loaded with the address of label found in $t1 
    jr $t2 

    is1: 
     addi $t3, $t3, 1 #increment 'i' by 1 
     jr $ra    #jump back to the return address in main 
    is2: 
     addi $t3, $t3, 2 #increment 'i' by 2 
     jr $ra    #jump back to the return address in main 
    is3: 
     addi $t3, $t3, 3 #increment 'i' by 3 
     jr $ra    #jump back to the return address in main 
    is4: 
     addi $t3, $t3, 4 #increment 'i' by 4 
     jr $ra    #jump back to the return address in main 
    isD: 
     li $t0, 0   #set 'i' to 0 
     jr $ra    #jump back to the return address in main 
+0

私はSPIMは大文字と小文字が区別され、あなたのコードだけ 'MAIN'を定義しながら、main''へジャンプみる疑います。 – EOF

+0

Wow、spot on、MainとCASEの両方をメインとケースに変更し、出力を得ました。ありがとうございました! –

答えて

0

私はそれを考え出したと思います。 (警告:私は火星持っている)あなたのエラーメッセージで

は、エラーがライン188から来たので、それはあなたのプログラムのない部分です。それはmainspimコールコードの一部です。

.globl main 
main: 

どちらmarsspimは、上記のように、あなたのMAIN:を認識:

main関数を定義するには、それは とグローバルは、[小文字]でなければなりません。 marsがメインを見つけられない場合は、 .textセクションの最下位アドレスでプログラムを開始します。しかし、 spimは、ただ文句を言うだけです。


UPDATE:

On another note, if you have a chance, I'm learning MIPS now, going by my comments, does everything seem on the up and up in my code?

私はソースを注釈付きと私は何が起こっていたか理解するまで、物事をクリーンアップを意味コードレビュー、の私のバージョンをしました。あなたの場合、これはちょうどいくつかのコメントを編集していた。私はこのプロセスで小さな[1行]のバグを見つけました。私は以下の注釈付きコードで修正しました。

あなたはあなたがいるステージについてコメントしてくれました。いくつかのOPはコメントなしでasmを投稿する。 [任意の言語の]良いコメントは、の意図を示す必要があります。つまり、あなたのプログラムが特定のポイント(what/why)でやろうとするものです。実際のasm命令は「方法」です。

あなたが行ったように、ほとんどのasm命令については、サイドバーのコメントをお勧めします。私はまた、関数の上にコメントブロックをお勧めします。

それらのいくつかは次のようにあったように私は自分のために行うだろうと私はあなたのコメントをクリーンアップ:

x = 23; // set the value of x to twenty three 

ASMを学んでいるときに、時にはあなたが与えられたのasm命令が何を覚えておくことをしなければなりません。しかし、彼らはあなたのプログラムの流れ/ロジックに従うのを妨げることがあります。そこで、私はそれらを「学習者」として注釈をつけました。最終的にあなたは自分自身を削除したり短くしたりして、サイドバーを私がやりたいことに置き換えました。

また、asmプログラマが少なくとも1つの高水準言語に精通しているとすれば、i < 1またはi += 3などのコメントを使用できます。

テストを高速化するために、引数のユーザープロンプトをcaseに追加しました。

はとにかく、ここで[無償スタイルのクリーンアップをご容赦ください]注釈付きソースです:

.data 
jumptable: .word  isD,is1,is2,is3,is4 
prompt:  .asciiz  "Enter case input argument: " 
currentvalue: .asciiz "Returned : i = " # Used for result output 
space:  .asciiz  " \n"   # Used for result output 

    .text 
    .globl main 

main: 
    # prompt user for test value 
    li  $v0,4     # syscall: print string 
    la  $a0,prompt 
    syscall 

    # get user test value from stdin 
    li  $v0,5     # syscall: read integer 
    syscall 
    move $a0,$v0     # move to argument for case 

    # LEARNER: testing use of the arg reg to hold input 
    ###li  $a0,0    # test value for case function 

    # LEARNER: set $ra and go to the case procedure (i.e. we are calling the 
    # case function) 
    jal  case     # calculate [whatever case calculates ;-)] 

    # print the prefix string 
    li  $v0,4     # syscall: print string 
    # LEARNER: address of currentvalue into $a0 
    la  $a0,currentvalue 
    syscall 

    # print the value the 'case' function returned 
    li  $v0,1     # syscall: print integer 
    # LEARNER: move the contents of $t3 to $a0 
    move $a0,$t3     # get value to reg for syscall 
    syscall 

    li  $v0,10     # syscall: exit program 
    syscall 

# case -- does what? 
# 
# RETURNS: what does it return? 
# t3 -- return value 
# FIXME: to follow ABI, this should be v0 but okay for small internal 
# functions like this one. 
# 
# arguments: 
# a0 -- contains 'i' (FIXME: what is 'i'?) 
# 
# internal register usage (i.e. what registers get clobbered): 
# t0 -- offset into jump table 
# t1 -- address of jump table 
# t2 -- internal jump address 
case: 
    move $t3,$a0     # save 'i' to our work/return register 

    blt  $a0,1,isD    # jump to default if i < 1 
    bgt  $a0,4,isD    # jump to default if i > 4 

    # LEARNER: multiply 'i' by 2^2 (4) --> i <<= 2 
    sll  $t0,$a0,2    # get the offset into the jump table 

    la  $t1,jumptable   # base address of the jump table 

    # LEARNER: $t1 = $t1(base address of table) + $t0('i' * 4) 
    add  $t1,$t1,$t0    # add offset to base address 

    # LEARNER: $t2 is loaded with the address of label found in $t1 
    lw  $t2,($t1)    # jump_address = jumptable[i] 
    jr  $t2      # jump to desired label 

is1: 
    addi $t3,$t3,1    # i += 1 
    jr  $ra      # return to caller 

is2: 
    addi $t3,$t3,2    # i += 2 
    jr  $ra      # return to caller 

is3: 
    addi $t3,$t3,3    # i += 3 
    jr  $ra      # return to caller 

is4: 
    addi $t3,$t3,4    # i += 4 
    jr  $ra      # return to caller 

isD: 
    # BUG: this should be t3 
    ###li  $t0,0    # i = 0 
    li  $t3,0     # i = 0 
    jr  $ra      # return to caller 
+0

それは意味があります、私はラベルの名前を変更し、スピムで私のために働いたが、名前変更のときに火星でそれを試していない。 –

+0

チャンスがあれば、私は今、MIPSを学んでいます。私のコメントによれば、すべてのことが私のコードで上に出てくるようですか? –

+0

ありがとうございます。本当にありがとう! –

関連する問題