Skip to content

Instantly share code, notes, and snippets.

@iddoeldor
Last active January 31, 2019 16:41
Show Gist options
  • Save iddoeldor/5e5487b0259310364acbbb45be291454 to your computer and use it in GitHub Desktop.
Save iddoeldor/5e5487b0259310364acbbb45be291454 to your computer and use it in GitHub Desktop.
Run ARM assembly using Qemu on Ubuntu
cat <<EOT > hello.ld
ENTRY(_start)
MEMORY
{
ram : ORIGIN = 0x00010000, LENGTH = 0x1000
}
SECTIONS
{
.text : { *(.text*) } > ram
.rodata : { *(.rodata*) } > ram
.bss : { *(.bss*) } > ram
}
EOT
cat <<EOT > start.s
.thumb
.thumb_func
.global _start
_start:
mov r4,#10
top:
nop
sub r4,#1
bne top
mov r7,#0x1
mov r0,#0
swi #0
.word 0xFFFFFFFF
b .
.end
EOT
arm-none-eabi-as --warn --fatal-warnings start.s -o start.o
arm-none-eabi-ld -o notmain.elf -T hello.ld start.o
arm-none-eabi-objdump -D notmain.elf > notmain.list
arm-none-eabi-objcopy notmain.elf notmain.bin -O binary
qemu-arm -d in_asm,cpu,cpu_reset -D hello -cpu cortex-m4 notmain.elf
cat hello
#!/usr/bin/env bash
# sudo apt install binutils-arm-none-eabi qemu-system-arm
F='arm' # file name, it will generate: "$F.s, $F.o, $F.elf & $F.memmap"
cat <<EOT > $F.s
.globl _start
_start:
ldr r0,=0x101f1000
mov r1,#0
loop:
add r1,r1,#1
and r1,r1,#7
add r1,r1,#0x30
str r1,[r0]
mov r2,#0x0D
str r2,[r0]
mov r2,#0x0A
str r2,[r0]
b loop
EOT
cat <<EOT > $F.memmap
MEMORY
{
rom : ORIGIN = 0x00010000, LENGTH = 32K
}
SECTIONS
{
.text : { *(.text*) } > rom
}
EOT
# Makefile
arm-none-eabi-as $F.s -o $F.o --warn --fatal-warnings
arm-none-eabi-ld $F.o -T $F.memmap -o $F.elf
#arm-none-eabi-objdump -D $F.elf > $F.list
#arm-none-eabi-objcopy $F.elf -O $F.bin
# Run
echo "
To QUIT:
1. first press Ctrl + A (A is just key a, not the alt key)
2. release the keys
3. afterwards type X
Enjoy!
"
read -p "Press enter to continue"
qemu-system-arm -M versatilepb -m 128M -nographic -kernel $F.elf
#!/usr/bin/env bash
# sudo apt install gcc-aarch64-linux-gnu
F='arm' # file name, it will generate: "$F.s, $F.o, $F.elf & $F.memmap"
cat <<EOT > $F.s
.text
.globl main
main:
mov x8, #64
mov x0, #1
adr x1, msg
mov x2, 13
svc #0
ret
msg:
.ascii "hello world\n"
EOT
aarch64-linux-gnu-gcc -static -c arm.s
aarch64-linux-gnu-gcc -static -o arm arm.o
./arm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment