Introduction to Programming Chapter 2 Microcontrollers
Objectives Describe the difference between source code and machine code. Define opcode, operand, and address of an operand. Explain the purpose of memory addressing modes
Objectives Show how memory addressing modes work in the HC11 Explain how an instruction is fetched from memory and executed ecuted by the processor. Use flowcharts to show the flow/function of a program.
Programming the Computer Types of Computers Dedicated computers designed to run only one program computers in your car microcontroller General-purpose computers designed to run multiple programs desktop computer running word processor spreadsheet, etc.
Levels of Program Languages Lowest Level Machine Code Highest Level Basic
Source Code Written in a programming language English like words that communicate instructions to the computer. Levels of programming g languages g
Relative Level of Programming Highest Level Lowest Level BASIC C Assembler Machine Code
Programming Levels English if X is equal to Y, then set tz equal lto X, otherwise set Z equal to Y BASIC if (X==Y) then Z=X else Z=Y end if C if (x==y) Z=X; else Z=Y; Assembler LDAA X CMPA Y BEQ PAST LDAA Y PAST STAA Y MACHINE 96 00 91 01 27 02 96 01 97 02
Advantage of High-Level Source code is portable Must be compiled
Assembler Language Close to actual machine code Portability limited to processors within the same family. Advantage is tends to be smaller and run faster
Machine Code Only language that computer hardware understands Referred to as object code In binary Consists of operational codes operands (data) address of the operands
OPCODES Multi-bit code that identifies the instruction Contains information about the instruction and how to execute ecute the instruction Single instruction will be implemented with one or more opcodes
Operand Data that is required by the instruction addition instruction adds operands load instruction reads an operand from an address and loads it into a processor register. Operand field is a group of up to three bytes following the opcode.
Address Pointer into memory. Each memory address is an unique address by which it is identified. Address points to a byte of memory
Address Types Absolute A complete address or an address that contains all the address bits Offset commonly used to allow addressing of memory without specific reference to the entire address.
HC11 Machine Code Made up of OPCODES, operands, and addresses. Uses an 8-bit OPCODE and supports various ways of accessing operands in memory. 256 unique OPCODES can be generated. WHY?
Prefix for Number Representation binary % octal @ decimal nothing or & hexadecimal $
Immediate Mode Machine Code Source Code 86 10 LDAA #$10 opcode immediate data mnemonic
Extend Mode Machine Code Source Code B7 01 9B STAA $019B extended mode effective address
Inherent Mode Machine Code Source Code 1B ABA
Memory Addressing Modes Immediate operand immediately follows the opcode in memory Absolute used to access operand directly from memory Implied location of the date implied by the instruction
Memory Addressing Modes Indexed uses an address in an index register combined with an address offset Relative used to change the flow of the program
HC11 Addressing Modes Immediate (IMM) LDAA #$29 # indicates IMM mode
Extended (EXT) Motorola s name for Absolute Addressing LDAA $000B
Direct (DIR) A form of absolute addressing LDAA $0B 8-bit address becomes the low-order byte of the effective address.
Inherent (INN) Used when the operand is inherent or implied. The operands are already contained in the processor registers. ABA INX
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
Relative (REL) Used only for branch instructions. relative mode instructions do not process data, but control the flow of the epoga program.
Processing Instructions Machine cycles a short set of steps that are performed during a single clock cycle. HC11 performs a four step process during each machine cycle.
Four Step Process Phase One Controls steps one and two Deal with the program counter Phase Two Controls the last two steps Deals with memory access and processing
Fetch Cycle reads the instruction OPCODE from the memory. instruction OPCODE is always located in memory at the address that is contained in the program counter. first machine cycle for each instruction.
Machine cycles 1. Move contents of the PC to the MAR 2. Increment the PC. MAR to address bus 3. Enable memory OPCODE put on data bus 4. OPCODE to the IR where it is decoded.
Execute Cycle requires one or more machine cycles built-in in sequence of events that completes the operation of the instruction. instruction execution is unique for each instruction
Example LDAA #$A8 Fetch 1 machine cycle Execute 1 machine cycle 1. PC to MAR 2. Increment PC 3. Enable memory location, operand put on data bus 4. Operand loaded in AccA
LDAB $019B Fetch 1 machine cycle Execute 3 machine cycles
Example ABA Fetch 1 machine cycle Execute 1 machine cycle STAA $20 Fetch 1 machine cycle Execute 2 machine cycles
Program Flow First document what the program is going to do. Defects, undesirable behavior, BUGS
Flowcharting A means of organizing the flow of a program.
Symbols Terminator used to show start or end of program subroutine used to show a subroutine process Process used to show a process shows flowchart continues on same page input/output used to show any data input/output operations shows flowchart continues to another page direction of flow decision used to show process decisions i