inline assembly
程序员文章站
2022-06-01 14:36:36
...
写道
#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
推荐阅读
-
C# 通过 inline-asm 解决嵌入x86汇编
-
Springboot如何基于assembly服务化实现打包
-
模拟兼容性的 inline-block 属性
-
完美解决Could not load file or assembly AjaxPro.2 or one of its dependencies. 拒绝访问。 原创
-
关于“Failed to complete setup of assembly(hr = 0x80131040). Probing terminated”
-
关于div设置display: inline-block之后盒子之间间距的处理
-
html标签的block、inline分类总结分享
-
Css中display:inline-block用法详解
-
C++基础知识-inline、const、mutable、this、static
-
【转】Kotlin的inline内联函数