ARM Flow Control Instructions
ARM Flow Control Instructions
ARM's Flow Control Instructions modify the default sequential execution. They control the operation
of the processor and sequencing of instructions.
1
R0 to R12 are the general-purpose registers.
R13 is reserved for the programmer to use it as the stack pointer.
R14 is the link register which stores a subroutine return address.
R15 contains the program counter and is accessible by the programmer.
The arithmetic and logic instructions affect the condition code flags only if explicitly specified to do so
by a bit in the OP-code field. This is indicated by appending the suffix S to the OP-code.
For example, the instruction ADDS R0, R1, R2 sets the condition code flags. But ADD R0, R1,
R2 does not.
Offset is a signed 24-bit number. It is shifted left two-bit positions (all branch targets are aligned word
addresses), signed extended to 32 bits, and added to the updated PC to generate the branch target
address. The updated PC points to the instruction that is two words (8 bytes) forward from the branch
instruction.
ARM instructions are conditionally executed depending on a condition specified in the instruction. The
instruction is executed only if the current state of the processor condition code flag satisfies the
condition specified in bits b31-b28 of the instruction. Thus the instructions whose condition does not
meet the processor condition code flag are not executed. One of the conditions is used to indicate
2
that the instruction is always executed.
Here is a more detailed description.
All the ARM instructions are conditionally executed depending on a condition specified in the
instruction(bits 31-28).
The instruction is executed only if the current state of the processor condition code
flag satisfies the condition specified in bits b31-b28 of the instruction.
For example:
3
CMP R0, #'A' ; flags are updated according to (R0 - #'A')
BEQ VowelCount
The instructions whose condition does not meet the processor condition code flag
are not executed.
One of the conditions is used to indicate that the instruction is always executed.
------------------------------------------------------------------------
B loopA ; Branch to label loopA unconditioally
------------------------------------------------------------------------
BEQ target ; Conditionally branch to target, when Z = 1
------------------------------------------------------------------------
4
Examples of Compare Instructions
Mnemonic Meaning
------------------------------------------------------------------------
CBZ R5, target ; Forward branch if R5 is zero
------------------------------------------------------------------------
CBNZ R0, target ; Forward branch if R0 is not zero
------------------------------------------------------------------------
CMP R2, R9 ; R2 - R9, update the N, Z, C and V flags
------------------------------------------------------------------------
CMN R0, #6400 ; R0 + #6400, update the N, Z, C and V flags
------------------------------------------------------------------------
CMPGT SP, R7, LSL #2 ; update the N, Z, C and V flags
------------------------------------------------------------------------
;;; Directives
PRESERVE8
THUMB
__Vectors
DCD 0x20001000 ; stack pointer value when stack is empty
DCD Reset_Handler ; reset vector
ALIGN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Byte array/character string
; DCB type declares that memory will be reserved for consecutive bytes
; You can list comma separated byte values, or use "quoted" characters.
; The ,0 at the end null terminates the character string. You could also use "\0".
; The zero value of the null allows you to tell when the string ends.
5
;
; The DCB directive allocates one or more bytes of memory, and defines the initial
; runtime contents of the memory.
;
; Example
; Unlike C strings, ARM assembler strings are not null-terminated.
; You can construct a null-terminated C string using DCB as follows:
; C_string DCB "C_string",0
;
;**************************************************************************
string1
DCB "Hello world!",0
; The program
; Linker requires Reset_Handler
ENTRY
EXPORT Reset_Handler
Reset_Handler
LDR R0, = string1 ; Load the address of string1 into the register R0
loopCount
LDRB R2, [R0] ; Load the character from the address R0 contains
CMP R2, #0
BEQ countDone
; If it is zero...remember null terminated...
; You are done with the string. The length is in R1.
B loopCount
countDone
STOP
B STOP
6
Another Example
;The semicolon is used to lead an inline documentation
;When you write your program, you could have your info at the top document block
;For Example: Your Name, Student Number, what the program is for, and what it does
etc.
;
; See if you can figure out what this program does
;
;;; Directives
PRESERVE8
THUMB
__Vectors
DCD 0x20001000 ; stack pointer value when stack is empty
DCD Reset_Handler ; reset vector
ALIGN
; The program
; Linker requires Reset_Handler
ENTRY
EXPORT Reset_Handler
Reset_Handler
LOOP
ADD R0, R0, R1 ;Add number into R0
7
SUBS R1, R1, #1 ;Decrement loop counter R1
BGT LOOP ;Branch back if not done
STOP
B STOP
END
Lab work: