Professional Documents
Culture Documents
Experiment - 10 Inline Assembly & Thumb State: Objectives
Experiment - 10 Inline Assembly & Thumb State: Objectives
OBJECTIVES:
a) To write and execute a c language program that allows inline assembly
instructions.
b) To write and execute an assembly language program in arm state and thumb
state.
THEORY:
Inline assembly is a special provision in the ARM processors which allows users
to write ARM assembly language programs in higher language. Basic advantage of
such provision is to get high code density. This provision is mostly used where code
storage capacity of processors is less compared to the requirement. Restrictions on
inline assembly operations: There are a number of restrictions on the operations that
can be performed in inline assembly code. These restrictions provide a measure of
safety, and ensure that the assumptions in compiled C and C++ code are not violated in
the assembled assembly code.
MISCELLANEOUS RESTRICTIONS:
Some low-level features that are available in the ARM assembler arm asm, such as
branching and writing to PC, are not supported. x Label expressions are not
supported.
You cannot get the address of the current instruction using dot notation (.) or {PC}.
The & operator cannot be used to denote hexadecimal constants. Use the 0x prefix
instead. For example:__asm { AND x, y, 0xF00 }
The notation to specify the actual rotate of an 8-bit constant is not available in inline
assembly language. This means that where an 8-bit shifted constant is used, the C
flag must be regarded as corrupted if the NZCV flags are updated.
You must not modify the stack. This is not necessary because the compiler
automatically stacks and restores any working registers as required. The compiler
does not permit you to explicitly stack and restore work registers.
Registers:
Registers, such as r0-r3, sp, lr, and the NZCV flags in the CPSR must be used with
caution. If you use C or C++ expressions; these might be used as temporary registers
and NZCV flags might be corrupted by the compiler when evaluating the expression.
The pc, lr, and sp registers cannot be explicitly read or modified using inline assembly
code because there is no direct access to any physical registers. However, you can use
the following intrinsic to access these registers:
current_pc in the Compiler Reference Guide
current_sp in the Compiler Reference Guide
return_address in the Compiler Reference Guide.
Unsupported Instructions:
SAMPLE PROGRAM:
int main()
{
int a=10,b=10,c,d,e,f,g;
c=a+b;
__asm
{
mov e,0x04;
mov f,0x02;
add g,e,f;
}
d=a*b;
return 0;
}
OUTPUT:
The register window and Variable screen shows where the compiler and assembler
assign the values and to which register.
THEORY:
The Thumb instruction set addresses the issue of code density. It may be viewed as a
compressed form of a subset of the ARM instruction set. Thumb instructions map onto
ARM instructions, and the Thumb programmer's model maps onto the ARM
programmer's model. Implementations of Thumb use dynamic decompression in an
ARM instruction pipeline and then instructions execute as standard ARM instructions
within the processor.
SAMPLE PROGRAM:
AREA TEST, CODE, READONLY
EXPORT SQUARE
SQUARE
MOV R1,#&44
MOVS R2,#44
ADCS R3,R2,R1,ASR#5
SUBS R7,R2,R1,LSL#4
LDR R5,=FUN ;load the address of label FUN into R5
MOVS LR,PC ;Store the return address into Link Register
BX R5 ;Branch to the label FUN which is in THUMB state
ADD R1,#1
ADD R1,#1
ADD R1,#1
B FUNCTION
THUMB
FUN
LDR R6,=0X40000000 ;observe the changes in the instruction of ARM
and ;THUMB state
MOVS R7,R6
LDR R0,=65534
LDR R1,=280
MOVS R2,#54
MOVS R3,#0
STMIA R6!,{R0-R3}
MOVS R2,#0
LOOP
LDR R3,[R7]
ADDS R2,R3
ADDS R7,#4
CMP R6,R7
BGT LOOP
BX LR ;Return to the address stored in Link Register
ARM ;ARM state
FUNCTION
ADD R5,R0,R1
END
OUTPUT:
By checking CPSR [5] (TBIT), we can always say whether the processor is in ARM
state or Thumb state. If T bit = 1, it is in Thumb state otherwise in ARM state. The figure
below shows the initial execution of the program while the processor is still in ARM
state.
CONCLUSION:
In this experiment we learnt about the inline assembly. This feature allows the
user to write program in both high-level and assembly language in a single file. This
gives an advantage of increasing the code density thus allowing the user to perform
complex tasks with efficiency.
We also learnt about THUMB state and how it is useful in increasing the code
density of a program. THUMB state allows the execution of some restricted instructions
with half the code length as compared to that of ARM state (i.e., the machine code is
just 16bit long compared to 32bit long machine codes in ARM state). This allows writing
huge codes in a small memory space.