Module 2(Lecture 2)
Module 2(Lecture 2)
SYSTEMS (17EC62)
1
The Cortex-M3 provides Rotate and Shift Instructions.
In some cases, the rotate operation can be combined with other operations (for
example, in memory address offset calculation for load/store instructions).
Again, a 32-bit version of the instruction is used if "S" suffix is not used and if
UAL syntax is used.
provides the two instructions. Both 16-bit and 32-bit versions are available. The
16-bit version can only access low registers.
Sign Extend Instructions
Instruction Operation
SXTB Rd, Rm ; Rd = signext(Rm[7:0]) Sign extend byte data into word
SXTH Rd, Rm ; Rd = signext(Rm[15:0]) Sign extend half word data into word
Instruction Operation
SBFX.W Rd, Rn, #<lsb>, #<width> Copy bit field from source and sign extend it
UBFX.W Rd, Rn, #<lsb>, #<width> Copy bit field from source register
3. Execution Flow control instructions
Assembler Language: Call and Unconditional Branch:
The most basic branch instructions are as follows:
B label ; Branch to a labeled address
BX reg ; Branch to an address specified by a register
In BX instructions, the LSB of the value contained in the register determines the next
state (Thumb/ARM) of the processor.
In the Cortex-M3, because it is always in Thumb state, this bit should be set to 1. If it is
zero, the program will cause a usage fault exception because it is trying to switch the
processor into ARM state.
However, when using BLX, make sure that the LSB of the register is 1. Otherwise the
processor will produce a fault exception because it is an attempt to switch to the ARM
state.
You can also carry out a branch operation using MOV instructions and LDR
instructions. For example,
MOV R15, R0 ; Branch to an address inside R0
LDR R15, [R0] ; Branch to an address in memory location ;
; specified by R0
POP {R15} ; Do a stack pop operation, and change the program counter
Instruction List
16-Bit Branch Instructions
Instruction Function
B Branch
Branch with link; call a subroutine and store the return address in LR (this
BL is actually a 32-bit instruction, but it is also available in Thumb in
traditional ARM processors)
BLX Branch with link and change state (BLX <reg> only)
Instruction Function
B Branch
TBB Table branch byte; forward branch using a table of single byte offset
TBH Table branch half word; forward branch using a table of half word offset
Assembler Language: Decisions and Conditional Branches
Z (Zero) flag: This flag is set when the result of an instruction has a zero value
or when a comparison of two data returns an equal result.
N (Negative) flag: This flag is set when the result of an instruction has a
negative value (bit 31 is 1).
C (Carry) flag: This flag is for unsigned data processing—for example, in add
(ADD) it is set when an overflow occurs; in subtract (SUB) it
is set when a borrow did not occur (borrow is the invert of carry).
V (Overflow) flag: This flag is for signed data processing; for example, in an
add (ADD), when two positive values added together produce a
negative value, or when two negative values added together produce a
positive value
With combinations of the four flags (N, Z, C, and V), 15 branch conditions are defined.
Using these conditions, branch instructions can be written as, for example,
BEQ label ; Branch to address 'label' if Z flag is set.
You can also use the Thumb-2 version if your branch target is further away. For example,
BEQ.W label ; Branch to address 'label' if Z flag is set
Conditions for Branches or Other Conditional Operations
Symbol Condition Flag
EQ Equal Z set
NE Not equal Z clear
CS/HS Carry set/unsigned higher or same C set
CC/LO Carry clear/unsigned lower C clear
MI Minus/negative N set
PL Plus/positive or zero N clear
VS Overflow V set
VC No overflow V clear
HI Unsigned higher C set and Z clear
LS Unsigned lower or same C clear or Z set
GE Signed greater than or equal N set and V set, or N clear and V clear (N == V)
LT Signed less than N set and V clear, or N clear and V set (N != V)
GT Signed greater than Z clear, and either N set and V set, or N clear and V clear (Z == 0, N == V)
LE Signed less than or equal Z set, or N set and V clear, or N clear and V set (Z == 1 or N != V)
AL Always (unconditional) —
The defined branch conditions can also be used in IF-THEN-ELSE structures.
For example,
CMP R0, R1 ; Compare R0 and R1
ITTEE GT ; If R0 > R1 Then if true, first 2 statements execute, if false, other 2
statements execute
MOVGT R2, R0 ; R2 = R0
MOVGT R3, R1 ; R3 = R1
MOVLE R2, R0 ; Else R2 = R1
MOVLE R3, R1 ; R3 = R
With ARM architecture v7-M, two new instructions are provided on the Cortex-M3 to supply a simple compare
with zero and conditional branch operations. These are CBZ (compare and branch if zero) and
CBNZ (compare and branch if nonzero).
Assembler Language: Combined Compare and Conditional Branch
With ARM architecture v7-M, two new instructions are provided on the Cortex-M3 to
supply a simple compare with zero and conditional branch operations. These are CBZ
(compare and branch if zero) and CBNZ (compare and branch if nonzero).
For example, i = 5;
while (i != 0 ){ func1(); ; call a function
i--;
}
This can be compiled into the following:
MOV R0, #5 loop1 ; Set loop counter
CBZ R0,loop1exit ; if loop counter = 0 then exit the loop
BL func1 ; call a function
SUB R0, #1 B loop1 ; loop counter decrement
loop1 exit ; next loop
The usage of CBNZ is similar to CBZ, apart from the fact that the branch is
taken if the Z flag is not set (result is not zero). For example,
status = strchr(email_address, '@');
if (status == 0)
{ //status is 0 if @ is not in email_address
show_error_message();
exit(1);
}
BL strchr
CBNZ R0, email_looks_okay ; Branch if result is not zero BL
show_error_message BL exit
email_looks_okay
The APSR value is not affected by the CBZ and CBNZ instructions .
Conditional Execution Using IT Instructions
The IT (IF-THEN) block is very useful for handling small conditional code. It
avoids branch penalties because there is no change to program flow. It can provide
a maximum of four conditionally executed instructions. In IT instruction blocks,
the first line must be the IT instruction, detailing the choice of execution, followed
by the condition it checks.
IT<x><y><z> <cond> ;
instr3<cond or not cond> <operands> ; 3rd instruction (can be; <cond> or <!cond>)
instr4<cond or not cond> <operands> ; 4th instruction (can be; <cond> or <!cond>)
Conditional Execution Using IT Instructions
If a statement is to be executed when <cond> is false, the suffix for the instruction must
be the opposite of the condition.
For example, the opposite of EQ is NE, the opposite of
GT is LE, and so on. The following code shows an example of a simple conditional
execution:
You can have fewer than four conditionally executed
instructions. The minimum is 1. You need to make sure
the number of T and E occurrences in the IT instruction
If (R1<R2) matches the number of conditionally executed
then instructions after the IT.
R2=R2-R1
R2=R2/2
else R1=R1- If an exception occurs during the IT instruction block,
R2 the execution status of the block will be stored in the
R1=R1/2 stacked PSR (in the IT/Interrupt-Continuable
In Instruction [ICI] bit field). So, when the exception
assembly, handler completes and the IT block resumes, the rest of
CMP R1,
R2 ITTEE If R1 < R2 (less then) the instructions in the block can continue the execution
LT correctly.
SUBLT.W ; then execute instruction 1 and
R2,R1 2(indicated by T) else execute
LSRLT.W instruction 3 and 4 (indicated by E) In the case of using multicycle instructions (for
R2,#1
SUBGE.W 1st instruction 2nd instruction example, multiple load and store) inside an IT block, if
R1,R2 3rd instruction (notice the GE is an exception takes place during the execution, the
LSRGE.W whole instruction is abandoned and restarted after the
R1,#1 opposite of LT) 4th instruction 37
interrupt process is completed.
Conditional Execution Using IT Instructions
If a statement is to be executed when <cond> is false, the suffix for the instruction
If an exception occurs during the IT instruction block, the execution status of the
block will be stored in the stacked PSR (in the IT/Interrupt-Continuable
Instruction [ICI] bit field). So, when the exception handler completes and the IT
block resumes, the rest of the instructions in the block can continue the execution
correctly.
In the case of using multicycle instructions (for example, multiple load and store)
inside an IT block, if an exception takes place during the execution, the whole
instruction is abandoned and restarted after the interrupt process is completed.
Thank You