如何在MIPS汇编中的内存中的特定地址创建和使用100个元素的多个数组? (QTSpim)

时间:2018-03-17 02:35:16

标签: arrays assembly mips qtspim

所以在这个网站上,我了解到你可以使用QTSpim的数据段在内存中的特定地址创建数组,如下所示:

.data 0xA000
A: .word 3:40

.data 0xB000
B: .word 4:40

.data 0xC000
C: .word 0:40

(这会创建具有特定起始内存地址的元素数组。) 然而,当我尝试通过简单的乘法和减法计算来运行时,我会遇到许多异常,最有可能与我在数组中引用单词有关。

CPU EXCEPTION

BAD ADDRESS EXCEPTION

有人可以告诉我应该如何正确引用这些数组中的单词吗?

目前,我的代码是:

.text
.globl main

main:
    la $a1,A          #Needs to load the address of arrayA into $a1
    la $a2,B          #Needs to load the address of arrayB into $a2
    la $a3,C          #Needs to load the address of arrayC into $a3
    li $t0,0          #Declare counter variable (i)

loop:
    lw $t2,0($a1)        #Needs to load the value of the arrayA at the counter offset into t2
    lw $t3,8($a2)        #Needs to load the value of the arrayB 8 bits after the counter offset into t3

    mul $t4,$t2,$t3     #Multiply A[i] and B[i+2]

    sw $t4,0($a3)       #Save result of all calculations into C[i]

    li $v0,1            #Print the value in the C[i]
    lw $a0,0($a3)
    syscall

    li $v0, 4           #print newline
    la $a0, newline
    syscall

    addi $a1,$a1,4      #Increase all address offsets by 1 word or 4 bits
    addi $a2,$a2,4
    addi $a3,$a3,4
    addi $t0,$t0,1      #Increase counter variable by 1
    beq $t0,20,exit     #If the counter variable (i) < 98, exit
    j loop


    exit:
        li $v0, 10  #exit syscall
        syscall

.data 0xA000
A: .word 3:40

.data 0xB000
B: .word 4:40

.data 0xC000
C: .word 0:40
newline: .asciiz " \n"

在像C这样的高级语言中,这看起来有点像:

int A[40], B[40], C[40];
for (i=0; i < 20; i++) {
       C[i] = A[i] * B[i+2];
       cout << C[i] << "\n";
}

0 个答案:

没有答案