Embedded Systems Engineering: R.A. Prabhath Buddhika

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 64

Embedded Systems

Engineering

R.A. Prabhath Buddhika
B.Sc.Eng(Hons)(Moratuwa)
Department of Electronic & Computer Engineering
Introduction

Lecture 01

Based on original slides by Dr. Malitha Wijesundara

-- Embedded Software Design -- 2


Objectives

1. Compare different hardware platforms for
embedded systems development.
2. Examine the viability of different programming
languages for a given embedded system design.
3. Justify the design techniques for a certain embedded
system implementation.
4. Decide most suitable technologies for various
requirements of an embedded system.

-- Embedded Software Design -- 3


Objectives

5. Study the existing embedded systems and interpret
the design principles been applied.
6. Infer new ideas to enhance the performance and/or
the applicability of existing embedded systems.
7. Improve the performance of a particular
segment/part of an existing embedded system.
8. Construct comprehensive embedded system
solutions for a given requirement.

-- Embedded Software Design -- 4


Topics to be covered

1. HCS12 architecture
2. Interrupt handling
1. C programming
3. State machine based embedded software design
4. Fixed point arithmetic
5. Pulse width modulation
6. Timers/capture and compare modules
7. Analog to digital conversion
8. Digital to analog conversion
9. PID controller design
10. Controller area networks
11. Power management in embedded Systems
12. Noise considerations
-- Embedded Software Design -- 5
Assessment criteria

 Mid term test20%
 Lab classes 30%
 Final examination 50%

 Mid term test


 One hour
 02 compulsory Structured essay type questions
 Final examination
 Three hours
 06 compulsory Essay type questions
-- Embedded Software Design -- 6
Lab classes

 Embedded systems laboratory
 4th floor – 413
 Two major groups
 Each group is divided in to 8 subgroups
 Three students maximum per subgroup (two
preferred)

-- Embedded Software Design -- 7


Computer Hardware
Computer

- a combination of hardware and software that performs
pre-defined tasks depending on input.

Computer HW - circuitry that:


- executes program instructions
- handles input & output devices
- has memory to store program & data

CPU - “Central Processing Unit” contains:

Registers –
Storage locations within the CPU. Faster than memory, used
to hold data and/or memory addresses during
execution of an instruction. “Accumulates” results of ALU

Arithmetic Logic Unit – (ALU)


Circuitry that performs numerical calculations and logic functions.

Sequence Controller –
State Machine that decodes program instructions and controls
the execution of the instructions. Also allocates HW resources
within the CPU

-- Embedded Software Design -- 8


Computer Hardware
 Address
Memory – storage of digital information
Data
- memory can be “read” or “written” to.
- the address bus tells the location of the data read/write
- the R/W control signals tell the memory to store or output data
Write_L
Read_L

ROM - “Read Only Memory”, Non-volatile, keeps data when power is removed.
Good for program instructions
ex) disk drives, CD’s, Flash

RAM - “Random Access Memory”, Volatile, looses data when power is removed.
Faster than ROM, can withstand more access cycles during life-cycle, cheaper
ex) DRAM or CACHE in your PC

-- Embedded Software Design -- 9


Computer Hardware

Input / Output

- all computer need input and output to make their function useful to the real world
- the input into the computer will change how the program is executed

ex)
Human I/O : Keyboard, Mouse, Monitor, Speakers

Electronic I/O : A/D, DAC, control signals from other electronics, sensors

-- Embedded Software Design -- 10


Computer Hardware
Typical System

Non Volatile Memory
CPU (program memory, ROM, Disk, CD)
- ALU
- Control Unit Volatile Memory
- Registers (data memory, RAM)

I/O
Terminology (Input/Output, Keyboard, Display)

microprocessor - CPU implemented using Integrated Circuit Fabrication technology (uP)


microcomputer - a computer that uses a uP as its CPU
microcontroller - I/O and memory are integrated onto same IC as CPU
8-bit processor - # of bits the ALU can manipulate during operation
Embedded System - special purpose system where computer is contained within the system
it controls

-- Embedded Software Design -- 11


Computer Software

Instruction Set - Pre-defined tasks that a CPU can perform, specific to CPU
design, controller known tasks

Program - A set of instructions that the computer HW executes.


Stored in memory as binary #’s.

Machine Code - The binary #’s representing the program (ex, A+B = 0001 1011)

Assembly Language -It is very difficult to program in Machine Code. Assembly


Programming simplifies this by giving each instruction
a mnemonic representation

(ex, $1B = Machine Code, ABA = Assembly Instruction)


Source Code - The file of Assembly mnemonics that the design engineer creates
-- Embedded Software Design -- 12
Computer Software

Assembler -A program that translates the “Source Code” into Machine Instructions
The output of the assembler is “Object Code”

Absolute Assembler - source code dictates the exact location of programs, all code

must be in one file difficult for large programs where multiple


engineers are working on design
Relocatable Assembler - takes in multiple source files, keeps track of inter-dependencies,
each source
file can be assembled separately producing independent object codes
Linker - takes in multiple object files and combines them into an efficient

memory configuration.

-- Embedded Software Design -- 13


Computer Software

High Level Language - Makes programming more general, easier to read, can handle larger programs
(C/C++)
written at functional level (IF, THEN, WHILE)

Compiler - Program that translates high-level source code into Machine Instructions

-- Embedded Software Design -- 14


Computer Software

Development Overview so far
1. Design Task Defined
2. Program is partitioned into sub-designs
3. Sub-designs are coded in Assembly or C for a given
processor instruction set
4. Code is assembled (or compiled) using re-locatable
assembler
5. Linker takes object files from assembler and creates
downloadable machine code
6. Program is downloaded into system and run

-- Embedded Software Design -- 15


Adapt9S12E128

-- Embedded Software Design -- 16


16-bit Microcontroller Roadmap

-- Embedded Software Design -- 17


M68HCS12 Overview

 HCS12 Microcontroller - what we will be using
- learn basic principles of a microprocessor and
microcontroller
- principles are applicable to all microprocessors
 16-bit microcontroller - can operate on 16-bits of data at a time

ROM
CPU
- ALU
- Control Unit RAM
- Registers

I/O
(Ports, IRQ’s, Timers, A/D)

-- Embedded Software Design -- 18


Freescale 68HCS12 CPU

CPU Details
Accumulators - 2x, 8-bit registers, most arithmetic is performed on these registers. The HCS12
has
8-bit registers called A and B. The A and B registers can be used together to form
a 16-bit register called D. ACCD[15:0] = {ACCA[7:0], ACCB[7:0]}.
These registers are the source or destination for most operations within the CPU.
Called Accumulators because they “accumulate” the results of ALU operations.

Index Registers - 16-bit registers used mainly for addressing operations. The HCS12 has two 16-bit
index registers called X and Y.

Stack Pointer - 16-bit register that points to the STACK. The STACK is a 1 st in, last out structure
(FILO).
The Stack Pointer (SP) points to the address above the TOP element in the Stack.

Program Counter - 16-bit register that holds the location of the next instruction to be executed.

-- Embedded Software Design -- 19


Freescale 68HCS12 CPU

Condition Code Register - 8-bit register that tracks the status of operations, branches, and
interrupts.

CCR[7:0] = {S,X,H,I,N,Z,V,C} S = Stop Disable


X = X interrupt mask
H = Half Carry
I = ‘I’ interrupt mask
N = Negative Result
Z = Zero Result
V = Overflow 7 AC C U M U LATO R A 0 7 AC C U M U LATO R B 0
C = Carry
15 D O U BLE AC C U M U LATO R D 0

15 IN D E X R E G IS T E R X 0
Programmer’s CPU Model
15 IN D E X R E G IS T E R Y 0

15 S T A C K P O IN T E R 0

15 PROGRAM COUNTER 0
C O N D IT IO N C O D E
S X H I N Z V C
R E G IS T E R

-- Embedded Software Design -- 20


Freescale 68HCS12
Memory

Memory Addressing – the HCS12 has an 8-bit storage capability. This means that each location in
memory
can hold an 8-bit byte. The HCS12 uses 16-bits for addressing.

The CPU reads/writes to memory by:

1) Provide an Address to the memory via the “Address Bus”


2) Data is then transmitted between the CPU and the
memory via the “Data Bus”
Address[15:0]
CPU Memory
Data[7:0]
READ_L
WRITE_L

-- Embedded Software Design -- 21


Freescale 68HCS12
Memory

Memory Map - A graphical way to illustrate memory. We will use this continually

$0000
$0001
:
$FFFE
$FFFF

8-bits

# of Address Lines - 2(# of address lines) = the number of locations we can access with that many lines.
- The HCS12 has 16 address lines : 2 (16)=65,536 addressable memory locations
- 16 address lines can be represented with 4 HEX digits
- The HCS12 has an address range from $0000 to $FFFF (standard notation)
- Each memory location holds 8-bits of data (1 byte), or $00 to $FF (standard notation)

Kilobyte - K = 210 = 1024


Megabyte - M = K2 = 1,048,576

ex) when we say we can access a “64k” byte memory, actually (64)(2 10) = 65,536 bytes

-- Embedded Software Design -- 22


Freescale 68HCS12
Memory

Registers - “I/O Registers” are mapped into memory space and treated
as a normal memory address by the programmer.

- CPU registers are used to perform general purpose operations


(arithmetic, logic). They do not occupy memory space but
are accessible by the programmer.

- Everything accessible by a program is given an address


(RAM, ROM, I/O, Configuration Settings)

-- Embedded Software Design -- 23


Freescale 68HCS12
Memory map for M68HC9S12E128 microcontroller

Memory
- responsibility of the programmer to “know the map”

Freescale
Semiconductor, Inc.

MC9S12E-Family
Device User Guide

-- Embedded Software Design -- 24


HCS12 Configuration Options

Memory Configurations for MC9S12E Processor Family
Freescale
Semiconductor, Inc.

MC9S12E-Family
Device User Guide

NOTE: Part numbers getting confusing? Try to remember what a microcontroller is. There
is a CPU (with a part number), Memory Configuration (with a part number), and I/O
Configuration (with a part number). This creates a matrix of part numbers that an
engineer can choose from.

-- Embedded Software Design -- 25


HCS12 Block Diagram

-- Embedded Software Design -- 26


Instruction Execution

Instruction - a specific task that the CPU can perform
- consists of an “Opcode” and 0 or more “Operands”

Opcode - Machine code for a specific operation

Operand - The “Data” or “Address” used by the operation

Memory Notation - “M”= contents of memory Memory


() = address, we'll with and without the "M"
Program $0000 $ff
Ex) M($0000) = $ff Counter
M($0002) = $55 $0001 $12
$0002 $55

-- Embedded Software Design -- 27


Instruction Execution

Memory

Program $e000 $86


How an instruction is executed Counter
$e001 $AB
- a 2-byte instruction resides in memory at $e000 and $e001 $e002 $96
- M($e000) is the opcode for the instruction
- M($e001) is the operand for the instruction
- PC always starts at the beginning of the instruction,
- the beginning of the instruction is always a unique Opcode

1) PC points at beginning of instruction


2) CPU reads data at (PC)=$e000, which it knows is an Opcode, decodes, increments PC
3) From decode, CPU knows what instruction it is, also knows this instruction has an operand
4) CPU reads operand at (PC)=$e001, which it knows is the operand, increments PC
5) CPU performs remaining tasks associated with the instruction (ADD, Store, etc…)

NOTE: the PC now points at $e002, which is the beginning of the next instruction
the beginning of the next instruction is an "Opcode"

-- Embedded Software Design -- 28


The LOAD Operation

LOAD - an operation that moves information into
the internal registers of the CPU (A,B,D,X,Y,SP).

Where the “information” comes from , depends on the


addressing mode.

The HCS12 has 7 addressing modes. We will use the


LOAD instruction to illustrate these:

1) Immediate
2) Direct
3) Extended
4) Indexed
5) Indexed-indirect
6) Inherent
7) Relative

-- Embedded Software Design -- 29


Addressing Modes
(IMM)

Immediate Addressing - the Operand of the instruction is the “Actual Data”
(IMM) to be loaded into a register. The “#” sign indicates to
the cross-assembler that “Immediate Addressing” is
intended.

Ex) LDAA #$FF ; this instruction will put %11111111 into accumulator A

Memory
Opcode Operand
$e000 $86
Machine
Code: $86 $FF $e001 $FF How it looks
- in memory

Unique Code to CPU, the Control Unit in the CPU


decodes $86 and knows what to do
(i.e., grab $FF and put in ACCA)

-- Embedded Software Design -- 30


Addressing Modes
(DIR)

Direct Addressing – the Operand of the instruction is the “Address”
(DIR) of where the data is located in memory. Direct uses
only 1 Byte in memory to indicate the address. This
means that the data must reside between $00 - $FF.

Ex) LDAA $00 ; this instruction will put the contents of memory
; location $00 into ACCA. (ACCA = M($00)) Memory

Opcode Operand $0000 $12


- ACCA = $12
Machine
Code: $96 $00 - after this
instruction is
$e000 $96 executed
The Control Unit knows that $e001 $00
$96 means Direct Addressing

-- Embedded Software Design -- 31


Addressing Modes
(EXT)

Extended Addressing – this is the same as Direct except that 2 Bytes (16 bits) are used
(EXT) for the “address” of where the data is to be loaded from.
With two bytes of memory, the data can be located
from locations $0000 to $FFFF in memory.

Ex) LDAA $1234; this instruction will put the contents of memory
; location $1234 into ACCA. (ACCA = M($1234))
Memory
Opcode Operand
$1234 $34
Machine ACCA = $34
Code: $B6 $12, $34 $e000 $b6 after this
instruction is
$e001 $12 executed
The Control Unit knows that $B6 means $e002 $34
Extended Addressing. This instruction uses 3 bytes

-- Embedded Software Design -- 32


Addressing Modes
(EXT)

Extended vs. Direct Addressing –

EXT and DIR work the same way, why ever use DIR?

- DIR only takes 2 bytes of memory. EXT takes 3 bytes


- DIR executes faster, only 2 bytes of information to get from memory
- Most I/O registers are mapped between $00 - $FF

-- Embedded Software Design -- 33


Addressing Modes
(INDX,INDY)
Indexed Addressing – The effective address is calculated using the contents of an "Index"
(INDX, INDY) register and an "Offset". The index can be X,Y,SP, or PC. (mostly X,Y)

The offset can be A,B,D or a constant.


The offset can be a 5-bit, 9-bit, or 16-bit signed number (more later).

The Index register needs to be initialized.

Ex) LDX #$0800 ; initializes the X register to $0800 using Immediate Addressing
LDAA 1,X ; loads A with the contents of X, plus an offset of ‘1’
Memory
Opcode Operand
$0801 $56
- ACCA = $56
Machine
Code: $A6 $01 $e000 $A6 after this
instruction is
The Effective address is $0801. $e001 $01 executed
ACCA is loaded with ($0801).
$e002 -
-- Embedded Software Design -- 34
Addressing Modes
Example –

What is in the CPU registers after the following code segment is executed?
What addressing mode is used?
What are the effective addresses for the LOADS? Memory

$0800 $AB
Addressing Effective
Mode Address $0801 $CD
LDX #$0800 IMM n/a
$0802 $EF
LDY #$0801 IMM n/a
LDAA 1,X INDX $0801
LDAB 1,Y INDY $0802
-

After execution: ACCA = $CD


$e000 -
ACCB = $EF $e001 -
X = $0800
Y = $0801 $e002 -

-- Embedded Software Design -- 35


Addressing Modes
(Indexed)

Indexed Addressing –

We learned Indexed addressing uses an Index Register and an Offset to calculate the
Effective Address (EA).

Ex) LDAA offset, index_resister

This gives a method for the program to alter the Effective Address during execution.
Good for accessing pages/arrays of data in memory.

The Offset is a signed number of the following type:

5-bit : gives range of (-16 to +15) NOTE: remember 2's complement?


9-bit : gives range of (-256 to + 255)
16-bit: gives range of (-32,768 to + 32,767)

The Index Register can be X, Y, SP, and in some special cases PC

-- Embedded Software Design -- 36


Addressing Modes
Indexed Addressing –

The HCS12 offers the ability to increment or decrement the index either pre or post operation.

The syntax is as follows:

"Automatic Pre-decrement"
LDAA n, -X ; n subtracted from X prior to data fetch

"Automatic Pre-increment"
LDAA n, +X ; n added to X prior to data fetch

"Automatic Post-decrement"
LDAA n, X- ; n subtracted from X after data fetch

"Automatic Post-increment"
LDAA n, X+ ; n added to X after data fetch

Notes : n ranges from 1 to 8


Index Register is modified during these instructions

-- Embedded Software Design -- 37


Addressing Modes

Auto pre inc/dec –

Ex) LDX #$0801 Memory


LDAA 1,-X ; auto pre-decrement
$0800 $00
; X-1 → X (first)
; M(X) → A (second) $0801 $11
; after instruction : A=$00, X=$0800 $0802 $22
$0803 $33
Ex) LDX #$0801
LDAA 1,+X ; auto pre-increment
; X+1 → X(first)
; M(X)→ A (second)
; after instruction : A=$22, X=$0802

-- Embedded Software Design -- 38


Addressing Modes

Auto post inc/dec –

Ex) LDX #$0801 Memory


LDAA 1,X- ; auto post-decrement
;M( X) → A (first) $0800 $00
; X-1 → X (second) $0801 $11
; after instruction : A=$11, X=$0800 $0802 $22
$0803 $33
Ex) LDX #$0801
LDAA 1,X+ ; auto post-increment
; M(X) → A (first)
; X+1 → X (second)
; after instruction : A=$11, X=$0802

-- Embedded Software Design -- 39


Addressing Modes
(Indexed-Indirect)

Indexed Indirect - When the "address" of the data to be manipulated resides
in memory.
(INDX, INDY)
- Indirect addressing because the first thing the instruction does is go
to memory and grab the address.

- The second thing it does is go the address and manipulate the data

- We use [square brackets] to indicate indexed indirect addressing

-- Embedded Software Design -- 40


Addressing Modes
(Indexed-Indirect)

Ex) LDX #$e000 ; initializes the X register to
$e000 using Immediate Addressing
LDAA [$10,X] ; Load ACCA with
M(M(X+$10))
Memory

$0800 $AA
-
$e010 $08 $e010:$e011 holds
After instruction ACCA = $AA $e011 $00 the EA to load data
from

-- Embedded Software Design -- 41


Addressing Modes
(INH)

Inherent Addressing – All data for the instruction resides in the CPU registers
(INH) (A, B, D, X,Y,SP, PC, CCR)

Ex)
ABA ; adds ACCA with ACCB and puts the contents in ACCA .(A =
A+B)
INX ; increment register X

Only the Opcode is needed (1-byte instruction)

-- Embedded Software Design -- 42


Addressing Modes
(REL)

Relative Addressing (REL) –
The operand gives an offset that is added to the
memory location where the instruction resides.
This is used for “branching”.
A branch is where the program counter is set to
something besides PC+1 (covered later).
The offset is a signed number.
That means the “effective address” for an 8-bit can be
from –128 to +127 memory locations from the current
address.
-- Embedded Software Design -- 43
Addressing Modes
(REL)

Ex) BRA $02; This will “branch” positive 3 memory locations

Opcode Operand Memory PC = $0003


PC $0000 $20 after the instruction is
$0001 $02 executed.
Machine
$0002 -
Code $20 $02
$0003 -

The effective address is the current instruction


address + $02. The Program Counter will be
set to that address
-- Embedded Software Design -- 44
Addressing Modes
(REL)

Relative Addressing –

The HCS12 supports :


8-bit relative addressing (-128 to +127) and
16-bit relative addressing (-32,768 to +32,767)

from the current PC location.

-- Embedded Software Design -- 45


The STORE Operation

STORE - an operation that moves information from
the internal registers of the CPU (A,B,D,X,Y,SP) and puts into memory.

Where the “information” goes to depends on the


addressing mode.

IMM addressing doesn't apply to stores.

ex) STAA $0F ; DIR addressing,


; A → ($0F)

Opcode Operand

-- Embedded Software Design -- 46


Example
 Memory
Now we can move information in and out of the CPU and Memory using LOADs and STOREs
$0800 $11
Ex) LDX #$0800 $0801 $22
LDY #$0802 Before Execution
LDAA 0,X $0802 -
STAA 0,Y $0803 -
LDAB 1,X
STAB1,Y
What is in the registers after execution?

A = $11
B = $22 $0800 $11
X = $0800 $0801 $22
Y = $0802 After Execution
$0802 $11
What Addressing Modes were used?
$0803 $22
IMM, IND
-- Embedded Software Design -- 47
LOADs and STOREs

So far we have the following instructions:

LOAD STORE
LDAA STAA
LDAB STAB
LDD STD
LDS STS
LDX STX
LDY STY

LEAS (EA) → SP
LEAX (EA) → X
LEAY (EA) → Y

-- Embedded Software Design -- 48


Assembler

Source Code – when you write your program, you will include
additional
information besides just instructions. You Source Code will contain:

1) Instruction Mnemonics
2) Memory Location for instructions
3) Memory Allocation for program data
4) Variable Names
5) Global Constants

-- Embedded Software Design -- 49


Assembler contd…

2 Pass Assembler - The cross- assembler reads in the source file
in two passes.

1st Pass: - assembler directives concerning program location


are decoded and memory locations are setup

- labels / constants / variable names are recognized and


stored

2nd Pass - Opcode / Operands are decoded and given a memory


location of where they are to be downloaded.

- labels / constants / variable names are substituted in


-- Embedded Software Design -- 50
Assembler Fields

Assembler
 to write programs for the HCS12, we must understand how
the assembler works.

 The assembler reads the source text file and decodes the text first depending on
which column the text is in. There are four columns that are called “assembler
fields”

1) Label
2) Opcode
3) Operand
4) Comment

-- Embedded Software Design -- 51


Assembler Fields

Label

 Text that is not proceeded by a white space character (i.e., the first character of a line)
is treated as a label. A label is a piece of text that gets assigned the memory address
of where it is located in the file. This is only used during assembly.

 This makes tracking memory addresses easier

Ex) $e000main ldx main


$e001 ldx #main

- “main” is assigned the value of $e000


- $e000 is substituted everywhere in the code that ‘main’ is typed
- ldx main is using “Extended Addressing”, same as ldx $e000
- ldx #main is using “Immediate Addressing”, same as ldx #$e000

-- Embedded Software Design -- 52


Assembler Fields

Opcode
 Text that is proceeded by 1 group of white space (i.e., a tab) is treated as an Opcode (or label)
 Only HC12 mnemonics can be used in this field. If the assembler does not recognize the
mnemonic, it will give an ERROR
 Example, LDAA, LDX
 A list of all mnemonics are given in the “Programmer’s Reference Guide”
from Freescale

Operand
 text that follows an Opcode is treated as an Operand (3 rd column)

LDAA#$ff

Opcode Operand - the assembler converts this to machine code

$86 $ff
-- Embedded Software Design -- 53
Assembler Fields

Comment
 There are three ways to enter a comment

1) the fourth column of the text file is treated as a comment


Place a “;” in front of the comment to make readable

ex) ldaa #$ff ;this instruction loads $ff into ACCA

2) placing an asterisk (*) or semicolon (;) as the first character of a line makes it a comment

ex) *start of code


main
ldaa #$ff

- Use comments in your code or the lab instructor CAN’T HELP YOU!!!!

-- Embedded Software Design -- 54


Assembler Directives

We need to give the assembler other information about the program such as:

- where to locate code


- allocating memory
- setting up constants

Directives - We accomplish the above using “Directives”. The assembler


directives are specific to the ‘assembler’. The
assembler recognizes keywords and knows what to do.

-- Embedded Software Design -- 55


Assembler Directives

ORG - this tells the assembler where to insert the program code. Any instructions
that follow this directive will be inserted sequentially into memory.

address machine code


Ex) ORG $e000 $e000 $86
ldaa #$ff -> assemble -> $e001 $ff
ldab #$ee $e002 $c6
$e003 $ee

The ORG statement can be used with a label also:

Ex) ORG $0040


RAM ; RAM is a label for address location $0040

ORG $e000 ; we now move to $e000 to insert the following


ldaa RAM ; ACCA = M($40), RAM = $40

-- Embedded Software Design -- 56


Assembler Directives

SECTION -
defines a relocatable (by linker) section of code.
Instructions that follow this directive will be inserted sequentially into memory.

Ex)
ROM SECTION

main: ldaa #$ff ; this code section will be inserted into memory by linker
bra main

RAM SECTION

variables….

-- Embedded Software Design -- 57


Assembler Directives

EQU - This will form a constant and associated label. This does not go into the program
memory, it is only used for readability in your code.

Ex) CONST EQU $ff

ORG $e000 ; start code at $e000


ldaa #CONST ; ACCA = $ff

address machine code


$e000 $86
$e001 $ff
$e002 -
$e003 -

NOTE: The assembler will insert $ff for CONST on the 2 nd pass through the source code.

SET- Similar to EQU but the program can alter the initial value later in program.

-- Embedded Software Design -- 58


Assembler Directives

DS - Define Storage. This reserves and labels a block of memory. Does not initialize

Label: DS.size <n> (size can be "B" or "W", default=B)

Ex) ORG $0800 ; go to $0800


BUFFER1: DS 1 ; reserve 1 byte of memory at $0800 called BUFFER1
BUFFER2: DS.B 2 ; reserve 2 bytes of memory called BUFFER2 (after BUFFER1)
BUFFER3: DS.W 3 ; reserve 3 words of memory called BUFFER3 (after BUFFER2)

address machine code


$0800 - -> Labeled “BUFFER1”
$0801 - -> Labeled “BUFFER2”
$0802 - ->
$0803 - -> Labeled “BUFFER3”
$0804 - ->
$0805 - ->
$0806 - ->
$0807 - ->
$0808 - ->

We can now use these memory locations as variables in the rest of our code.

-- Embedded Software Design -- 59


Assembler Directives

DC - Define Constant Storage. This reserves and labels a block of memory and DOES initialize.

<label.DC.size <value1>,<value2>,… (size can be "B", "W", or "L", default=B)

Ex) ORG $0800 ; go to $0800


BUF1: DC $11 ; reserve 1 byte of memory at $0800 called BUF1 and initialize to $11
BUF2: DC.B $22,$33 ; reserve 2 bytes of memory called BUF2 (after BUF1) and initializes to $22,$33
BUF3: DC.W $ABCD ; reserve 2 bytes of memory called BUF3 (after BUF2) and initializes to $ABCD

address machine code


$0800 $11 -> Labeled “BUF1”
$0801 $22 -> Labeled “BUF2”
$0802 $33 ->
$0803 $AB -> Labeled “BUF3”
$0804 $CD ->
$0805 - ->
$0806 - ->
$0807 - ->
$0808 - ->

We can now use these memory locations as variables in the rest of our code.

-- Embedded Software Design -- 60


Assembler Directives

DCB -Define Constant Block. Similar to DC, but with a few more features to initialize larges blocks.
<label.DCB.size <count>,<values> (size can be "B", "W", or "L", default=B)
(count can range from 1 to 4096)

Ex) ORG $0800 ; go to $0800


BUF1: DCB 1,$11 ; reserve 1 byte of memory at $0800 called BUF1 and initialize to $11
BUF2: DCB.B2, $22 ; reserve 2 bytes of memory called BUF2 (after BUF1) and initializes both to $22
BUF3: DCB.B4, $44 ; reserve 4 bytes of memory called BUF3 (after BUF2) and initializes all to $44

address machine code


$0800 $11 -> Labeled “BUF1”
$0801 $22 -> Labeled “BUF2”
$0802 $22 ->
$0803 $44 -> Labeled “BUF3”
$0804 $44 ->
$0805 $44 ->
$0806 $44 ->
$0807 - ->
$0808 - ->

We can now use these memory locations as variables in the rest of our code.

-- Embedded Software Design -- 61


Assembler Directives

END - this tells the assembler that your program ends here

Ex) ORG $e000 ; start code at $e000


main ; put a label at $e000 called ‘main’
ldaa #$ff ; Put $ff into ACCA
bra main ; Branch Always to main

END ; this is the end of the code

NOTE: The END does not have anything to do with program flow, it only tells the
assembler when you are done writing your code.

-- Embedded Software Design -- 62


Assembler Directives

NOTE: We've covered the most common directives.
There are more directives available. Refer the reference Text.

-- Embedded Software Design -- 63



Thank you!

-- Embedded Software Design -- 64

You might also like