assemblyarmraspberry-pibare-metal

ARMv6 Branch in Bare Metal Programming (Raspberry Pi B+)


I'm trying to write a very basic cross compiler for the Raspberry Pi B+ Version to build simple bare metal programs. My compiler is able to translate simple commands into the corresponding machine language instruction, using the ARM Instruction Set Datasheet.

Turning on LEDs (orientating myself on the baking pi course, http://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/os/) works fine.

But now I want to do some branch instructions, and this is where nothing seems to work anymore:

First I would like to branch to absolute address, no relative branches using the B / BL instruction.

To test a branch, I'm using the following disassembled code (disassembled using the Hopper Disassembler V3 Test Version), which turns on a LED connected with GPIO 16 and22:

00000000  mov r0, #0x20000000     ;Load the GPIO Base Address into R0
00000004  orr r0, r0, #0x200000
00000008  mov r1, #0x40           ;Load the Set Function Mask (for GPIO 22) into r1
0000000c  str r1, [r0, #0x8]      ;Store the Set Function Mask into the GPFSEL2
00000010  mov r1, #0x400000       ;Move the Set Output Mask (for GPIO 22) into r1
00000014  str r1, [r0, #0x1c]     ;Store the Set Output Mask into GPSET0

00000018  mov r0, #0x20000000     ;Load the GPIO Base Address into R0
0000001c  orr r0, r0, #0x200000
00000020  mov r1, #0x40000        ;Load the Set Function Mask (for GPIO 16) into r1
00000024  str r1, [r0, #0x4]      ;Store the Set Function Mask into the GPFSEL2
00000028  mov r1, #0x10000        ;Move the Set Output Mask (for GPIO 16) into r1
0000002c  str r1, [r0, #0x1c]     ;Store the Set Output Mask into GPSET0

00000030  b 0x30                  ;Infinity Loop to keep the processor up

Now I want to add a branch to the beginning of the code, to skip the first section, so that only the second LED is activated.

I tried it like that:

mov r15, #0x1c

but the only effect is that both LEDs stay dark.

My second attempt is like that:

mov r2, #0x20
bx r2

But that neither works.

So my question is:


Solution

  • Thanks to the hint from @TimothyBaldwin I've got now the answer (I hope its okay if I write a own answers providing more details instead of simply accepting his one).

    As mentioned by @TimothyBaldwin, the Problem was that the Programm is loaded at 0x8000, as you can see in the diagram found at https://raspberrypi.stackexchange.com/questions/10442/what-is-the-boot-sequence.

    As explained there, in my case it works perfectly nice if I add to my config.txt the following line:

    disable_commandline_tags=1
    

    because then the code is loaded at 0x0, and everything works as expected.