E.Bil
E.Bil

Reputation: 21

cross compile an arm assembly and simulate non OS arm environment wih qemu on linux

Currently, I'm trying to test an arm assembly code that I wrote. I work on Ubuntu, so I downloaded a cross compiler tool chain (arm-linux-gnueabi) so I can compile my code and then I test it using qemu-arm. But when I try to compile with arm-none-eabi-gcc it compiles but it doesn't work with qemu-arm. My guess is it doesn't work because I'm compiling for bare metal arm environment. My question is how can I use qemu-system-arm instead of qemu-arm to simulate a bare metal arm environment and test my code ?

Upvotes: 2

Views: 966

Answers (1)

old_timer
old_timer

Reputation: 71506

You want assembly you only need binutils, dont use a C compiler on assembly, it may work but doesnt that just leave a bad taste in your mouth? You probably didnt separately link and/or left the stock bootstrap and linker script with arm-non-eabi-gcc. The example below does not care about arm-none-eabi- vs arm-linux-gnueabi-

Qemu uarts tend to not actually implement an amount of time to wait for the character to go out, nor need any initialization, YMMV.

memmap

MEMORY
{
    ram  : ORIGIN = 0x00000000, LENGTH = 32K
}
SECTIONS
{
   .text : { *(.text*) } > ram
}

so.s

.globl _start
_start:
    b reset
    b hang
    b hang
    b hang
    b hang
    b hang
    b hang
    b hang

hang: b hang

reset:
    ldr r0,=0x101f1000
    mov r1,#0
top:
    add r1,r1,#1
    and r1,r1,#0x07
    orr r1,r1,#0x30
    str r1,[r0]
    b top

build

arm-linux-gnueabi-as --warn --fatal-warnings -march=armv5t so.s -o so.o
arm-linux-gnueabi-ld so.o -T memmap -o notmain.elf
arm-linux-gnueabi-objdump -D notmain.elf > notmain.list
arm-linux-gnueabi-objcopy notmain.elf -O binary notmain.bin

run

qemu-system-arm -M versatilepb -m 128M -nographic -kernel notmain.bin

then ctrl-a then x to exit the qemu console back to the command line.

This will print out 1234567012345670... forever or until you stop it

Another way to run is

qemu-system-arm -M versatilepb -m 128M -kernel notmain.bin

and then ctrl-alt-3 (not F3 but 3) will switch to the serial0 console and you can see the output, and can close out of the qemu console when done.

There are other machines you can experiment with. Their peripherals of course will vary, as well as the architecture, most should be either compatible with armv4 arm instructions or thumb instructions if a cortex-m.

Adding C functions to this is fairly simple.

Upvotes: 1

Related Questions