0% found this document useful (0 votes)
31 views

Microprocessor Based System: Muhammad Syargawi B. Abdullah Photonics Lab, Mimos Berhad For Unikl, Miit Sept 2012

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Microprocessor Based System: Muhammad Syargawi B. Abdullah Photonics Lab, Mimos Berhad For Unikl, Miit Sept 2012

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

Microprocessor Based System

IED24103
Programming Intel 8086 Microprocessor

Muhammad Syargawi b. Abdullah


Photonics Lab, MIMOS Berhad
For UniKL, MIIT
Sept 2012
What is a Computer Program?

• A computer program is basically a sequence of


commands (instructions) used to tell
computer what to do
• Every computer, regardless of its architecture
and complexity, has two basic elements at its
heart: a CPU & memory
• These two components make it possible to
run programs
Machine Code

• On the most basic level, a computer program is nothing


more than a collection of binary code stored in memory
• Different binary numbers tell the CPU to do different
things
• The CPU reads the numbers one at a time, decodes
them, and does what the numbers say
• For example, if CPU reads “64” as a part of the program,
it will add 1 to the number stored AX
• If the CPU reads “146”, it will swap the content in AX with
the content in BX
• Hexadecimal is generally used to represent binary codes
Assembly Language
• Issues with machine code:
– difficult to memorize
– error prone due to long series of ‘0’ and ‘1’
• Assembly language programming uses mnemonics to represent
each instruction
– easier to program
– made up of two to four letters (e.g. MOV) which are initials or
shortened from English word(s)
– MOV is short for move
• Assembly language is a machine specific programming language
– one-one correspondence between statements and native machine
language
– matches machine instruction set and architecture
• IBM-PC Assembly Language:
– Refers to 8086, 8088, 80186, 80286, 80386, 80486, and Pentium
Processor
8086 Assembly Language
Programming Format

Label Opcode Operand [s] ; comment

; Add data
Start ADD AL, 07H 07H into AL

; move data
MOV AX, 0A05H 0A05H into AX
Programming Format ctn...
• Label – optional: alphanumeric string
– 1st character must be a-z, A-Z, ?, @, _, $
• Opcode – assembly language instruction
– mnemonic an instruction format for humans
– assembler translates mnemonic into hexadecimal opcode (e.g.
MOV is F8H)
• Operand – 0-3 pieces of data required by instruction
– can be several different forms
– delineated by commas
– immediate, register name, memory data, & memory address
• Comment: Extremely useful
– rule of thumb: 1 comment per instruction
Instruction Types

• The instruction set of 8086 microprocessor


can be divided into six different groups:
i. Data Transfer
ii. Arithmetic
iii. Bit Manipulation
iv. String
v. Program Execution Transfer (Branch & Loop)
vi. Processor Control
Data Transfer

• It’s a group of instructions that transfer data


from memory to register, register to register,
and register to memory
• Also include instructions that perform I/O data
transaction
• Data can be 8-bit or 16-bit and all 8086’s
registers are accessible (including the stack
pointer and flag register)
General Purpose Data Transfer

• MOV Destination, Source


– Move byte or word from specified source to specified
destination
– Source operand can be register, memory location or
immediate operand
– Destination can be register or memory operand
– Both source and destination can’t be memory location
at the same time
– E.g.:
MOV CX,037AH
MOV AL,BL
MOV BX,[0301H]
Addressing Modes For Data
Transfer
• 8 addressing modes for MOV instruction
i. Register addressing mode
ii. Immediate addressing mode
iii. Direct addressing mode
iv. Register Indirect addressing mode
v. Based addressing mode
vi. Indexed addressing mode
vii. Based indexed addressing mode
viii. Based, indexed with displacement addressing mode
Addressing Modes For Data
Transfer
• Register and Immediate addressing modes
MOV AX, 1 immediate
MOV BX, AX register

• Direct addressing mode


MOV AX, DS:[0200H] data moved from memory
location DS:0200H
– the square bracket denotes the content of the
memory location
Addressing Modes For Data
Transfer
• Register Indirect addressing modes
– allows data to be addressed at any memory location
through an offset address held in [BP], [BX], [SI] or [DI]

MOV AX,[BX] move the content stored in


memory location DS:BX
– [BX], [SI] & [DI] modes use DS by default while [BP]
mode uses SS by default
– we can use segment override prefix symbol if we wish
to access data in different segments
e.g.: MOV AX,CS:[BX]
Addressing Modes For Data
Transfer
• Register Indirect Byte addresses in memory DI
0200 0204

MOV DI, 0204H 56 02

MOV BX, [DI] BX


MOV AX, [BX] 0250 0256

34 12

If DS:0204H contains
0256H, then AX will
contain whatever is
stored at DS:0256H AX = 1234
Addressing Modes For Data
Transfer
• Based addressing mode
– The offset address of the operand is given by the
sum of contents of the BX or BP registers and 8-bit
or 16-bit displacement
suppose BX contains
0200H, then data is
MOV DX, [BX+04H]
moved from location
DS:0204H
Addressing Modes For Data
Transfer
• Indexed addressing mode
– the operands offset address is found by adding
the content of SI or DI register with 8-bit or 16-bit
displacements
suppose SI contains
0200H, then data is
MOV BX, [SI+04H]
moved from location
DS:0204H
Addressing Modes For Data
Transfer
• Based-Indexed addressing mode
– the offset address of the operand is computed by
summing the base register (BX or BP) and the
content of an index register (SI or DI)

MOV AX, [BX+SI]

if BX contains 1598H and SI contains 1004H,


then AX will be loaded with 16-bit data
from location DS:259C and DS:259D
Addressing Modes For Data
Transfer
• Based-Indexed with displacement addressing
mode
– the operands offset is computed by adding the
base register content, and index registers content
and 8 or 16-bit displacement

MOV AX, [BP+DI+125H]

suppose BP contains 1000H and DI contains


05H, then AX will be loaded with 16-bit
data from location SS:112A and SS:112B
General Purpose Data Transfer

• PUSH Operand
– It push specified word (operand) to top of the stack
– E.g.:
PUSH BX

• POP Destination
– Pop the word (operand) from top of the stack to the
specified location (destination)
– Destination can be a general purpose register, segment
register (except CS) or memory location
– E.g.:
POP AX
General Purpose Data Transfer

• XCHG Destination, Source


– Exchange the contents of the specified source and
destination
– It can’t change two memory location
simultaneously
– E.g.:
XCHG DX, AX
I/O Port Data Transfer

• IN Accumulator, Port Address


– Read a byte or word (operand) from specified port
and transfer it to the accumulator
– E.g.:
IN AX, 0028H

• OUT Port Address, Accumulator


– Transfer a byte or word from accumulator to a
specified port
– E.g.:
OUT 0028H, AX
Special Address Data Transfer

• LEA Register, Source


– Load effective address
– Loads 16-bit register with the offset address of the
data specified by the source
– E.g.:
LEA BX, [DI]
• This instruction loads the content of DI (offset) into the
BX register
Special Address Data Transfer

• LDS Destination, Source


– It load 32-bit pointer from memory source to
destination register and DS
– The offset is placed in the destination register and the
segment is place in DS
– To use this instruction the word at the lower memory
address must contain the offset and the word at the
higher address must contain the segment
– E.g.:
LDS BX, [0301H]
Special Address Data Transfer

• LES Destination, Source


– It loads 32-bit pointer from memory source to
destination register and ES
– The offset is placed in the destination register and
the segment is placed in ES
– Similar to LDS except that it initializes ES instead of
DS
– E.g.:
LES BX, [0301H]
Flag Register Data Transfer

Load AH register with the low byte of the flag


LAHF
register
Store content of AH register to low byte of flag
SAHF
register

PUSHF Copy flag register to top of the stack

POPF Copy word at top of the stack to flag register


Arithmetic

• ADD Destination, Source


– It adds a byte to byte or a word to word.
– It effects AF, CF, OF, PF, SF, ZF flags
– E.g.:
ADD AL, 74H
ADD DX, AX
ADD AX, [BX]
Arithmetic

• ADC Destination, Source


– It adds the two operands with CF.
– It effects AF, CF, OF, PF, SF, ZF flags
– E.g.:
ADC AL, 74H
ADC DX, AX
ADC AX, [BX]
Arithmetic

• SUB Destination, Source


– It subtracts a byte from byte or a word from word
– It effects AF, CF, OF, PF, SF, ZF flags.
– For subtraction, CF acts as borrow flag
– E.g.:
SUB AL, 74H
SUB DX, AX
SUB AX, [BX]
Arithmetic

• SBB Destination, Source


– It subtracts the two operands and also the borrow
from the result.
– It effects AF, CF, OF, PF, SF, ZF flags
– E.g.:
SBB AL, 74H
SBB DX, AX
SBB AX, [BX]
Arithmetic

• INC Source
– It increments the byte or word by one
– The operand can be a register or memory location
– It effects AF, OF, PF, SF, ZF flags
– CF is not effected
– E.g.:
INC AX
Arithmetic
• DEC Source
– It decrements the byte or word by one
– The operand can be a register or memory location
– It effects AF, OF, PF, SF, ZF flags
– CF is not effected
– E.g.:
DEC AX

• NEG Source:
– It creates 2’s complement of a given number
– That means, it changes the sign of a number
Arithmetic

• CMP Destination, Source


– It compares two specified bytes or words
– The source and destination can be a constant, register
or memory location
– Both operands cannot be a memory location at the
same time
– The comparison is done simply by internally
subtracting the source from destination
– The value of source and destination does not change,
but the flags are modified to indicate the result
Arithmetic
• MUL Source
– It is an unsigned multiplication instruction
– It multiplies two bytes to produce a word or two words to
produce a double word
AX = AL * Src
DX : AX = AX * Src
– This instruction assumes one of the operand in AL or AX
– Source can be a register or memory location

• IMUL Source
– It is a signed multiplication instruction
Arithmetic

• DIV Source
– It is an unsigned division instruction
– It divides word by byte or double word by word
– The operand is stored in AX, divisor is source and
the result is stored as:
AH = remainder AL = quotient
• IDIV Source
– It is a signed division instruction
Arithmetic

• CBW (Convert Byte to Word)


– This instruction converts byte in AL to word in AX
– The conversion is done by extending the sign bit of AL
throughout AH

• CWD (Convert Word to Double Word)


– This instruction converts word in AX to double word in
DX : AX
– The conversion is done by extending the sign bit of AX
throughout DX
Bit Manipulation

• These instructions are used at the bit level


• These instructions can be used for:
– Testing a zero bit
– Set or reset a bit
– Shift bits across registers
Bit Manipulation

• NOT Source
– It complements each bit of source to produce 1’s
complement of the specified operand
– The operand can be a register or memory location
Bit Manipulation

• AND Destination, Source


– It performs AND operation of destination and
source
– Source can be immediate number, register or
memory location
– Destination can be register or memory location
– Both operands cannot be memory locations at the
same time
– CF and OF become zero after the operation
– PF, SF and ZF are updated
Bit Manipulation

• OR Destination, Source
– It performs OR operation of destination and
source
– Source can be immediate number, register or
memory location
– Destination can be register or memory location
– Both operands cannot be memory locations at the
same time
– CF and OF become zero after the operation
– PF, SF and ZF are updated
Bit Manipulation

• XOR Destination, Source


– It performs XOR operation of destination and
source
– Source can be immediate number, register or
memory location
– Destination can be register or memory location
– Both operands cannot be memory locations at the
same time
– CF and OF become zero after the operation
– PF, SF and ZF are updated
Bit Manipulation

• SHL Destination, Count (CL)


– It shift bits of byte or word left, by count
– It puts zero(s) in LSBs
– MSB is shifted into carry flag
– If the number of bits desired to be shifted is 1,
then the immediate number 1 can be written in
Count
– However, if the number of bits to be shifted is
more than 1, then the count is put in CL register
Bit Manipulation Instructions

• SHR Destination, Count (CL)


– It shift bits of byte or word right, by count
– It puts zero(s) in MSBs
– LSB is shifted into carry flag
– If the number of bits desired to be shifted is 1,
then the immediate number 1 can be written in
Count
– However, if the number of bits to be shifted is
more than 1, then the count is put in CL register
Bit Manipulation

• ROL Destination, Count


– It rotates bits of byte or word left, by count
– MSB is transferred to LSB and also to CF
– If the number of bits desired to be shifted is 1,
then the immediate number 1 can be written in
Count
– However, if the number of bits to be shifted is
more than 1, then the count is put in CL register
Bit Manipulation

• ROR Destination, Count


– It rotates bits of byte or word right, by count
– LSB is transferred to MSB and also to CF
– If the number of bits desired to be shifted is 1,
then the immediate number 1 can be written in
Count
– However, if the number of bits to be shifted is
more than 1, then the count is put in CL register
Program Execution Transfer

• These instructions cause change in the


sequence of the execution of instruction
• This change can be through a condition or
sometimes unconditional
• The conditions are represented by flags
Program Execution Transfer

• CALL Destination
– This instruction is used to call a subroutine or
function or procedure
– The address of next instruction after CALL is saved
onto stack
• RET
– It returns the control from procedure to calling
program
– Every CALL instruction should have a RET
Program Execution Transfer

• JMP Destination
– This instruction is used for unconditional jump
from one place to another

• Jxx Destination (Conditional Jump):


– All the conditional jumps follow some conditional
statements or any instruction that affects the flag
Conditional Jump Table
Mnemonic Meaning Jump Condition
JA Jump if Above CF = 0 & ZF = 0
JAE Jump if Above or Equal CF = 0
JB Jump if Below CF = 1
JBE Jump if Below or Equal CF = 1 or ZF = 1
JC Jump if Carry CF = 1
JE Jump if Equal ZF = 1
JNC Jump if Not Carry CF = 0
JNE Jump if Not Equal ZF = 0
JNZ Jump if Not Zero ZF = 0
JPE Jump if Parity Even PF = 1
JPO Jump if Parity Odd PF =0
JZ Jump if Zero ZF = 1
Program Execution Transfer

• Loop Destination
– This is a looping instruction
– The number of times looping is required is placed
in the CX register
– With each iteration, the contents of CX are
decremented
– ZF is checked whether to loop again or not
String

• String in assembly language is just a


sequentially stored bytes or words
• There are very strong set of string instructions
in 8086
• By using these string instructions, the size of
the program is considerably reduced
String

• CMPS Destination, Source


– It compares the string bytes or words.

• SCAS String
– It scans a string
– It compares the string with byte in AL or with
word in AX
String

• MOVS / MOVSB / MOVSW


– It causes moving of byte or word from one string
to another
– In this instruction, the source string is in Data
Segment and destination string is in Extra
Segment
– SI and DI store the offset values for source and
destination index
String

• REP (Repeat):
– This is an instruction prefix
– It causes the repetition of the instruction until CX
becomes zero
– E.g.:
REP MOVSB STR1, STR2
– It copies byte by byte contents
– REP repeats the operation MOVSB until CX
becomes zero
Processor Control

• These instructions control the processor itself.


• 8086 allows programmer to control certain
control flags that:
– causes the processing in a certain direction
– processor synchronization if more than one
microprocessor attached
Processor Control

Opcode Purpose
STC It sets the carry flag to 1
CLC It clears the carry flag to 0
CMC It complements the carry flag
It sets the direction flag to 1.
STD If it is set, string bytes are accessed from higher
memory address to lower memory address.
It clears the direction flag to 0.
CLD If it is reset, the string bytes are accessed from
lower memory address to higher memory address.
Assembler Directives

• Assembler directives are the direction to the


assembler which indicate how and operand or
section of the program is to be processed
• These are also called pseudo operations which
are not executable by the microprocessor
Assembler Directives

Directives Purpose
To inform the assembler the name of the logical
ASSUME
segment it should use for a specified segment
E.g.: ASSUME DS:DATA
• Tells the assembler that for any program instruction
which refers to the data segment, it should use the
logical segment called DATA
Assembler Directives

Directives Purpose
Define byte. To declare a byte variable or set aside
DB one or more storage locations of type byte in
memory
E.g.: TEMP_VALUE DB 36H
• Tells the assembler to reserve 1 byte of memory for a
variable named TEMP_VALUE and to put the value 36H in
that menory location when the program is loaded into
RAM
Assembler Directives

Directives Purpose
Define word. Tells the assembler to define a
DW variable of type word or to reserve storage
locations of type word in memory.
Define double word. To declare a variable of type
DD double word or restore memory locations which
can be accessed as type double word.
Define quad word. To tell the assembler to declare
DQ a variable with length of 4 words or to reserve 4
words of storage in memory.
Assembler Directives

Directives Purpose
Define ten bytes. To inform the assembler to define
DT a variable which is 10 bytes in length or to reserve
10 bytes of storage in memory.
Equate. To give name to some value or symbol.
Every time the assembler finds the given name in
EQU
the program, it will replace the name with the
value or symbol we have equated with that name.
Procedure. To identify the start of a procedure or
PROC
subroutine.
Assembler Directives

Directives Purpose
Originate. Changes the starting offset address of
ORG the data. It allows to set the location counter to a
desired value at any point in the program.
E.g.: ORG 3000H
• Tells the assembler to set the location counter to address
3000H
Assembler Directives

Directives Purpose
End program. This directive indicates the assembler
that this is the end of the program module. The
END
assembler ignores any statements after an END
directive.
End procedure. It indicates the end of the
ENDP
procedure or subroutine to the assembler.
End segment. This directive is used with the name
of the segment to indicate the end of that logical
ENDS segment.
E.g.: CODE SEGMENT ; start code segment
END SEGMENT ; end code segment
Assembly Language Development
Tools
• To develop an assembly language program, we
need certain program development tools.
• The tools are very handful for engineers or
programmers to write, edit, assemble, debug,
simulate and load the assembly code into
targeted hardware.
Assembly Language Development
Tools
Tools Purpose
A program which allows us to create an assembly code program. As
we type the assembly codes, the editor stores the ASCII codes for
Editor
letters and numbers in successive RAM location. The code’s source
files need to be saved with .ASM extension.
To translate the assembly language mnemonics into machine
language (i.e. binary codes). The assembler reads the source file and
generates two files: object file (.OBJ) and list file (.LST).

Assembler The object file consists of binary codes for the instructions and
information about the addresses of the instructions. After further
processing, the contents of the file will be loaded into memory and
run. A listing file can be used to see what error the assembler
encountered.
Assembly Language Development
Tools
Tools Purpose
A program to connect several object files into one large object file.
While writing a large program, it is better to divide the program into
Linker several modules (modular type of programming). Each module can
be individually written, tested & debugged. Then all modules are
linked together to form one functioning program.
An emulator is a mixture of hardware and software. It is usually
Emulator used to test and debug the hardware and software of an external
system such as the prototype of microprocessor based instrument.
Assembly Language Development
Tools
Tools Purpose
A program to load your object code program into system memory,
execute the program, and troubleshoot or debug it. The debugger
always looks into the contents of registers and memory location s
after the program has been run.

With debugger, we can also change the contents of the register and
Debugger memory locations and return the program. Some debuggers allow
you to halt the running program after each instruction has been
executed so that the contents of memory or registers can be
checked or altered.

Debugger also allows us to set breakpoints at any points within the


program. The running program will stop upon meeting this point.
Processes for Creating
Executable Code

Object File
(machine code, .HEX)
Source File
Assembler
(text, .ASM)

List File
(text file, .LST, may include symbol table)
Processes for Creating
Executable Code

Source File Object File


Assembler
(text, .ASM) (machine code)

Source File Object File Object Executable


Assembler
(text, .ASM) (machine code) Linker File

Source File Object File


Compiler
(text, .C) (machine code)
General Structure of Assembly Language
Program

Data Segment ; start of data segment


------- ;
------- ;
Data Ends ;

Code Segment ; start of code segment


Assume CS:Code, DS:Data

Main Proc Far ; Near/Far depends on RET to be used


------- ;
HLT ; Halt the system
Main Endp ;

Sub Proc Near ; Near/Far depends on RET to be used


------- ;
------- ;
Sub Endp ;

Code Ends ; end of code segment


End Main ;
That’s all folks… Thank You!!!

Please ask now or…


Mobile: 012-2905356
Email: [email protected]
Skype: ah-wee

You might also like