; ; Short ARM assembler program to illustrate different ; ways in which we can load constants and addresses ; into registers. ; ; rdm34@cl.cam.ac.uk ; 2.11.02 ; AREA simple, CODE, READONLY SWI_Exit EQU 0x11 ; define a constant FAV_NUM EQU 0x99 ; define another constant ENTRY ; define program entry point (start) ; ; a simple example ; MOV r0, #FAV_NUM ; r0=0x99 ; lets try something different.... ; lets load R3 with the value FFF, we can't do this ; in a single MOV instruction as FFF can't be ; represented as an immediate. We could use two ADD ; instructions, or load the value from a table in ; memory. Lets have a go at storing FFF in memory and ; then loading it into R3. ; So if you look at the end of the code we have added ; a single word of data with the value FFF. This will ; be stored at the address 'litpool' ; ; So lets load R4 with this address using PC relative ; addressing ; ; R4=PC+offset to value in litpool ; ; so R4=PC + (address in litpool) - (current PC) - 2 words ; The -2 words (8 bytes) is needed due to pipelining, ; the PC has actually moved on two before we perform ; the addition. The -8 corrects for this (don't worry ; too much about this!) ; ; So, the first instruction loads R4 with the address ; of our data (FFF) and the second loads r3 with the ; contents of memory address r4 ADD r4, pc, #litpool-.-8 LDR r3, [r4] ; ; instead of the above, we could just write the following ; and get the assembler to generate the code, it ; will also create a literal pool and write FFF into it. ; (very handy!) ; ; These sort of instructions are known as pseudo-instructions, ; as they are replaced by real instructions by the ; assembler. LDR r3, =0xFFF ; if want to load an address we can use the ; ADR and ADRL pseudo instructions. ; for ADR the assembler creates a single instruction ; for ADRL two instructions are produced allowing a wider ; range of addresses to be represented ADR r1, text ; would be replaced by an ADD r1, pc, #text-.-8 ; we can also use the LDR pseudo instruction to load ; addresses LDR r1, =text ; this doesn't create the address directly using ; PC relative addressing. Instead it looks in a table ; for an entry holding the address (much like the ; first example, instead of FFF we load the address ; of the 'text' string). ; ; The assembler expands the pseudo instruction to two ; instructions, one to form the address of the entry in ; the literal pool and another to load the value of this ; entry into r1. SWI SWI_Exit text DCB "Hello World\n",0 litpool DCD 0xfff END