Skip to content

Commit 9c858ba

Browse files
committed
Add C and ASM examples.
1 parent f82f493 commit 9c858ba

File tree

9 files changed

+547
-0
lines changed

9 files changed

+547
-0
lines changed

samples/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,11 @@ This application panics, printing a nice panic message.
4141
## [`fault`](./fault)
4242

4343
This application generates a Hard Fault.
44+
45+
## [`asmhello`](./asmhello)
46+
47+
A basic "Hello, world" but written in ARM assembly. It prints the string "Hello, world" to *standard output* and then exits with an exit code of 0.
48+
49+
## [`chello`](../chello)
50+
51+
A basic "Hello, world" but written in C. It prints the string "Hello, world" to *standard output* and then exits with an exit code of 0.

samples/asmhello/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.elf
2+
*.o

samples/asmhello/asmhello.S

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
.syntax unified
2+
.thumb
3+
.cpu cortex-m0plus
4+
.global app_entry
5+
.thumb_func
6+
7+
.text
8+
.section ".text"
9+
10+
// r0 will contain a pointer to the syscall table
11+
// the write function is the third entry (offset 8)
12+
// and takes the arguments r0=handle, r1=pointer, r2=length
13+
app_entry:
14+
// Save registers
15+
push {r0, r1, r2, lr}
16+
// Fetch write function address
17+
ldr r3, [r0, #8]
18+
// Set up data length
19+
movs r2, #13
20+
// Set up file handle
21+
movs r0, #1
22+
// Set up data pointer
23+
ldr r1, =message
24+
// Call write function
25+
blx r3
26+
// Set return value
27+
movs r0, #0
28+
// Exit
29+
pop {r1, r2, r3, pc}
30+
31+
.data
32+
.section ".rodata"
33+
34+
message:
35+
.ascii "Hello world!\n"

samples/asmhello/build.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
arm-none-eabi-gcc -mcpu=cortex-m0 -Wl,-T../neotron-cortex-m.ld -o asmhello.elf asmhello.S -nostdlib

samples/build.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ cargo build
2424
echo "Building for ${TARGET}"
2525
cargo build --target ${TARGET} --release
2626

27+
pushd chello
28+
./build.sh
29+
popd
30+
31+
pushd asmhello
32+
./build.sh
33+
popd
34+
2735
for program in panic hello fault input-test; do
2836
cp ./target/${TARGET}/release/${program} ./release/${program}.elf
2937
done
38+
cp ./asmhello/asmhello.elf ./release
39+
cp ./chello/chello.elf ./release

samples/chello/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.elf

samples/chello/build.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
arm-none-eabi-gcc -Os -mcpu=cortex-m0 -Wl,-T../neotron-cortex-m.ld -o chello.elf chello.c -nostdlib

samples/chello/chello.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Basic C sample which runs on Neotron OS.
3+
*/
4+
5+
#include "neotron.h"
6+
7+
int app_entry(const NeotronApi *f)
8+
{
9+
FfiByteSlice buffer = {
10+
.data = "Hello, world",
11+
.data_len = 12
12+
};
13+
Handle fd = {
14+
// fd 1 is STDOUT
15+
._0 = 1
16+
};
17+
f->write(fd, buffer);
18+
return 0;
19+
}

0 commit comments

Comments
 (0)