2010-02-28 12 views
6

इस सी कोड को एमआईपीएस में बदलने और इसे स्पिम में चलाने की कोशिश कर रहा है।सी से एमआईपीएस अनुवाद

int A[100], B[100]; 
for(i=1; i<100; 1++){ 
A[i] = A[i-1] + B[i]; 
} 

अभी तक यह है कि मैं क्या है।

# comments are delimted by has marks 

.data 
A: .word 0:100  # array of 12 integers 
B: .word 0:100  # array of 12 integers 


.text 
main: 
    li $v0, 1  # load the value "1" into register $v0 
    li $t0, 1  # load the value "1" into register $t0 
    li $t1, 100  # load the value "100" into register $t1 
    blt $t0, $t1, loop # branches to Loop if $t0 < 100 
    la $t9, B 
    la $t8, A 

loop: 
    sll $t0, $t0, 2 
    add $t2, $t9, $t0 
    lw $s4, 0($t9) 
    add $t3, $t0, -1 
    add $t4, $t8, $t3 
    lw $s5, 0($t4) 
    add $t5, $t2, $s5 
    add $t6, $s0, $t0 
    sw $t7, 0($t5) 
    addi $t0, $t0, 1 
    li $v0, 1 # system call for print_int 
    move $a0, $t0 # the sum to print 
    syscall # print the sum 

जब SPIM में चल रहा निम्न त्रुटियों मिलती है:

Exception occurred at PC=0x00400040 
    Bad address in data/stack read: 0x00000004 
Exception occurred at PC=0x0040004c 
    Unaligned address in inst/data fetch: 0x00000003 
Exception occurred at PC=0x00400058 
    Bad address in data/stack read: 0x00000000 
Attempt to execute non-instruction at 0x0040006c 

कुछ दिशा अच्छा होगा। धन्यवाद

+2

क्यों पहले संकलित नहीं करें और परिणाम को अनुकूलित करें? – Potatoswatter

+0

एमआईपीएस क्या है? विकिपीडिया –

+0

एलोन में नहीं मिल रहा है: http://en.wikipedia.org/wiki/MIPS_architecture – Gabe

उत्तर

2

आप loop (blt $t0, $t1, loop) करने के लिए शाखाओं में कर रहे हैं इससे पहले कि आप A और B की ओर इशारा आरंभ कर देगा। आपको अपने कोड के अंत में blt $t0, $t1, loop स्थानांतरित करने की आवश्यकता है, शुरुआत में नहीं है।

मुझे ऐसा करने से नफरत है, लेकिन उन सभी को सूचीबद्ध करने में बहुत सारी चीज़ें गलत हैं। इसे आज़माएं:

.data 
A: .word 0:100  # array of 100 integers 
B: .word 0:100  # array of 100 integers 


.text 
main: 
    li $t0, 4  # load the value "1" into register $t0 
    li $t1, 400  # load the value "100" into register $t1 
    la $t9, B 
    la $t8, A 

loop: 
    add $t2, $t9, $t0 # $t2 = B + i 
    lw $s4, 0($t9)  # $s4 = B[i] 
    add $t3, $t0, -4 # $t3 = i - 1 
    add $t4, $t8, $t3 # $t4 = A + i - 1 
    lw $s5, 0($t4)  # $s5 = A[i - 1] 
    add $t5, $t8, $t0 # $t5 = A + i 
    add $t6, $s4, $s5 # $t6 = B[i] + A[i - 1] 
    sw $t6, 0($t5)  # A[i] = $t6 
    addi $t0, $t0, 4 # i++ 

    li $v0, 1 # system call for print_int 
    move $a0, $t6 # the sum to print 
    syscall # print the sum 

    blt $t0, $t1, loop # branches to Loop if $t0 < 100 
1

बल्ले से बाहर, एस 1 और एस 2 को आपके सरणी के स्टैक-आधारित (मुझे लगता है) पते में प्रारंभ किया जाना चाहिए।

+0

मैंने अब सरणी को ठीक से शुरू किया है (मुझे लगता है)। मुझे smae त्रुटियां मिल रही हैं, लोड शब्द कमांड को निष्पादित किया जा रहा है। – user282964

संबंधित मुद्दे