Based on raspberry pie + openeuler platform
Experiment 3-2: assembly language practice - finding the maximum number
1. Experimental purpose
Through this experiment, understand and be familiar with ARM64 assembly language.
2. Experimental requirements
ARM64 assembly language is used to realize the following functions: find the maximum number in a given set of numbers. The program can be compiled using GCC (Aarch64 version) tool, and can be run on raspberry pie Linux system or QEMU + ARM64 experimental platform.
Experiment code:
.section .data //Define data segments .align 3 //Align with 2 ^ 3 my_data: //Define a data .quad 1 .quad 2 .quad 5 .quad 8 .quad 10 .quad 12 my_data_count: //Number of arrays .quad 6 .align 3 print_data: //Define a string .string "big data: %d\n" .section .text //Define code snippets .globl main //Define entry function main main: stp x29, x30, [sp, -16]! //x29 is the FP stack frame register, x30 is the connection register, which is put into the stack ldr x0, =my_data //Read my_ Address of data tag ldr x1, my_data_count //Read my_data_count tag content (6) add x4, x0, #40 / / the address of the last digit in the array is stored in x4 mov x3, xzr //x3 temporary register, initial value 0 1: ldr x2, [x0], #8 / / the value at x0 address (array my_data[0]), load it into x2 register, and then update x0=x0+8 cmp x2, x3 csel x3, x2, x3, hi //csel condition selection: when the condition is satisfied (HI, x2 > x3), x2 is returned and stored in X3; otherwise, X3 is returned; X3 save Max cmp x0, x4 //Judge whether x0 is my_data last number b.ls 1b //Conditional (unsigned less than or equal to) jump 1b, ldr x0, =print_data //Load print_data tag address //adrp x0, print_data //add x0, x0, :lo12:print_data mov x1, x3 //Pass parameters to the printf function through x0 and x1 bl printf ldp x29, x30, [sp], 16 //Recover x29,x30 registers from start function stack ret //Function return exit
Use the command gcc 3-2.s -o 3-2 -g to enter gdb debugging
Main function sets breakpoint: (gdb) b main
Run the test program: (gdb) r
Single step operation: (gdb) s
View register value: (gdb) info reg x3
View run results:
Experiment 3-3: assembly language practice -- calling assembly functions through C language
1. Experimental purpose
Through this experiment, understand and be familiar with how to call assembly functions in C language.
2. Experimental requirements
Use assembly language to implement an assembly function, which is used to compare the size of two numbers and return the maximum value, and then call the assembly function with C language code. The program can be compiled with GCC (Aarch64 version) tool, and can run on raspberry Linux system or QEMU + ARM64 experimental platform.
3. Code
.section .text .globl compare_data compare_data: cmp x0, x1 csel x0, x0, x1, hi # Conditional selection instruction ret
#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); }
The compilation instruction GCC - O main. C compare. S -- Static - G # - G contains debugging information
The operation results are shown in the figure below:
Experiment 3-4: assembly language practice -- calling C functions through assembly language
1. Experimental purpose
Through this experiment, understand and be familiar with how to call C functions in assembly language.
2. Experimental requirements
A function is implemented in C language to compare the size of two numbers and return the maximum value, and then the C function is called with assembly code. The program can be compiled with GCC (Aarch64 version) and can be run on raspberry Linux system or QEMU + ARM64 experimental platform.
code:
3-4compare.c
int compare_data(int a, int b) { return (a >= b) ? a : b; }
3-4main.s
.section .data .align 3 print_data: .string "big data: %d\n" .section .text .globl main main: stp x29, x30, [sp, -16]! mov x0, #6 mov x1, #5 bl compare_data mov x1, x0 ldr x0, =print_data bl printf ldp x29, x30, [sp], 16 ret
Compilation instruction gcc -o 3-4main 3-4compare.c 3-4main.s --static -g
Operation results:
Experiment 3-5: assembly language practice - GCC inline assembly
1. Experimental purpose
Through this experiment, understand and be familiar with the use of GCC inline assembly.
2. Experimental requirements
Use GCC inline assembly to implement a function to compare the size of two numbers and return the maximum value, and then call this function with C language code. The program can be compiled with GCC (Aarch64 version) tool and can be run on raspberry Linux system or QEMU + ARM64 experimental platform.
code:
#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); }
Compile instruction gcc -o 3-5main 3-5main.c -g
Operation results:
Experiment 3-6: write a bare metal program on raspberry pie
Environmental preparation
When building projects on different platforms, first prepare the following environment:
1.arm cross compilation tool chain
2. Tools for executing makefile can be used
The cross compilation tool chain arm-none-eabi-gcc-5.4.1 can be downloaded on the official website or online disk:
Link: https://pan.baidu.com/s/1ad3d-pl4YbpgYrxKg6mX9g
Extraction code: f3cm
Raspberry pie
2. Hardware configuration of raspberry pie 4B
The performance of raspberry pie 4b is very strong, and there is no pressure to run Android and win10.
1.5GHz 4-core 64 bit ARM Cortex-A72 CPU (~3 × Multiple performance)
1GB/2GB/4GB LPDDR4 SDRAM memory
Full throughput Gigabit Ethernet
Dual band 802.11ac wireless network
Bluetooth 5.0
Two USB 3.0 and two USB 2.0 interfaces
Dual display support, resolution up to 4K
VideoCore VI graphics card supporting OpenGL ES 3.x
HEVC video 4Kp60 hard decoding
Source code download
The source code project already exists in GitHub. The relevant source code can be found at the following address:
https://github.com/bigmagic123/raspi4-bare-metal.git
You can see the list of related files in arm/1.compilation_environment.
The function of this code is to start jump to c code for execution, and then output hello world program through serial port.
Raspberry pie hardware link
Raspberry Pie 4 needs to prepare an empty SD card, a serial port line to output information and a TYPEC line to supply power.
First, put the files under sd_boot of the project directory into the empty SD card directory, and then put the compiled kernel7.img into the SD card.
Then connect the serial port and power on the development board. You can see the serial port output information in putty and other software.
Since there is no sd card available at present, the last step will be completed in the future.