Professional Documents
Culture Documents
Microprocessor System
Microprocessor Assembly Language Programming
What we will learn in this section:
Example 2:
TST R1,#0xFF ;check to see if any bits of R1 is zero, if so ;Z=1. R1 remain unchanged
Microprocessor Assembly
Language Programming
CONDITIONAL BRANCH
WHEN CPU executes a conditional branch, it check a condition, if the
condition is true then it jumps to the target location, otherwise, it executes the
next instruction
➢ UNCONDITIONAL BRANCH
WHEN CPU executes a unconditional branch, it jumps unconditionally (without
checking any condition) to the target location,
ARM Conditional Branch
MOV R0,#0
SUBS R2,R2,#1
BNE AGAIN
Compare Instructions (Unsigned Number)
CMP Instruction
The CMP instruction compares two operands and changes the flags
according to the result of the comparison.
CMP Rn, Op2 ; Compare Rn with Op2 and set the flags
Example :
MOV R0,#50
MOV R1,#40
CMP R0,R1 ; will effect the flags, C=1 and Z=0
ARM Conditional Branch
Conditional branch (BNE) used for increment counter
;R0=R0+9
; increment R2 is a counter
NOTES: All branch is short jump size 32M Byte means that the address of the target must
be within 32M bytes of the program counter (PC). That means the short jumps cannot
cover the entire address space of 4G bytes (0x00000000 to 0xFFFFFFFF). To jump over 32M
byte can use BX
Unconditional Branch BX (branch and
exchange)
To branch beyond the address space of 32M bytes, we use BX instruction.
“BX Rn” instruction uses register Rn to hold target address.
Since Rn can be any of the R0–R14 registers and they are 32-bit registers,
the “BX Rn” instruction can land anywhere in the 4G bytes address space
of the ARM.
Subroutine
BL (branch and link) instruction, used to call a subroutine.
BX LR, used to return from subroutine [LR is (R14)]
Every subroutine needs “BX LR” as the last instruction for return
address.
AREA A_SIMPLE_PROGRAM,CODE,READONLY
ENTRY
EXPORT main
Main
………………… ;main loop
BL SUBR_n ;Call Subroutine n where n=1,2,3….
HERE B HERE ;stay here.
;end of main
;SUBROUTINE n
SUBR_n …………
………………………
BX LR ;return to main
;end of subroutine n
Subroutine -Example
Notes:
Can also used BX R14 or
MOV PC,LR
22 Example
Flowchart
Start
END
24 Program
Example of Looping & Branching
;Set R6 as a counter
;clear R7
;R7=R7+R6
;Decrement R6
END
Example of Loop & Branch
Write the program to add 0x99999999 together 10 times.
Example of Looping & Branching
Write a program to place value 0x55 into 100 bytes of
RAM locations.
Example : Nested Loop (loop within a loop)
Write a program to (a) load the R0 register with the value 0x55, and (b)complement it 20,000
times.
Rx is a 32-bit register, it can hold a maximum of 0xFFFFFFFF (2 – 1 decimal); therefore, the loop
can be repeated a maximum of 232 – 1 times. This example shows how to create a nesting
loop to go beyond 4 billion times. Because 20,000 is larger than 0xFFFFFFFF (the maximum
capacity of any R0–R12 registers), we use two registers to hold the count. The following code
shows how to use R2 and R1 as a register for counters in a nesting loop.
MOV R0,#0x55 ;R0 = 0x00000055
MOV R2,#100 ;load 100 into R2 (outer loop count)
L1 LDR R1,=200 ;R1 = 200 (inner loop count)
L2 EOR R0,R0,#0xFF ;complement R0 (R0 = R0 Ex-OR 0xFF)
SUBS R1,R1,#1 ;R1 = R1 – 1, dec. R1 (inner loop) This is a loop within a loop. The inner loop
BNE L2 ;repeat it until R1 = 0 runs 200 times but it is within an outer loop
SUBS R2,R2,#1 ;R2 = R2 – 1, dec. R2 (outer loop) that runs 100 times so the loop body runs 100 x
BNE L1 ;repeat it until R2 = 0 200 = 20,000 times.
HERE B HERE ;stay here LDR R1, =100;
END L1 LDR R2, =200;
L2 ;Loop body goes here
SUBS R2, #1;
BNE L2;
SUBS R1, #1;
BNE L1;
Microprocessor Assembly
Language Programming
Flow chart
General Programming Logic
Basic shapes:
Terminator.
Process.
Decision.
Input/Output.
Connectors.
Basic Shapes – Terminator
Examples:
A = 3?
Port is active?
FALSE
TRUE
TRUE FALSE
Basic Shapes – Input/Output
1 3
Example: Connector
START
1
Input A
OPERATION
Input B
FINISH
1
Programming Procedures
R1 MOV R1, #2
R2 MOV R2, #6
R3 MOV R3,R0
HERE B HERE
FINISH END
EXERCISE
1) Compare B and BX instructions.
2) Write a program to add 9 to R0 a thousand times and place the sum in R4 using
loop instruction.
3) Write a program to examine the contents of memory location 0x20000004. If it is
nonzero, AND it with 0x0F. If it is otherwise, OR the content with 0x55.
4) Modify the program in question 3) into subroutine program
5) Find the number of times the following loop is performed:
MOV R0,#0x55
MOV R2,#40
L1 LDR R1,=10000000
L2 EOR R0,R0,#0xFF
SUB R1,R1,#1
BNE L2
SUB R2,R2,#1
BNE L1
EXERCISE
6) Assuming a crystal frequency of 100 MHz, find the time delay associated with the
loop section of the following DELAY subroutine:
Machine Cycle
DELAY LDR R2,=50000000 1
HERE NOP 1
NOP 1
NOP 1
NOP 1
NOP 1
SUBS R2,R2,#1 1
BNE HERE 3
BX LR 1