Microcontroller PPT 1
Microcontroller PPT 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
• 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