(ARM).Using the ARM assembler
.pdfUsing the Barrel Shifter
Value resulting from rotating right with extend
The value of the operand is taken by rotating the register and the carry bit one bit right. Because the carry is included in the shift, 33 bits are affected. This is known asrotate right extended.
Syntax: register-name, RRX
Example: r0, RRX
Example use:
MOV r1, r0, RRX ; move rotate right extended contents of r0 into r1
Register shifted by n bits
The register operand is shifted by n bits, where the value n is taken from the least significant byte of a register.
Syntax: register-name, shift-type register-name
Example: r0, LSR r2
Example use:
MOV r1, r0. LSR r2 ; move contents of r0 divided by 2^r2 into r1
Application Note 50
ARM DAI 0050A |
19 |
Open Access
Access to C Global Variables from Assembly Code
5 Access to C Global Variables from Assembly Code
The simplest way to access C global variables from assembly language source is to
IMPORT your global variables into your assembler source and access them directly.
For example:
;File sub1.s:
AREA globals,CODE
EXPORT asmsubroutine
IMPORT globvar
asmsubroutine |
|
|
LDR |
r1, |
=globvar |
LDR |
r0, |
[r1] |
ADD |
r0, |
r0, #2 |
STR |
r0, |
[r1] |
MOV |
pc, |
lr |
END |
|
|
This loads the address of globvar into r1, loads the value contained by r1 into r0, manipulates it, then stores the new value back into globvar.
A problem occurs if the variable’s memory address is greater than 4Kbytes from the LDR instruction, which may be caused when using scatter loading or large source files. The way to avoid this is to give globvar a label and then access it through the label:
;File sub2.s |
|
|
|
AREA |
globals,CODE |
|
|
EXPORT asmsubroutine |
|
||
IMPORT globvar |
|
||
asmsubroutine |
|
|
|
LDR |
r1, |
globaddr |
|
LDR |
r0, |
[r1] |
|
ADD |
r0, |
r0, #2 |
|
STR |
r0, |
[r1] |
|
MOV |
pc, |
lr |
|
globaddr |
|
DCD |
globvar |
;More code can follow here without putting "globaddr" out of
;range of the LDR.
END
|
Application Note 50 |
20 |
ARM DAI 0050A |
Open Access
Access to C Global Variables from Assembly Code
The above are effectively equivalent pieces of code: using the LDR Rn,=expression syntax causes the Assembler to create a “label DCD expression” for itself and then try to access it via LDR Rn,label (choosing label so as not to conflict with any other labels in the program). If you write globaddr DCD globvar at the end of the program, you encounter LDR offset problems at precisely the same point with the second piece of code above as with the first.
The difference is that you can write globaddr DCD globvar where you like, and you can usually find a safe place to put it that lies within range of the LDR, or create a safe place by inserting an unconditional branch around the DCD.
However, rather than explicitly placing labels in your code at a place you know can be reached from the current PC, you can use the LTORG directive to tell the Assembler that data items generated by LDR Rn,=expression instructions can safely be placed where the directive appears in your source. If this directive is not used, the Assembler assumes that the only such place is at the END directive of the outermost file being assembled.
So the first sample piece of code above can be made to work, even if the assembler source file becomes very long, by changing it to:
; File sub1.s
AREA globals, CODE
EXPORT asmsubroutine
IMPORT globvar
asmsubroutine |
|
|
LDR |
r1, |
=globvar |
LDR |
r0, |
[r1] |
ADD |
r0, |
r0, #2 |
STR |
r0, |
[r1] |
MOV |
pc, |
lr |
LTORG
;More code can follow here without putting the location
;containing"globvar" out of range of the LDR.
END
Application Note 50
ARM DAI 0050A |
21 |
Open Access