introduction to programming - welcome to matc

39
Introduction to Programming Chapter 2 Microcontrollers Copyright 2005 Richard Lokken

Upload: others

Post on 18-Feb-2022

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Programming - Welcome to MATC

Introduction to Programming

Chapter 2Microcontrollers

Copyright 2005 Richard Lokken

Page 2: Introduction to Programming - Welcome to MATC

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

Page 3: Introduction to Programming - Welcome to MATC

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

Page 4: Introduction to Programming - Welcome to MATC

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

Page 5: Introduction to Programming - Welcome to MATC

Levels of Program Languages

Lowest Level Machine Code

Highest Level Highest Level Basic

Copyright 2005 Richard Lokken

Page 6: Introduction to Programming - Welcome to MATC

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

Page 7: Introduction to Programming - Welcome to MATC

Relative Level of Programming

Highest LowestHighestLevel

LowestLevel

BASIC MachineCode

AssemblerC

Copyright 2005 Richard Lokken

Page 8: Introduction to Programming - Welcome to MATC

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

Page 9: Introduction to Programming - Welcome to MATC

Advantage of High-Level

Source code is portable

Must be compiled

Copyright 2005 Richard Lokken

Page 10: Introduction to Programming - Welcome to MATC

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

Page 11: Introduction to Programming - Welcome to MATC

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

Page 12: Introduction to Programming - Welcome to MATC

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

Page 13: Introduction to Programming - Welcome to MATC

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

Page 14: Introduction to Programming - Welcome to MATC

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

Page 15: Introduction to Programming - Welcome to MATC

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

Page 16: Introduction to Programming - Welcome to MATC

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

Page 17: Introduction to Programming - Welcome to MATC

Prefix for Number Representationbinary %

octal @

decimal nothing or &

hexadecimal $

Copyright 2005 Richard Lokken

Page 18: Introduction to Programming - Welcome to MATC

Immediate ModeMachine Code Source Code

86 10 LDAA #$10

opcode immediate data

mnemonic

Copyright 2005 Richard Lokken

Page 19: Introduction to Programming - Welcome to MATC

Extend ModeMachine Code Source Code

B7 01 9B STAA $019B

extended modeeffective address

Copyright 2005 Richard Lokken

effective address

Page 20: Introduction to Programming - Welcome to MATC

Inherent ModeMachine Code Source Code

1B ABA

Copyright 2005 Richard Lokken

Page 21: Introduction to Programming - Welcome to MATC

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

Page 22: Introduction to Programming - Welcome to MATC

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

Page 23: Introduction to Programming - Welcome to MATC

HC11 Addressing Modes

Immediate (IMM) LDAA #$29$

# indicates IMM mode

Copyright 2005 Richard Lokken

Page 24: Introduction to Programming - Welcome to MATC

Extended (EXT)

Motorola’s name for Absolute Addressing

LDAA $000B LDAA $000B

Copyright 2005 Richard Lokken

Page 25: Introduction to Programming - Welcome to MATC

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

Page 26: Introduction to Programming - Welcome to MATC

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

Page 27: Introduction to Programming - Welcome to MATC

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

Page 28: Introduction to Programming - Welcome to MATC

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

Page 29: Introduction to Programming - Welcome to MATC

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

Page 30: Introduction to Programming - Welcome to MATC

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

Page 31: Introduction to Programming - Welcome to MATC

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.

Page 32: Introduction to Programming - Welcome to MATC

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

Page 33: Introduction to Programming - Welcome to MATC

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

Page 34: Introduction to Programming - Welcome to MATC

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

Page 35: Introduction to Programming - Welcome to MATC

LDAB $019B Fetch – 1 machine cycley Execute – 3 machine cycles

Copyright 2005 Richard Lokken

Page 36: Introduction to Programming - Welcome to MATC

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

Page 37: Introduction to Programming - Welcome to MATC

Program Flow

First document what the program is going to do.

Defects, undesirable behavior, BUGS

Copyright 2005 Richard Lokken

Page 38: Introduction to Programming - Welcome to MATC

Flowcharting

A means of organizing the flow of a program.

Copyright 2005 Richard Lokken

Page 39: Introduction to Programming - Welcome to MATC

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