inline assembly

写道
#include <stdio.h>
#include <stdlib.h>
#define N 10

void Arraysum(int len, int *A, int *B, int *C)
{
// int k = len;
// do
// {
// k--;
// C[k] = A[k] + B[k];
// } while (k > 0);
__asm__(//".text"
//".globl main"

"main:"
"##########################################################"
"# Note: pseudo-instruction la becomes lui followed by ori"
"la $t0, length # t0 = address of word before array[0], length"
"lw $t2, 0($t0) # t2 = length"
"sll $t2, $t2, 2 # multiply length by 4 to make t2 = length in bytes"
"add $t1, $t2, $t0 # t1 is initial value for k, address of A[k-1], since base is at t0 + 4"
"j test # jump to test at end of loop"

"loop: lw $t3, 0($t1) # t2 = A[k]"
"add $t4, $t1, $t2 # t4 = address of B[k]"
"lw $t5, 0($t4) # t5 = B[k]"
"add $t5, $t5, $t3 # t5 = A[k] + B[k]"
"add $t4, $t4, $t2 # t4 = address of C[k]"
"sw $t5, 0($t4) # C[k] = A[k] + B[k]"
"addi $t1, $t1, -4 # k--"
"test:"
"bne $t1, $t0, loop # loop test"

"################################################################"
"li $v0, 17 # set codes for end syscall"
"li $a0, 0");
// "syscall # end program");
}

main()
{
int Array1[N];
int Array2[N];
int output[N];

// Init input data
for (int i = 0; i < N; i++) {
Array1[i] = rand()%10;
Array2[i] = rand()%10;
}

Arraysum(N, Array1, Array2, output);

printf("Array1: \n");
for (int i = 0; i < N; i++) {
printf("%d ", Array1[i]);
printf(" ");
}

printf("\nArray2: \n");
for (int i = 0; i < N; i++) {
printf("%d ", Array2[i]);
printf(" ");
}

printf("\nOutput: \n");
for (int i = 0; i < N; i++) {
printf("%d ", output[i]);
printf(" ");
}
}

猜你喜欢

转载自cutecoot.iteye.com/blog/2158391