100% found this document useful (1 vote)
527 views

Microcontroller PPT 1

This document provides an overview of assembly language programming. It discusses that microcomputers need step-by-step instructions to process data. Programs are defined as sequences of commands that tell the microcomputer what to do. Assembly language uses alphanumeric symbols instead of binary to represent machine code instructions. It also discusses the different stages in producing an executable program from assembly code, including assembly, linking, and loading onto the target microcontroller.

Uploaded by

Luckmore Maponga
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
527 views

Microcontroller PPT 1

This document provides an overview of assembly language programming. It discusses that microcomputers need step-by-step instructions to process data. Programs are defined as sequences of commands that tell the microcomputer what to do. Assembly language uses alphanumeric symbols instead of binary to represent machine code instructions. It also discusses the different stages in producing an executable program from assembly code, including assembly, linking, and loading onto the target microcontroller.

Uploaded by

Luckmore Maponga
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

Lecture 1

ASSEMBLY LANGUAGE
PROGRAMMING
M.MUNYARADZI
2019
CT215
ASSEMBLY LANGUAGE
PROGRAMMING
• A  microcomputer does not know how to
process data.
• Must be told exactly what to do, where to
get data, what to do with data, where to
put the results when it is done.
• These are Jobs of software in the
μComputer system.
• Program: defined as sequence of
commands used to tell μComputer what to
do.
• Instruction: Each command in a program.
ASSEMBLY LANGUAGE
PROGRAMMING
• Software is a general name used to refer to
a wide variety of programs that can be run
by a μComputer.
• Egs are Operating System, Application
System programs, etc
• System software represents a group of
programs that enable the Computer to
operate & is known as the operating
system (OS).
ASSEMBLY LANGUAGE
PROGRAMMING
• A collection of programs installed on the
computer for the user is the application
software.
• E.g. of frequently used PC- based applications
are Word, Excel, Power Point, etc
• Native language of PC is machine language
• Programs must always be coded in this
machine language b4 they can be executed
by a μP
ASSEMBLY LANGUAGE
PROGRAMMING
• A program written in machine language is
often referred to as machine code, using 0s &
1s.
• Even if a μP understands only machine code, it
is impossible to write programs directly in
machine language.
• In Assembly language, each of the operations
that can be performed by μP, is described with
alphanumeric symbols instead of 0s & 1s
ASSEMBLY LANGUAGE
PROGRAMMING
• A single Assembly Language Statement
represents each instruction in a program.
• This statement must specify which operation is
to be performed & what data are to be
processed.
• For this reason, an instruction can be divided
into 2 parts; its operation code (opcode) &
its operands.
• The opcode is that part of the instruction that
identifies the operation that is to be performed.
ASSEMBLY LANGUAGE
PROGRAMMING
• Eg., typical operations are add, subtract &
move.
• Each opcode is assigned a unique letter
combination called a mnemonic.
• The mnemonics for the earlier mentioned
operations are ADD, SUB, and MOV.
• Operands describe the data that are to be
processed as the μP carries out the operation
specified by the opcode.
ASSEMBLY LANGUAGE
PROGRAMMING
• They identify whether the source & the
destination of the data are registers within
MPU or Storage locations in the data
memory.
• E g. Of an instruction written for 8085:
ADD Ax, Bx
This instruction says “ Add the contents of
registers Bx and Ax together and put the
sum in Ax “
ASSEMBLY LANGUAGE
PROGRAMMING
• Ax is called the destination operand becoz it is the
place where the result ends up, & Bx is called the
source operand
• An Eg of complete assembly language statement is
• START: MOV Ax, Bx; Copy Bx into Ax.
• General format for an assembly language statement is:
LABEL : INSTRUCTION ; COMMENT
ASSEMBLY LANGUAGE
PROGRAMMING
• Programs written in assembly language are
referred to as source code.
• Assembly language programs cannot be directly
run on the 8085/8086/8088 etc
• They must be translated to an equivalent
machine language program for execution by the
μP.
• Conversion is done automatically by running the
source program through a program known as an
assembler- object code
ASSEMBLY LANGUAGE
PROGRAMMING
• A compiled machine code implementation
of a program that was written in a high-level
language results in many more machine
language instructions than a hand –written
assembly language version of the program.
• This leads to the 2 key benefits derived
from writing programs in assembly la
nguage.
Programming Requirements for a
Microcontroller
• The microcontroller
software development will
be based around a
programming environment
which will normally include:
• A text editor to develop the
source program code
• An Assembler to produce a
machine executable
program with linked library
modules
• Some form of debugging to
allow the program to be
tested
• Access to a programmer to
program the target device.
Stages in the Production of a
Program
• The job of the Assembler is to convert the source code produced by the programmer into
an object code file.  It works by replacing each line of assembly instruction code with the
corresponding machine code instruction that will be executed on the target
microcontroller.
• The object code file cannot be executed on the target micro-controller.  It needs to be
passed through a program called a Linker.
• The job of the Linker is to take the object file produced by the Assembler and link it with
standard library modules (that have already been pre-assembled into object files) to
produce a complete working executable file that can be loaded into the memory of the
microcontroller.
• Sometimes the Assembler and Linker are combined into one program so that assembling
a source program automatically links it as well.  This is the case with the PIC Assembler.
• Note that the only stage that requires a lot of user input is the initial production of the user
source code file. The rest of the program production involves using the Assembler and
Linker tools.
• 'Filename' is the name of the program that the user is developing, thus the output of the
Editor is the source file 'filename.asm'.
• The output of the Assembler is the object file 'filename.obj'.
• The Linker links the 'filename.obj' with any Library object files that the program might use.
It will also link any user object files that the user might want to include.
• The Linker output is the executable file to run on the target system.
• A program called a Loader takes your executable .hex file from the hard disk and loads it
onto the target microcontroller.
Hex file and the target
microcontroller
• The original source file will contain instructions about where to place the
.hex file in the target microcontroller memory. Typically, this instruction will
be:
• ORG  0000H
• This instruction ORGanises the target program to be loaded to the target
microcontroller memory address 0000 hex.
• ie. at the address   0000 0000 0000 0000 binary.
• Other key addresses may also be needed, such as the interrupt vector
addresses for any interrupt service routines (isr) that the program may use.
• Generally, the Loader program is integrated into the development
environment so that it is performed by the programmer tools.
• For example, in the MPLAB PIC development environment, the PICSTART
menu will deal with loading the target program into the memory of the
EPROM Programmer system which will then program the target
microcontroller which has been placed into the zero insertion socket of the
programmer system.
Source Code Writing

• Will look at the way in which an assembly language program is structured


and introduce some of the assembly language instructions of the PIC
microcontroller.
• The source code for a microcontroller can be written in assembly language
or in a high level language such as C (provided) a suitable compiler is
available). For this part of the course we will use assembly language
programming.
• A typical assembly language program will consist of:
• some assembler directives
• sub-routines if they are needed
• the main program code

• Assembler directives are a collection of commands that tell the assembler


such things as the type of microcontroller being used, its clock speed, etc.
They also allow names to be used for memory locations, ports and
registers, so making the program more readable.
Some assembler directives
• The EQU directive associates a name with a physical value such as
an address in memory.
• Name EQU physical_value
• After the equate directives the names can be used instead of register
addresses within the PIC microcontroller,
ie. meaningful names can be used instead of the numerical values.
• eg. CLRF  RTCC   will clear all the bits in the timer counter register
(which has the physical address of 1, or 0001 hex).
• The instruction   CLRF 1  would do the same, but the use of the label
RTCC makes the instruction more readable.
• Some of the equates refer to individual bits within a particular register.
• eg. CARRY EQU 4 refers to the fourth bit in the status register.
• The name CARRY only has meaning when referring to the status
register.
• eg.BCF STATUS,CARRY will clear the carry bit (bit 4) in the status
register, 
Equate directives for I/O registers
• A program will typically also include
equate directives for the I/O registers.
• More of these register labels can be
declared for other PIC registers.
• We can also declare memory equates
such as
The PIC Instruction Set
• The instructions used by the PIC microcontroller, the PIC instruction set  falls into
three groups:
• Bit operations
• Byte operations
• Literal/Control operations
• Most of these instructions execute in a single clock cycle.  The duration of a single
clock cycle depends on the frequency of the clock oscillator which is usually between
4MHz and 20MHz, depending upon the PIC device.
• To keep EM levels low most general purpose applications will use a 4MHz clock and
the execution time is therefore 1µS per instruction.
• Each memory cycle is made up of four states so the duration of a clock cycle (and
hence the instruction execution time) is four times the oscillator period.
• If the oscillator frequency is 4MHz, the execution time will be 1µS per instruction. If
the oscillator frequency is 20MHz, the execution time will be 200nS per instruction.
The Instruction Set and its Use

• The following section looks at the PIC instruction set, giving examples of
how the instructions are used.
• Bit Operations
• One of the principal requirements of a microcontroller is to control the
external environment via the digital I/O ports.  Indeed, the first
microcontrollers (Intel 8048/8031, Motorola 6801) were basically generic
microprocessors with integrated digital I/O and a timer module.
• Controlling digital I/O often involves turning individual bits on and off, taking
care not to affect any other I/O bits. Traditionally this has been achieved by
using ANDing and ORing mask operations, but most modern
microcontrollers now provide special instructions to selectively set or reset
individual bits of a selected port.
• The PIC has two instructions, one to turn on a selected bit of a port, the
other to turn it off.
•  
TURNING ON or OFF A GROUP
OF BITS
• Note that there is no instruction for turning on or
off a group of bits.  To do this it is necessary to
set or reset each bit in turn or use a mask.
• For example,  assuming that all bits of portB
start at zero, the effect of three such operators in
sequence would be as follows

• BSF portb,0 0000 0001


• BSF portb,1 0000 0011
• BCF portb,0 0000 0010
Branching Instructions
• All microcontrollers need an instruction to jump out of the program
sequence.  The instruction to do this on the PIC is the GOTO instruction.
 
• GOTO K causes the program to jump to the label/address k.

eg. GOTO 0050 causes the program to go to program memory


address 0050 hex .
• GOTO instructions cause the program to branch to another part of the
program.  They are often used to cause the program to loop back to repeat
an earlier section of code.
• For example,  the following program code will turn on and off bit 5 of port A
in an endless loop:

agn BSF porta,5 ; comment


BCF porta,5 ; comment
GOTO agn ; go back and do it again
Typical PIC application
• A typical PIC application may involve testing an input bit
and performing some action when the bit changes state.
E.g the input could be the state of a door switch and the
action could be to turn on a relay which then switches on
a motor.
• The PIC provides two instructions, Bit Test and Skip if
Set (BTFSS) and Bit Test and Skip if Clear (BTFSC).
•  
• BTFSS f,b Test bit b in file register f and skip the next
. instruction if it is set (ie.1) 
• BTFSC f,b Test bit b in file register f and skip the
next . instruction if it is clear (ie.0)
Typical Interfacing Example
• Program will cause bit 0
of portB to pulse on and
off while input bit 2 of
portA is a 1,
• ie. while the switch S1 is
open.  As soon as bit 2 of
portA becomes a 0 (when
the switch is closed), the
program will clear bit 1 of
portB and so energise the
relay.

You might also like