From 4b8bfe6ad3b26359fb8ee58776987ac0a1a3758c Mon Sep 17 00:00:00 2001 From: dex <marcel.busch@cs.fau.de> Date: Tue, 19 Jan 2021 16:33:35 +0100 Subject: [PATCH] add arm syscall example --- arm_asm/Makefile | 18 ++++++++++++++++++ arm_asm/README.md | 4 ++++ arm_asm/add.S | 7 +++++++ arm_asm/main.c | 26 ++++++++++++++++++++++++++ arm_asm/mywrite.S | 9 +++++++++ 5 files changed, 64 insertions(+) create mode 100644 arm_asm/Makefile create mode 100644 arm_asm/README.md create mode 100644 arm_asm/add.S create mode 100644 arm_asm/main.c create mode 100644 arm_asm/mywrite.S diff --git a/arm_asm/Makefile b/arm_asm/Makefile new file mode 100644 index 0000000..24c58ef --- /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 0000000..671b868 --- /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 0000000..3908067 --- /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 0000000..5edc824 --- /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 0000000..1a67dcc --- /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} -- GitLab