繁体   English   中英

简单的阵列操作 MIPS 组装

[英]Simple Array Manipulation MIPS Assembly

我是 MIPS 组件的新手,我正在尝试学习如何使用 arrays。 我知道 arrays 是在 .data 段中声明的,然后将数组的地址加载到寄存器中。 但是,我对访问数组元素和更改值感到困惑。 这是我在 C 中编写的一个简单程序,我正在尝试将其转换为程序集。 非常感谢任何解释/代码!

void addArray (int arrA[], int arrB[], int i, int j) {
    arrB[j] = arrA[i] + arrA[i + 1];
    printf("%d", arrB[j]);
}

int main(void) {
    int arrA [] = {1,2,3,4,5,6};
    int arrB [] = {1,1,1,1,1,1};
    int i = 2;
    int j = 3;

    addArray(arrA, arrB, i, j);

    return 0;
}

试试这个代码

    .data
    arrA: .word 1,2,3,4,5,6
    arrB: .word 1,1,1,1,1,1


.text

.globl main

main:
    la $a0,arrA   #load address of arrA into $a0 register (first parameter)
    la $a1,arrB   #load address of arrB into $a1 register (second parameter)

    li $a2,4      # load $a2 register with 3 (threeth parameter)
    li $a3,3      #  load $a3 register with 3 (fourth parameter)

    jal addArray  # call addArray function

    exit:         # syscall to exit the programm
        li $v0, 10   
        syscall
addArray:

    addi $sp,$sp,-4         #allocate space in stack
    sw $ra,0($sp)

    move $t0,$a2            #copy a content of $a2 register into $t0 
    move $t1,$a3            #copy a content of $a3 register into $t1

    mulu $t0,$t0,4          # multiplication by 4 with the value of $t0, because each number have 4 bytes as offset

    addu $a0,$a0,$t0        # 

    lw $t3,($a0)            # arrA[i]
    addi $a0,$a0,4          #  i+1

    lw $t4,($a0)            # arrA[i+1]

    add $t5,$t3,$t4         #   arrA[i] + arrA[i + 1]
    mul $t1,$t1,4           #  j 
    addu $a1,$a1,$t1      
    sw $t5,($a1)            #     arrB[j] = arrA[i] + arrA[i + 1];



print_out:
    lw $s0,($a1)
    li $v0, 1       #print the input number
        move $a0, $s0
        syscall 

        lw $ra,0($sp)      # free stack and return to main
    addi $sp,$sp,4
    jr $ra

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM