The Structure of an Assembler
Program
Chapter 3 – PIC Notes
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
Aims and Outcomes
• Aims:
• To introduce the structure of an assembler program
• Outcomes:
• After this chapter you should be able to:
• Draw the block structure of an assembler program
• Identify and discuss the various components of the block structure
• Discuss and implement the four-column approach when writing an assembler program
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
Introduction
• Assembler is a low-level language making it difficult to read
• To increase readability a best practices model must be followed
• Look at these examples
Listing 3.1: Example of unstructured code (Spies, 2020, p20) Listing 3.2: Example of structured code (Spies, 2020, p22)
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
Assembler program
structure
• Each block represents a specific
section present in an assembler
program
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
Figure 3.1: Block structure of Assembler program (Spies, 2020, p22)
Comments
• Comments are important regardless
of the programming language
• It explains the logic behind a command
• It helps you remember WHY you wrote a
piece of code
• It helps other people understand why
you wrote a piece of code
• Comments are created by starting text
with a ; (semicolon)
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
Figure 3.1: Block structure of Assembler program (Spies, 2020, p22)
Comment Block
• ALL programs starts with a comment
block
• It contains:
• Who you are
• A short description of the purpose of the
code
• Which oscillator configuration was used and
the oscillator speed
• Any other configuration settings e.g. fuse
settings
• Investigate the example comment block
in the notes
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
Figure 3.1: Block structure of Assembler program (Spies, 2020, p22)
Example Comment Block
Listing 3.3: Example of Main Program comment block (Spies, 2020, p24)
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
Procedures
Comment Block
• Every procedure/sub-routine should
also start with a comment block
• It contains:
• The purpose of this procedure/sub-
routine
• Any registers that will be used to ensure
the correct values are pre-loaded if
needed
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
Figure 3.1: Block structure of Assembler program (Spies, 2020, p22)
Example Procedure Comment Block
Listing 3.4: Example of Procedure comment block (Spies, 2020, p24)
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
ORG / EQU / Include /
Define or other directives
• This block contains
• aliases of registers (Equates)
• ORG statements
• Possible Included files
• Possible DEFINE statements
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
Figure 3.1: Block structure of Assembler program (Spies, 2020, p22)
GOTO Initialize
• It is not a block, only 1 line of code
• Jumps over the interrupt vector
• Must be inserted else the Interrupt
Service Routine code will be executed
each time the PIC starts
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
Figure 3.1: Block structure of Assembler program (Spies, 2020, p22)
Interrupt Handler
• When using interrupts, this block
contains the code for the interrupts
• There is only 1 interrupt vector (04H)
therefore GOTO commands are used to
jump to specific service routines
depending on the interrupt that fired
• Flag bits in the INTCON register indicates
which interrupts fired
• Use RETFIE command at the end of the
ISR
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
Figure 3.1: Block structure of Assembler program (Spies, 2020, p22)
Procedures / Predefined
Processes
• Code for all subroutines/procedures
are placed in this block, e.g. delay
routines
• Remember to add the comment block
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
Figure 3.1: Block structure of Assembler program (Spies, 2020, p22)
Initialize
• Use this block to setup all the
necessary registers and ports
• Will only execute once every time the
PIC starts
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
Figure 3.1: Block structure of Assembler program (Spies, 2020, p22)
Main Program
• This is the main piece of code that will
be executed continuously without
ending
• Subroutines are called from here
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
Figure 3.1: Block structure of Assembler program (Spies, 2020, p22)
GOTO Main Program
• The main routine never stops
executing
• This is not really a block but only 1
line of code that has the main
program looping again
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
Figure 3.1: Block structure of Assembler program (Spies, 2020, p22)
END
• One line of code indicating to the
compiler where to stop compiling the
code
• Any code after this directive will not
be compiled and will be ignored as if
commented
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
Figure 3.1: Block structure of Assembler program (Spies, 2020, p22)
Labels
• Labels create a human readable link
to the start of certain sections of code
• Without labels direct memory
addressing must be employed to
move to certain sections of code
• Labels must be relevant to the section
of code it refers to
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
Figure 3.1: Block structure of Assembler program (Spies, 2020, p22)
Label Characteristics
• A label can not be a reserved word, assembler instruction or a compiler directive.
• Labels must start with a letter of the alphabet or with an underscore.
• The length of a label is compiler dependent but can normally be up to 32
characters.
• Labels MUST start in the first column, right next to the left hand margin, of the
assembler program.
• A label can consist of both upper and lower case characters and depending on
the compiler used, labels might be case sensitive.
• Labels must always end with a : (colon).
• Labels are not allowed to have spaces between words, use an underscore.
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
Label Examples
Listing 3.5: Examples of Labels (Spies, 2020, p27)
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
Assembler Program Columns
• Each program must be divided into 4 columns
• Each column has a tab width of 8 characters
• Columns are:
• Labels
• Used for labels
• Mnemonics / Assembler instructions
• Contains al the assembler commands
• Destination / Source
• Used for the parameters of the assembler commands
• Comments
• Add necessary comments
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
Program Example
• Identify the different blocks of
an assembler program
• Identify the program columns
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
Listing 3.6: Example of a complete Assembler program (Spies, 2020, p29)
Summary
• In this lesson we looked at the reason for using structure in an
assembler program
• Increase readability
• Discussed the different parts that an assembler program consist of
• Looked at the columns that should be used when writing an
assembler program
• Used an example program to identify the different parts and columns
of an assembler program
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
End of Chapter
Any questions?
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
References
• Spies, J., 2020. An Introduction To Microcontroller Concepts Using The
PIC Microcontroller.
Faculty of Information and Communication Technology
Department of Computer Systems Engineering
Figure 3.1: Block structure of Assembler program (Spies, 2020, p22)