Introduction to Programming
Chapter 2Microcontrollers
Copyright 2005 Richard Lokken
Objectives
Describe the difference between source code and machine code.
Define opcode, operand, and address of an operand.add ess o a ope a d
Explain the purpose of memory addressing modesaddressing modes
Copyright 2005 Richard Lokken
Objectives
Show how memory addressing modes work in the HC11
Explain how an instruction is fetched from memory and executed by the o e o y a d e ecuted by t eprocessor.
Use flowcharts to show the Use flowcharts to show the flow/function of a program.
Copyright 2005 Richard Lokken
Programming the Computer
Types of Computers Dedicated computersp
• designed to run only one program • computers in your car – microcontroller
General-purpose computers• designed to run multiple programs
• desktop computer running• desktop computer running• word processor• spreadsheet, etc.
Copyright 2005 Richard Lokken
Levels of Program Languages
Lowest Level Machine Code
Highest Level Highest Level Basic
Copyright 2005 Richard Lokken
Source Code
Written in a programming language English like words that communicate g
instructions to the computer.
Levels of programming languagesp g g g g
Copyright 2005 Richard Lokken
Relative Level of Programming
Highest LowestHighestLevel
LowestLevel
BASIC MachineCode
AssemblerC
Copyright 2005 Richard Lokken
Programming Levels
English
if X is equal to Y, thent Z l t X
BASICif (X==Y) then Z=X
Cif (x==Y)
set Z equal to X,otherwise set Z equalto Y
elseZ=Yend if
( )Z=X;elseZ=Y;
AssemblerLDAA XCMPA Y
MACHINE96 0091 01CMPA Y
BEQ PASTLDAA Y
PAST STAA Y
91 0127 0296 0197 02
Copyright 2005 Richard Lokken
Advantage of High-Level
Source code is portable
Must be compiled
Copyright 2005 Richard Lokken
Assembler Language
Close to actual machine code
Portability limited to processors within the same familywithin the same family.
Ad i d b ll Advantage is tends to be smaller and run faster
Copyright 2005 Richard Lokken
Machine Code
Only language that computer hardware understands
Referred to as object code In binary Consists of
operational codesp operands (data) address of the operands
Copyright 2005 Richard Lokken
OPCODES
Multi-bit code that identifies the instruction
Contains information about the instruction and how to execute the st uct o a d o to e ecute t einstruction
Single instruction will be Single instruction will be implemented with one or more opcodes
Copyright 2005 Richard Lokken
opcodes
Operand
Data that is required by the instruction addition instruction adds operands load instruction reads an operand from p
an address and loads it into a processor register.
Operand field is a group of up to three bytes following the opcode.
Copyright 2005 Richard Lokken
g
Address
Pointer into memory.
Each memory address is an unique address by which it is identifiedaddress by which it is identified.
Add i b f Address points to a byte of memory
Copyright 2005 Richard Lokken
Address Types
Absolute A complete address or an address that p
contains all the address bits
Offset commonly used to allow addressing of commonly used to allow addressing of
memory without specific reference to the entire address.
Copyright 2005 Richard Lokken
HC11 Machine Code
Made up of OPCODES, operands, and addresses.
Uses an 8-bit OPCODE and supports various ways of accessing operands i in memory.
256 unique OPCODES can be t dgenerated.
WHY?
Copyright 2005 Richard Lokken
Prefix for Number Representationbinary %
octal @
decimal nothing or &
hexadecimal $
Copyright 2005 Richard Lokken
Immediate ModeMachine Code Source Code
86 10 LDAA #$10
opcode immediate data
mnemonic
Copyright 2005 Richard Lokken
Extend ModeMachine Code Source Code
B7 01 9B STAA $019B
extended modeeffective address
Copyright 2005 Richard Lokken
effective address
Inherent ModeMachine Code Source Code
1B ABA
Copyright 2005 Richard Lokken
Memory Addressing Modes Immediate
operand immediately follows the opcode in memorymemory
Absolute used to access operand directly from memory
Implied location of the date implied by the instruction
Copyright 2005 Richard Lokken
Memory Addressing Modes
Indexed uses an address in an index register g
combined with an address offset
Relative used to change the flow of the program used to change the flow of the program
Copyright 2005 Richard Lokken
HC11 Addressing Modes
Immediate (IMM) LDAA #$29$
# indicates IMM mode
Copyright 2005 Richard Lokken
Extended (EXT)
Motorola’s name for Absolute Addressing
LDAA $000B LDAA $000B
Copyright 2005 Richard Lokken
Direct (DIR)
A form of absolute addressing
LDAA $0B
8-bit address becomes the low-order b f h ff i ddbyte of the effective address.
Copyright 2005 Richard Lokken
Inherent (INN)
Used when the operand is inherent or implied. The operands are already contained in the processor registers.
ABA ABA INX
Copyright 2005 Richard Lokken
Indexed (INDX, INDY)
used when the operand may be at varying addresses with the 64k
memory map.
LDAA $02,X $02 is an offset X is used for the effective address
calculation
Copyright 2005 Richard Lokken
Relative (REL)
Used only for branch instructions. relative mode instructions do not relative mode instructions do not
process data, but control the flow of the program.t e p og a
Copyright 2005 Richard Lokken
Processing Instructions
Machine cycles a short set of steps that are performed p p
during a single clock cycle. HC11 performs a four step process
during each machine cycle.
Copyright 2005 Richard Lokken
Four Step Process
Phase One Controls steps one and twop Deal with the program counter
Phase Two Phase Two Controls the last two steps Deals with memory access and Deals with memory access and
processing
Copyright 2005 Richard Lokken
Fetch Cycle
reads the instruction OPCODE from the memory.
instruction OPCODE is always located in memory at the address ocated e o y at t e add essthat is contained in the program counter.ou
first machine cycle for each instruction
Copyright 2005 Richard Lokken
instruction.
Machine cycles
1. Move contents of the PC to the MAR2. Increment the PC. MAR to address 2. Increment the PC. MAR to address
bus3 Enable memory OPCODE put on 3. Enable memory OPCODE put on
data bus4 OPCODE to the IR where it is 4. OPCODE to the IR where it is
decoded.
Copyright 2005 Richard Lokken
Execute Cycle
requires one or more machine cycles built-in sequence of events that built in sequence of events that
completes the operation of the instruction.st uct o
instruction execution is unique for each instructioneach instruction
Copyright 2005 Richard Lokken
Example
LDAA #$A8 Fetch – 1 machine cycley Execute – 1 machine cycle
1 PC to MAR1. PC to MAR2. Increment PC
E bl l ti d 3. Enable memory location, operand put on data bus
Copyright 2005 Richard Lokken
4. Operand loaded in AccA
LDAB $019B Fetch – 1 machine cycley Execute – 3 machine cycles
Copyright 2005 Richard Lokken
Example
ABA Fetch – 1 machine cycley Execute – 1 machine cycle
STAA $20Fetch 1 machine cycle Fetch – 1 machine cycle
Execute – 2 machine cycles
Copyright 2005 Richard Lokken
Program Flow
First document what the program is going to do.
Defects, undesirable behavior, BUGS
Copyright 2005 Richard Lokken
Flowcharting
A means of organizing the flow of a program.
Copyright 2005 Richard Lokken
SymbolsTerminator used to show start used to show a subroutineTerminator used o s o s a
or end of program
Pused to show a
subroutine process
Process processshows flowchart continues on samepage
input/output used to show anydata input/outputoperations
shows flowchart continues toanother page
used to show processd i i
direction of flow
Copyright 2005 Richard Lokken
decision decisions