Académique Documents
Professionnel Documents
Culture Documents
Each field must be separated by one or more <whitespace> (such as a space or a tab). Actual instructions never start in the first column, since they must be preceded by whitespace, even if there is no label. All three sections are optional
Asm program
AREA ARMex, CODE, READONLY ENTRY start : MOV r0, #10 MOV r1, #0x20 ADD r0, r0, r1 stop: B stop END
/* Mark end of file */ /* r0 = r0 + r1 */
/*
/*
/* Set up parameters */
Directives are instructions to the assembler program, NOT to the microprocessors AREA Directive - specifies chunks of data or code that are manipulated by the linker.
The example above consists of a single area which contains code and is marked as being read-only. A single CODE area is the minimum required to produce an application. A complete application can consist of one or more areas.
An application can contain only a single entry point and so in a multisource-module application, only a single module will contain an ENTRY directive. Instructs the assembler to stop processing this source file
Assembly Directives
Labels Labels are symbols that represent addresses. The address given by a label is calculated during assembly. The assembler calculates the address of a label relative to the origin of the section where the label is defined. A reference to a label within the same section can use the PC plus or minus an offset. This is called program-relative addressing.
Calling subroutines
Adds the values of two parameters and returns a result in r0. AREA subrout, CODE, READONLY ENTRY
start:
subroutine doadd is called by using a branch with link instruction (BL branch address).
BL copies the address of the next instruction into r14 (lr=link register), then fills PC=branch address (go to branch address).
It then returns by simply restoring the program counter to the address which was stored in the link register (r14) on entry.
Description of ex1
The main routine of the program (labelled start ) loads the values 10 and 20 into registers 0 and 1. It calls the subroutine doadd by using a branch with link instruction (BL branch address). BL copies the address of the next instruction into r14 (lr=link register), then fills PC=branch address (go to branch address). The subroutine adds together the two parameters it has received and places the result back into r0. It then returns by simply restoring the program counter to the address which was stored in the link register (r14) on entry.
The S suffix
Data processing instructions of ARM will not modify the condition flags in CPSR. But we can modify the condition flags by appending S bit to the instruction. Example
MOV r3, #00 .. This instruction will not update zero flag. MOVS r3, #00 This instruction will update the zero flag.
31 30 29 28
7 6
N = Negative result from ALU is set to bit 31 of result Z = Zero result from ALU C = ALU operation Carried out V = ALU operation Overflowed
I bit When set to 1 disables IRQ interrupts F bit When set to 1 disables FIQ interrupts
M0, M1,M2, M3, M4 are mode bits. These determine the mode in which processor operates. (Tells operating mode of processor)
The S suffix
#AREA ARMex, CODE, READONLY #ENTRY start: MOV r0, #-3 MOV r1, #3 ADD r0, r0, r1 B stop
stop: #END
The S suffix
#AREA ARMex, CODE, READONLY #ENTRY start: MOV r0, #-3 MOV r1, #3 ADDS r0, r0, r1 B stop
stop: #END
This example conditional flags of CPSR will be modified because the S suffix is added to ADD instruction
Loop
again:
Branch if Z-flag is not set to again mov r0,#10 mov r1,#0 again: add r1,r1,#3 subs r0, r0,#1
ARM instructions
All instructions are 32 bits long. Most instructions execute in a single cycle. Every instruction can be conditionally executed. A load/store architecture
Data processing instructions act only on registers Three operand format Combined ALU and shifter for high speed bit manipulation
32 bit and 8 bit data types and also 16 bit data types on ARM Architecture v4. Flexible multiple register load and store instructions
3-address instruction format 2 address instruction format Instruction set extension via coprocessors
ARM instructions
Data processing instructions Load and store instructions Semaphore instructions Multiply instructions Exception generating instruction Branch instructions Coprocessor instruction
MOV MVN AND ORR EOR BIC ADD ADC SUB SBC RSB RSC CMP CMN TST TEQ
These instructions only work on registers, NOT memory. Syntax: 3 operands - 2 for inputs and 1 for result
<Operation>{<cond>}{S} Rd, Rn, Operand2
All operands are 32 bits, come either from registers or are specified as constants (called literals) in the instruction itself
Data Movement
Operations are:
Barrel shifter
One of the operand of ALU always comes from barrel shifter. This unique & powerful feature allows 32 bit binary pattern of the source register to be shifted left or right before it enters ALU.
Barrel shifter
C statement: int i=10, j; j = I << 2;
MOV
ADD
Logical Operations
Syntax: <Operation>{<cond>}{S} Rd, Rn, N
AND ORR EOR BIC bitwise AND of two 32-bit values bitwise OR of two 32-bit values bitwise XOR of two 32-bit values Logical bit clear Rd Rd Rd Rd = = = = Rn Rn Rn Rn &N |N N & N
Examples:
Arithmetic Operations
Operations are:
ADD operand1 + operand2 ADC operand1 + operand2 + carry SUB operand1 - operand2 SBC operand1 - operand2 - !(carry flag) RSB operand2 - operand1 RSC operand2 - operand1 - !(carry flag)
Examples:
r0 := r1+ r2 r0 := r1 r2 - !(carry) r0 := r2 r1
Comparisons
The only effect of the comparisons is to UPDATE THE CONDITION FLAGS. Operations are:
CMP operand1 - operand2 CMN operand1 + operand2 TST operand1 AND operand2 TEQ operand1 EOR operand2
Corresponding operation is carried out conditional flags are updated depending on the result so that subsequent instruction can be conditionally executed
CMP r1 , r2 r1 r2
Examples:
r1 + r2 r1 and r2 r1 xor r2
Comparisons
TEQ is useful
When TEQ is executed, N flag is logical exor of the sign bits of two operands hence we can check whether two values have the same sign. TST is used to check if register bits include at least one bit set.
Conditional execution
Most ISAs only allow branches to be executed conditionally. But ARM ISA has a unique and clever way of dealing with conditional branches. Instead of having special conditional branch instructions, ARM ISA implements a feature of executing ALL instructions conditionally.
For this every instruction contains a conditional field which determines whether the instruction will be executed or ignored. Thus CPU will execute the instruction only if the condition is satisfied.
Conditional execution
To execute an instruction conditionally, simply postfix it with appropriate condition code: For example an add instruction takes the form:
ADD r0,r1,r2
; r0 = r1 + r2
To execute this only if the zero flag is set: ADDEQ r0,r1,r2 ; If zero flag set then .. r0 = r1 + r2
Instruction will be executed only if N,Z,C & V satisfy the condition specified in instruction. If flags do not satisfy the condition instruction acts as NOP & execution advances to next instruction.
Condition Codes
This improves code density and performance by reducing the number of forward branch instructions.
Conditional execution reduces number of branches that reduces number of pipeline flushes and thus improves the performance.
Conditional execution
CMP r0, #5 ; BEQ BYPASS ; if (r0!=5) r1:=r1+r0-r2 ADD r1, r1, r0 SUB r1, r1, r2 BYPASS: ...
Conditional execution
MOVEQ r0,r1 MOVS r0, r1, LSR #1 MOVCC r0, #10 MOVCS r0, #11
; r0 := r1 if Z flag set ; C(flag) := r1[0] ; if C=0 then r0:=10 ;if C=1 then r0:=11
MOV r1,#10 loop ADD R3, R3,#3 SUBS r1,r1,#1 BNE loop