08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
1
System & Program
System & Program
Developments of 8051
Developments of 8051
Program Structure and Design
Introduction
Advantages and Disadvantages of Structured
Programming
The Three Structures: statements, loops, choice
Pseudo Code Syntax
Assembly Language Programming
Tools & Techniques for Program
Development
The Development Cycle
Integration and Verification
Command and Environments
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
2
Introduction
Structured Programming:
Organizing and coding programs that reduces
complexity, improves clarity, and facilitates
debugging and modifying
All programs may be written using only three
structures: statements, loops, and choice—too
good to be true.
Introduce Structure programming to
assembly language programming
Flowcharts
Pseudo code
Assembly language
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
3
Flowcharts
Decision block
Process box
Input/Output block
Program terminator
Off-page connector
Predefined process
(subroutine)
Program flow arrow
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
4
Pseudo Code
Strict adherence to structure in
combination with informal language
[get a character from the keyboard]
IF [condition is true]
THEN [do statement 1]
ELSE BEGIN
[do statement 2]
[do statement 3]
END
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
5
Advantages & Disadvantages of
Structured Programming
Advantages:
Simple to trace, debug
Finite number of structures
Structures as building block
The set of structure is complete
Structures are self-documenting, easy
to read
Structures are easy to describe in
flowcharts, syntax diagrams, pseudo
code,
Increased program productivity
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
6
Advantages & Disadvantages of
Structured Programming
Disadvantages:
Only a few high-level languages (Pascal,
C, PL/M) accept the structures directly;
others require extra translation stage
Structured program may execute slower
and require more memory
Some problems (a minority) are more
difficult to solve using only the three
structures
Nested structures can be difficult to
follow
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
7
The Three Structures
Statements
[count = 0]
PRINT_STRING(“Select Option:”)
Loops (iteration)
WHILE/DO
REPEAT/UNTIL
Choice
IF/THEN/ELSE
CASE
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
8
The WHILE/DO Statement
WHILE [condition] DO
[statement]
(statement might not be performed at all!)
Enter
Condition
True?
Statement
Exit
No
Yes
WHILE [c == 1] DO
[statement]
ENTER: JNC EXIT
STATEMENT: (statement)
JMP ENTER
EXIT: (continue)
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
9
WHILE/DO: SUM Subroutine
[sum (A) = 0]
WHILE [length (R7) > 0] DO BEGIN
[sum = sum + @pointer (R0)]
[increment pointer]
[decrement length]
END
[sum (A) = 0]
WHILE [length (R7) > 0] DO BEGIN
[sum = sum + @pointer (R0)]
[increment pointer]
[decrement length]
END
8051 code (closely structured; 13 bytes)
SUM: CLR A
LOOP: CJNZR7,#0,STAM
JMP EXIT
STAM: ADD A,@R0
INC R0
DEC R7
JMP LOOP:
EXIT: RET
(loosely structured; 9 bytes)
SUM: CLR A
INC R7
MORE: DJNZ R7,SKIP
RET
SKIP: ADD A,@R0
INC R0
SJMP MORE
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
10
WHILE/DO
Pseudo code:
WHILE [ACC != CR AND R7 != 0] DO
[statement]
8051 code:
ENTER: CJNE A,#0DH,SKIP
JMP EXIT
SKIP: CJNE R7,#0,STAM
JMP EXIT
STAM: (one or more statements)
.
.
JMP ENTER
EXIT: (continue)
Enter
ACC !=
<CR>?
R7 != 0?
Statement
Exit
No
No
Yes
Yes
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
11
The REPEAT/UNTIL Statement
REPEAT [statement]
UNTIL [condition]
(statement performed at least once)
Enter
Condition
True?
Statement
Exit
No
Yes
REPEAT [statement]
UNTIL [c == 1]
ENTER: (statement)
JNC ENTER
EXIT: (continue)
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
12
The REPEAT/UNTIL Statement
REPEAT [ACC = @pointer]
[increment pointer]
UNTIL [ACC == ‘Z’ or ACC == 0]
Enter
Char =
“Z”?
Get a char
Exit
No
Yes
8051 code:
STAM: MOV A,@R0
INC R0
JZ EXIT
CJNE A,#’Z’,STAM
EXIT: RET
Inc pointer
Char =
0?
Yes
No
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
13
The IF/THEN/ELSE Statement
IF [condition]
THEN [statement 1]
ELSE [statement 2]
Enter
condition
true?
Statement 1
Exit
Statement 2
YesNo
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
14
The IF/THEN/ELSE Statement
[input character]
IF [input character == graphic]
THEN [echo character]
ELSE [echo ‘.’]
Enter
Graphic
char?
Echo char
Exit
8051 code:
(closely structured; 14 bytes)
ENTER: ACALL INCH
ACALL ISGRPH
JNC STMENT2
STMENT1: ACALL OUTCH
JMP EXIT
STMENT2: MOV A,#’.’
ACALL OUTCH
EXIT: (continue)
Echo “.”
YesNo
Input character
(loosely structured; 10 bytes)
ACALL INCH
ACALL ISGRPH
JC SKIP
MOV A,#’.’
SKIP: ACALL OUTCH
(continue)
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
15
The CASE Statement
CASE [expression] OF
0: [statement 0]
1: [statement 1]
2: [statement 2]
.
.
N: [statement 0]
[default]
END_CASE
Enter
Exit
No
Yes
expression
0?
Statement 0
expression
1?
Statement 1
expression
2?
Statement 2
expression
n?
Statement n default
No No No
Yes Yes Yes
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
16
The CASE Statement
[input a character]
CASE [character] OF
‘0’: [statement 0]
‘1’: [statement 1]
‘2’: [statement 2]
‘3’: [statement 3]
END_CASE
Enter
Exit
No
Yes
char
== “0”?
Action 0
char
== “1”?
Action 1
char
== “2”?
Action 2
char
== “3”?
Action 3
No No No
Yes Yes Yes
Input
character
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
17
The CASE Statement
8051 code:
(closely structured)
ACALL INCH
CJNE A,#’0’,SKIP1
ACT0: .
.
JMP EXIT
SKIP1: CJNE A,#’1’,SKIP2
ACT1: .
.
JMP EXIT
SKIP2: CJNE A,#’2’,SKIP3
ACT2: .
.
JMP EXIT
SKIP3: CJNE A,#’3’,EXIT
ACT3: .
.
EXIT: (continue)
(loosely structured)
ACALL INCH
ANL A,#3 ;reduce to 3 bits
RL A
MOV DPTR,#TABLE
JMP @A+DPTR
TABLE: AJMP ACT0
AJMP ACT1
AJMP ACT2
ACT3: .
.
JMP EXIT
ACT0: .
.
JMP EXIT
ACT1: .
.
JMP EXIT
ACT2: .
.
EXIT: (continue)
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
18
The GOTO Statement
GOTO statement can always be avoided by
using the structures. Sometimes GOTO
statement provides an easy method of
terminating a structure when errors occur.
GOTO statements usually becomes
unconditional jump in assembly
implementation. Extreme caution is needed.
Never exit a subroutine using GOTO instead
of normal return for the return address will
be left on the stack and eventually stack
overflow will occur.
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
19
Pseudo Code Syntax
Tips of using pseudo code:
Use descriptive language for statements
Avoid machine dependency in statements
Enclose conditions & statements in brackets: []
Begin all subroutines with their names followed
by a set of parameters: ()
End all subroutine with RETURN ()
Use lower text except reserved words &
subroutine names
Indent all statements from the structure entry
points and exit points
Use the commercial at sign (@) for indirect
addressing
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
20
Suggested Pseudo Code Syntax
Reserved words:
BEGIN END
REPEAT UNTIL
WHILE DO
IF THEN ELSE
CASE OF
RETURN
Arithmetic operators:
+ addition
- subtraction
* multiplication
/ division
% modulus (remainder after division)
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
21
Suggested Pseudo Code Syntax
Relational operators:
== true if values equal to each other
!= true if values not equal to each other
< true if first value less than second
<= true if first value <= second
> true if first value > second
>= true if first value >= second
&& true if both values are true
|| true if either value is true
Bitwise Logical operators:
& logical AND
| logical OR
^ logical XOR
~ logical NOT (one’s complement)
>> logical shift right
<< logical shift left
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
22
Suggested Pseudo Code Syntax
Assignment operators:
= set equal to
op = assign operator shorthand
where “op” is one of
+ - * / % << >> & ^ |
Precedence operators:
( )
Indirect addressing:
@
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
23
Suggested Pseudo Code Syntax
Operator Precedence:
( )
~ @
* / %
+ -
<< >>
< <= > >=
== !=
&
^
|
&&
||
= += -= *= /= etc
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
24
Suggested Pseudo Code Syntax
Structures:
Statement:
[do something]
Statement block:
BEGIN
[statement]
[statement]
END
WHILE/DO:
WHILE [condition] DO
[statement]
08/09/14 T. L. Jong, Dept. of E.E.
, NTHU
25
Suggested Pseudo Code Syntax
REPEAT/UNTIL:
REPEAT
[statement]
UNTIL [condition]
IF/THEN/ELSE:
IF [condition]
THEN [statement 1]
(ELSE [statement 2])
CASE/OF:
CASE [expression] OF
1: [statement 1]
2: [statement 2]
3: [statement 3]
.
.
n: [statement n]
[default]
END