Topic 7 Program Control Instruction (Ismail - FKEUTM 2020)
Topic 7 Program Control Instruction (Ismail - FKEUTM 2020)
Instruction
Ismail Ariffin
ECE,SKE,FE,UTM, Skudai, Johor
2020
Structured Programming
Software Structural Program
o Advanced Software Applies Structural Program
o Loop - repeat statement lines, infinite (non-stop) and finite (stop after
complete counting times)
o while (condition == true) do - repeat AVR statement lines when condition
true else quit
o If (condition == true) else - do statement line when found selection
condition is true
o Switch (case), case == true: do statement
o In Assembly, use jump and call instructions to implement the
structural program
o Jump to an instruction and continue running next sequent instruction.
o Call is a jump to a subprogram from main program and return to main
program from subprogram after finish running the subprogram to continue
Structured Programming
o Structured programming is aimed on improving the clarity, quality
and development time of a programming by making extensive use
of subroutines, block structure, for & while loop.
o There are five basics structured programming.
o If-then
o If-then-else
o While
o Do-while
o For
o In AVR, we can use BRcc to emulate the structured programming
techniques present in the C language.
Structured Programming
Structured Programming: If-Then
o Probably the simplest of structured programming.
...
...
CPI R16, 1 ;n denote by R16
if (n == 1) {
BRNE NotEQL
m = 3;
LDI R17, 3 ;m denote by R17
}
NotEQL: NOP
…
…
o The most efficient way to code this is to skip the code block {…} if
the condition is not true.
Structured Programming: If-Then-Else
o The if-then-else construct has an alternative statement that is
executed when the condition is false.
... …
if (n == 1){ CPI R16, 1 ;n denote by R16
m = 3; BRNE NotEQL
} else { LDI R17,3 ;m denote by R17
m = 2; JMP Done
} NotEQL: LDI R17, 2
… Done: …
…
... Top: CP R16, R17 ;m denote by R16
while (m >= n) { BRLT Exit ;n denote by R17
n++; INC R17
} JMP Top
… Exit: NOP
…
... ...
do { Top: INC R17 ;m denote by R16
n++; CP R16, R17 ;n denote by R17
} while (m >= n); BRGE Top
… …
o Notice that the code produced by the “do while” is shorter (and faster)
than the “while” loop. However, you don’t get something for nothing.
Often times you want to do loop times, really do the test at the
beginning of the loop.
Structured Programming: For
o Just to finish up all the looping structures present in C, we might as
well show the “for” loop, although it’s really nothing new:
...
CLR R17 ;m denote by R16
... LDI R16, 1 ;n denote by R17
n = 0; Top: CPI R16, 10
for (m = 1; m < 10; m++;){ BRGE Exit
n += m;
ADD R17, R16
} INC R16
… JMP Top
Exit: NOP
…
Jump Instruction
Jump Instructions
o AVR allows user to change direction of program flow, jump backward or jump forward.
o Two types jump instructions are applicable in AVR. In AVR, all jump instructions are performed by Program
Counter (PC). PC will take the jump address absolutely (real address) or relatively (address will be
computed).
o Unconditional Jump - When CPU executes an unconditional jump, it jumps unconditionally (with no
condition) to the lower or higher location.
- Three types in assembly list:
JMP address ; PC = absolute address
RJMP address ; PC = PC + relative
IJMP ; Use Z address pointer. PC = Z
- Operand applies Program Counter Addressing mode o Program Counter Relative.
o Conditional Branch - For CPU to execute a conditional jump, it checks on a flag condition (e.g. the
Carry, Zero, Overflow and Sign flag condition) , if the condition is true then it jumps to the target
location; otherwise, it continues executing the next immediate instruction
– AVR performs branch operation upon flag condition is true
– Flag condition is set by any of flag affecting/setting instruction before branch operation.
– i.e ADD , SUB, CP, SBRC, SBIC
– Syntax branch instruction:
BRxx address ; PC = PC + relative
– where xx = condition case for branch operation
– Operand applies Program Counter Relative Addressing mode
Jump Instruction (Must)
Unconditional Branch
Code
o There are three unconditional jump 1 LDI R16, 0
o Example:
MOV R1, R0 ;Copy r0 to r1
JMP farplc ;Unconditional jump
…
farplc: NOP ;Jump destination
002
C002 0002 RJMP LBL_NAME
opCode operand 0003 LDI R18, 4
0004 ADD R18, R17
0005
0005 LBL_NAME:
Machine code: 0005 ADD R16,R17
CFFE
FFE 0006 RJMP LBL_NAME
opCode operand 0007
o Example:
SUB R11, R12 ;Subtract register r11 to r12
BRGE greateq ;Branch if r11 ≥ r12 (signed)
…
greateq: NOP ;Branch destination
o Example:
SUB R11, R12 ;Subtract register r11 to r12
BRLT less ;Branch if r11 < r12 (signed)
…
less: NOP ;Branch destination
o Example:
SUB R11, R12 ;Subtract register r11 to r12
BREQ equal ;Branch if r11 = r12
…
equal: NOP ;Branch destination
o Example:
SUB R11, R12 ;Subtract register r11 to r12
BRNE noequal ;Branch if r11 ≠ r12
…
noequal: NOP ;Branch destination
o Example:
CP R4, R19 ;Compare r4 with r19
BRNE noteq ;Branch if r4 not equal r19
…
noteq: NOP ; Branch destination
Flag affected : H, S, V, N, Z, C
CPU cycle: 1
Example: CP - Compare
o Write a program that if R20 is equal to R21 then R22 is cleared.
No
if (R20 == R21)
Yes
Clear R22
• Solution:
CP R20,R21 ;Z will be set if R20 == R21
BRNE NEXT ;if Not Equal jump to next
CLR R22
NEXT:
Example: CP - Compare
o Write a program that if R26 is less than R24 then R22 is increases.
No
if (R26 < R24)
Yes
increment R22
• Solution:
CP R26,R24 ;C will be set if R26 < R24
BRCC L1 ;if Carry cleared jump to L1
INC R22
L1:
Example: CP - Compare
o Write a program that if R26 is greater and equal than R24 then R22 is
increases.
No
Ifif (R26
(R26>= R24)
< R24)
Yes
increment R22
• Solution:
CP R26,R24 ;C will be cleared if R26 >= R24
BRCS L2 ;if Carry set jump to L1
INC R22
L2:
CPC: Compare with Carry
o CPC instruction performs a compare between two registers Rd and
Rr and also takes into account the previous carry.
o None of the registers are changed.
o All conditional branches can be used after this instruction.
Flag affected : H, S, V, N, Z, C
CPU cycle: 1
CPC: Compare with Carry
o Example:
;Compare r3:r2 with r1:r0
CP R2, R0 ;Compare low byte
CPC R3, R1 ;Compare high byte
BRNE noteq ;Branch if not equal
…
noteq: NOP ; Branch destination
CPSE: Compare Skip if Equal
o CPSE instruction performs a compare between two registers Rd
and Rr and skips the next instruction if Rd = Rr.
o Example:
INC R4 ;Increment r4
CPSE R4,r0 ;Compare r4 to r0
NEG R4 ;Only executed if r4 not equal r0
NOP ;Continue
o Example:
CPI R19, 3 ;Compare r19 with 3
BRNE noteq ;Branch if r19 not equal 3
…
noteq: NOP ; Branch destination
Flag affected : H, S, V, N, Z, C
CPU cycle: 1
SBRC: Skip if Bit in Register is Cleared
o SBRC instruction tests a single bit in a register and skips the next
instruction if the bit is cleared.
o Example:
SUB R0, R1 ;Subtract r1 from r0
SBRC R0, 7 ;Skip if bit 7 in r0 is cleared
SUB R0, R1 ;Only executed if bit7 in r0 is not cleared
NOP ;Continue (do nothing)
o Example:
SUB R0, R1 ;Subtract r1 from r0
SBRS R0, 7 ;Skip if bit 7 in r0 is set
NEG R0 ;Only executed if bit7 in r0 is not set
NOP ;Continue (do nothing)
o Example:
SBIS PIND, 0 ;Skip if bit 0 in PIND is set
NEG R0 ;Only executed if bit7 in r0 is not set
NOP ;Continue (do nothing)
c) SUB R17,R16
BRLO NEXT
d) SUBI R16, 0x99
BRMI L1
Use of Conditional Branch
in Loop Program (Countless and
Countable)
Loop
(Do … while)
o Countable loop is repeating a sequence of instructions or an
operation in certain number of times.
o Do a loop.
o Example: Use the BRNE to make a loop.
BACK
LDI Rn,Count ;Set loop count
DEC Rn
YES
Z≠1
NO
o In the last two instructions, The Rn is decremented. If it is not zero, it jumps
back to the target address referred by the label
Example: Loop
• Example: Write an AVR assembly program to add 3 to R16 for
20 times.
R18=3
.ORG 00
LDI R18,3 ; R18 = 3 LOOP R17=20
LDI R17,20 ; set counter = 20
R16=R16+R18
LOOP: ADD R16,R18 ; add 3 to R16
DEC R17 ; decrement counter R17=R17-1
BRNE LOOP ; go to loop if not zero
YES
Z≠1
NO
Example: Looping
o Write a program that executes the instruction ADD R30, R31 nine
times.
• Solution: R16 = 9
.ORG 00
LDI R16,9 ;R16 = 9 ADD R30,R31
L1: ;REPEAT
ADD R30,R31 ;R30 = R30+R31 R16 = R16 - 1
No
END
Example: Looping
o Write a program that calculates the result of 9+8+7+..+1.
• Solution: R16 = 9
R17 = 0
.ORG 00
LDI R16, 9 ;R16 = 9
R17 = R17 + R16
LDI R17, 0 ;R17 = 0
L1: ADD R17,R16 ;R17 = R17 + R16
R16 = R16 - 1
DEC R16 ;R16 = R16 - 1
BRNE L1 ;if Z = 0
L2: RJMP L2 ;Wait here forever Yes
if (R16 > 0)
No
END
Example: Looping
o Write a program that calculates the result of 1+3+5+..+27.
o Solution: R20 = 0
R16 = 1
END
Looping: Example
o Write a program (a) to clear R20, then (b) add 3 to R20 ten times
and (c) send the SUM to PORTB. Use the zero flag and BRNE.
o Solution : Programming flow chart
Loop: Example
o Write an assembly program to copy the value $55 into
memory locations $140 through $144 using loop.
Looping: Example
Loop Inside a Loop (Nested Loop)
o The maximum number of repetition in one loop is 255 times which
is $FF.
o To repeat an action more than 255 times, we need to use a loop
inside a loop which is called a nested loop.
o In nested loop, we use two registers to hold the count.
o Example: Write a program to (a) load the PORTB register with
the value of 0x55, and (b) complement PORTB 700 times.
Nested Loop: Example
Nested Loop: Example
o Example: write a program to (a) load the PORTB register with the
value of 0x55, and (b) complement PORTB 700 times.
PROBLEM EXERCISE
Analyze the following program. Determine the value of registers and memory
stated in the question. The instructions are executed sequentially. The initial
values of some registers and memory contents are given below:
Initial Values:
R16 $81 $350 $52 $400 $52 X $40B
R17 $68 $351 $67 $401 $67 Y $350
R18 $43 $352 $72 $402 $52 Z $401
R19 $07 $353 $04 $403 $64
R20 $7F $354 $88 $404 $88
R21 $0A $355 $77 $405 $77
$356 $68 $406 $62
$357 $88 $407 $38
$358 $79 $408 $79
$359 $A4 $409 $34
$35A $86 $40A $86
The Program: Answer
AGAIN: LD R16,Y+
$401 $406 X
CP R17,R16
BRGE SKIP $402 $407 Y
ST -X,R16 $403 $408 Z
SKIP: ST Z+,R17 $404 $409
DEC R21
$405 $40A
BRNE AGAIN
NOP
Loop: Problem Exercise
o Write a program in Atmel assembly language that will
perform the transfer of data as shown in TABLE 1.
Use only instruction involving indirect addressing mode for
processing the data. Use indirect addressing mode WITH
LOOP for this problem.
TABLE 1