OpenEuler树莓派基础实验2(无树莓派)
OpenEuler树莓派基础实验
3.2查找最大数
实验代码:
.section .data
.align 3
my_data:
.quad 1
.quad 2
.quad 5
.quad 8
.quad 10
.quad 12
my_data_count:
.quad 6
.align 3
print_data:
.string "big data: %d\n"
.section .text
.globl main
main:
stp x29, x30, [sp, -16]!
ldr x0, =my_data
ldr x1, my_data_count
add x4, x0, #40
mov x3, xzr
1:
ldr x2, [x0], #8
cmp x2, x3
csel x3, x2, x3, hi
cmp x0, x4
b.ls 1b
ldr x0, =print_data
mov x1, x3
bl printf
ldp x29, x30, [sp], 16
ret
编译运行结果:
调试:
3.4通过汇编语言调用C函数
实验代码:
//compare.S
.section .text
.globl compare_data
compare_data:
cmp x0, x1
csel x0, x0, x1, hi
ret
//main.c
#include <stdio.h>
extern int compare_data(int a, int b);
int main()
{
int val;
val = compare_data(5, 6);
printf("big data: %d\n", val);
}
编译运行结果:
3.5GCC内联汇编
实验代码:
#include <stdio.h>
static int compare_data(int a, int b)
{
int val;
asm volatile (
"cmp %1, %2\n"
"csel %0, %1, %2, hi\n"
: "+r" (val)
: "r" (a), "r" (b)
: "memory");
return val;
}
int main()
{
int val;
val = compare_data(5, 6);
printf("big data: %d\n", val);
val = compare_data(6, 4);
printf("big data: %d\n", val);
}
编译运行结果: