diff --git a/arm_asm/Makefile b/arm_asm/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..24c58efcd06fbf3ff60f8e0718d1ee9ac039f7c2 --- /dev/null +++ b/arm_asm/Makefile @@ -0,0 +1,18 @@ + +.PHONY: run debug clean + +all: main.arm32 + +main.arm32: main.c add.S mywrite.S + arm-linux-gnueabihf-gcc -marm -g -O0 -o $@ $^ + +run: main.arm32 + qemu-arm -L /usr/arm-linux-gnueabihf/ $< + +debug: main.arm32 + @echo "attach using" + @echo "arm-none-eabi-gdb $<\n" + qemu-arm -g 1234 -L /usr/arm-linux-gnueabihf/ $< + +clean: + rm -f main.arm32 diff --git a/arm_asm/README.md b/arm_asm/README.md new file mode 100644 index 0000000000000000000000000000000000000000..671b8686ff035f6e642e4ec06bdd5106a27e7ba4 --- /dev/null +++ b/arm_asm/README.md @@ -0,0 +1,4 @@ +Simple example for a C project integrating ARM assembly. +You need an arm toolchain to build this. +Change the compiler to your needs (`arm-linux-gnueabihf-gcc`). +The `Makefile` uses `qemu-user` to run the example. diff --git a/arm_asm/add.S b/arm_asm/add.S new file mode 100644 index 0000000000000000000000000000000000000000..3908067bf4a84548cdf01f7b62ff63aaa2cc4367 --- /dev/null +++ b/arm_asm/add.S @@ -0,0 +1,7 @@ +.section .text.add, "x" + +.global add + +add: + add r0, r0, r1 + mov pc, lr diff --git a/arm_asm/main.c b/arm_asm/main.c new file mode 100644 index 0000000000000000000000000000000000000000..5edc824018ba37729bf5a5f181c5a3772db44ac0 --- /dev/null +++ b/arm_asm/main.c @@ -0,0 +1,26 @@ +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +extern int add(int, int); +extern int mywrite(int fd, void* buf, size_t count); + +int sub(int a, int b){ + return a - b; +} + +int main(int argc, char** argv){ + int r = 0; + char* name = "Hello Reverser!\n"; + + write(STDOUT_FILENO, name, strlen(name)); + + r = sub(20, 10); + printf("sub(20, 10) = %d\n", r); + r = add(10, 20); + printf("add(10, 20) = %d\n", r); + + mywrite(1, "Hello!\n", 8); + return EXIT_SUCCESS; +} diff --git a/arm_asm/mywrite.S b/arm_asm/mywrite.S new file mode 100644 index 0000000000000000000000000000000000000000..1a67dcc4e07d158ed72449772a96bd1d925ae0d4 --- /dev/null +++ b/arm_asm/mywrite.S @@ -0,0 +1,9 @@ +.section .text.mywrite, "x" + +.global mywrite + +mywrite: + push {r7, lr} + mov r7, #4 + swi 0 + pop {r7, pc}