inline assembly
程序员文章站
2022-06-01 14:36:30
...
写道
#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(" ");
}
}
#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(" ");
}
}
上一篇: inline assembly
推荐阅读
-
assembly plugin打包 博客分类: maven
-
assembly plugin打包 博客分类: maven
-
Maven系列(八)assembly打包-程序和依赖jar包分开化+多环境
-
.net core An assembly specified in the application dependencied mainfest<****.json>was not found解决办法
-
完美解决Could not load file or assembly AjaxPro.2 or one of its dependencies. 拒绝访问。 原创
-
inline-block元素设置overflow:hidden 导致相邻行内元素向下偏移的之坑
-
正常文档流block、inline、inline-block元素与浮动框之间的位置关系
-
web前端入门之html+css实例④之浮动(float与inline-block)
-
Advanced Oracle SQL: Subquery Inline View
-
Python中%matplotlib inline的作用