LEC2 IntroductionToMicroprocessors
LEC2 IntroductionToMicroprocessors
Microprocessors
and Applications
Lecture 2
Assembly
Programming
Outline
▪ Introduction.
▪ Intel 8086
Microprocessors.
▪ Assembly Language
Programming.
▪ Program Segments.
▪ Addressing Modes.
2
Microprocessor-Based Microcomputer
3
Memory (RAM & ROM)
4
RAM Read & Write Operations
5
Brief History of the Computers
10
Intel 8086 Pin Diagram
11
Registers of 8086 Microprocessor
▪ AX is used for the accumulator, BX is used for base addressing, CX
is used for counter loop operations, DX is used to point out data
in I/O operations.
12
Registers of 8086 Microprocessor
▪ [Link1]
▪ [ALU]
▪ [SEG REGs]
▪ [FLAGs]
13
Assembly Language Programming
▪ Machine Language:
14
Assembly Language Programming
16
MOV Instruction
Rules regarding MOV instruction:
❖ Data can be moved among all registers except the flag register. There are
other ways to load the flag registers (to be studied later).
❖ Source and destination registers have to match in size.
❖ Data can be moved among all registers (except flag reg.) but data can be
moved directly into nonsegment registers only. You can’t move data
segment registers directly.
Examples:
MOV BX,14AFH ;move 14AFH into BX (legal)
MOV SI,2345H ;move 2345H into SI (legal)
MOV DI,2233H ;move 2233H into DI (legal)
MOV CS,2A3FH ;move 2A3FH into CS (illegal)
MOV DS,CS ;move the content of CS into DS (illegal)
MOV FR,BX ;move the content of BX into FR (illegal)
MOV DS,14AFH ;move 14AFH into DS (illegal)
17
MOV Instruction
▪ Important points
18
ADD Instruction
ADD destination, source; dest = dest + source
mnemonic operands
Example:
MOV AL,24H ; move 24H into AL
MOV DL,11H ; move 11H into DL
ADD AL,DL ; AL=AL+DL (AL=35H)(DL =11H)
19
ADD Instruction
ADD destination, source; dest = dest + source
mnemonic operands
20
Program Segments (8086 Real Mode)
▪ Physical Address is the 20-bit address that actually put on the address bus
(in 8086).
❖ Has a range of 00000H - FFFFFH
▪ Segment Address is a 16-bit address of the segment block. Each segment is
a block of 64 KB of memory space.
▪ Offset Address is a location within 64K byte segment range.
❖ Has a range of 0000H - FFFFH
▪ Logical Address consists of segment address and offset address.
22
Program Segments (8086 Real Mode)
23
Program Segments (8086 Real Mode)
24
Program Segments (8086 Real Mode)
Example: If CS register contains 2500H and IP register contains 95F3H. What is the
Logical Adress in the code segment?
CS:IP → 2500:95F3 (default in adressing is hex. You don’t need H)
25
Program Segments (8086 Real Mode)
Physical Address is generated by shifting the CS one hex digit to the left and
adding IP. Physical address is 20-bit address which can be generated by using a
logical address as follows.
1. Start with CS.
2. Shift left CS (insert 0 as the Least significant digit).
3. Add IP.
Example: If CS register contains 1980 H and IP register contains 78FE H. What is the
Physical Adress in the code segment (in real mode)?
Logical address: CS:IP → 1980:78FE
1. Start with CS 1980
2. Shift left CS 19800
3. Add IP 78FE (19800 + 78FE = 210FE)
Physical address: The microprocessor will retrieve the instruction from the memory
locations starting from 210FE (20-bit address).
26
Program Segments (8086 Real Mode)
Solution:
a) The logical address is; 24F6:634A
b) The offset address is; 634A
c) The Physical address is; 24F60 + 634A = 2B2AA
d) The lower range of the code segment: 24F6:0000 → 24F60 + 0000 = 24F60
e) The upper range of the code segment: 24F6:FFFF → 24F60 + FFFF = 34F5F
27
Program Segments (8086 Real Mode)
28
Program Segments (8086 Real Mode)
Example: If DS = 7FA2H and the offset is 438EH, determine (in real mode) :
a) The physical address
b) The lower range of the data segment
c) The upper range of the data segment
d) Show the logical address
Solution:
a) The Physical address is: 7FA20 + 438E = 83DAE
b) The lower range: 7FA20 + 0000 = 7FA20
c) The upper range: 7FA20 + FFFF = 8FA1F
d) The logical address is: 7FA2:438E
29
Program Segments (8086 Real Mode)
• Addressing in Data Segment
Why do we use data segment?
Assume that a program is needed to add 5 bytes of data (25H, 12H, 15H,1FH and 2BH)
One way: MOV AL, 00H ; initialize AL
ADD AL, 25H
ADD AL, 12H
ADD AL, 15H code and data are mixed
ADD AL, 1FH (bad programming practice)
ADD AL, 2BH ; AL=25+12+15+1F+2B
Better way: Assume that the Data segment contains the array of bytes starting from
offset address 0200H.
MOV AL, 0 ; clear AL
ADD AL, [0200] ; add the contents of DS:200 to AL
ADD AL, [0201] ; add the contents of DS:201 to AL code and data are separated
ADD AL, [0202] ; add the contents of DS:202 to AL (good programming practice)
ADD AL, [0203] ; add the contents of DS:203 to AL
ADD AL, [0204] ; add the contents of DS:204 to AL
30
Program Segments (8086 Real Mode)
This convention is called little endian convention: This convention is used by Intel.
Big Endian Convention is the opposite, where the high byte goes to the low address and
low byte goes to the high address. Motorola microprocessor uses this convention.
31
Program Segments (8086 Real Mode)
32
Flag Register EMU8086 Tutorial – Page 20
33
Flag Register
CF, the Carry Flag: This flag is set whenever there is a carry out, either from d7 after an 8-bit
operation, or from d15 after a 16-bit data operation.
PF, the Parity Flag: After certain operations, the parity of the result’s low-order byte is checked. If the
byte has an even number of 1s, the parity flag is set to 1; otherwise, it is cleared.
AF, the Auxiliary Carry Flag: If there is a carry from d3 to d4 of an operation this bit is set to 1,
otherwise cleared (set to 0).
ZF, the Zero Flag: The ZF is set to 1 if the result of the arithmetic or logical operation is zero,
otherwise, it is cleared (set to 0).
SF, the Sign Flag: MSB is used as the sign bit of the binary representation of the signed numbers.
After arithmetic or logical operations the MSB is copied into SF to indicate the sign of the result.
TF, the Trap Flag: When this flag is set it allows the program to single step, meaning to execute one
instruction at a time. Used for debugging purposes.
IF, Interrupt Enable Flag: This bit is set or cleared to enable or disable only the external interrupt
requests.
DF, the Direction Flag: This bit is used to control the direction of the string operations.
OF, the Overflow Flag: This flag is set whenever the result of a signed number operation is too large,
causing the high-order bit to overflow into the sign bit.
34
Flag Register
• The Flag Register (FR) and bit fields:
• The Flag register is a 16-bit register sometimes referred as the status register. Although
the register is 16-bit. Not all the bits are used.
• Conditional flags: 6 of the flags are called the conditional flags, meaning that they
indicate some condition that resulted after an instruction was executed. These 6 are:
CF, PF, AF, ZF, SF, and OF.
35
Flag Register
• Manipulating the Flag Register:
• There are two instructions that can be used to use/change the content of
the flag register.
• PUSHF Instruction:
PUSHF ; Copy the flag register into stack
SP register is decremented by 2
Example: Copy the content of the flag register into register AX.
PUSHF ; copy the content of Flag register into the stack.
POP AX ; copy from the stack into AX
36
Flag Register
• Manipulating the Flag Register:
• There are two instructions that can be used to use/change the content of
the flag register.
• POPF Instruction:
Example: Clear the flag bits. (Make the flag bits to be all 0)
MOV AX, 0000H
PUSH AX ; now top of the stack contains 16-bit 0.
POPF ; copy the content of stack into flag register.
37
Flag Register
• Flag Register and ADD instruction
• The flag bits affected by the ADD instructions are: CF, PF, AF, ZF, SF and OF. The OF will
be studied later.
• Example: Show how the flag register is affected by the addition of 38H and 2FH.
• The following example shows the implementation of the loop concept in the program
which adds 5 bytes of data. Assume that the beginning address of the array of 5 bytes
starts from offset address 0200H in the data segment.
40
Addressing Modes
• The CPU can access operands (data) in various ways, called addressing modes.
In 80x86, there are 7 addressing modes:
1. register.
2. immediate.
3. direct.
4. register indirect.
5. based relative.
6. indexed relative.
7. based indexed relative.
1. Register addressing mode:
❖ involves the use of registers
❖ memory is not accessed, so faster
❖ source and destination registers must match in size.
Example: MOV BX,DX ; possible
MOV ES,AX ; possible
ADD AL,BH ; possible
MOV AL,CX ; not possible
41
Addressing Modes
2. Immediate addressing mode:
❖ source operand is a constant
❖ possible in all registers except segment , instruction pointer, and flag registers.
Ex: Find the physical address of the memory location and its content after the execution of the
following operation. Assume DS = 1512H
MOV AL,99H ; Physical address of DS:3518 => 15120+3518=18638H
MOV [3518],AL ; The memory location 18638H will contain the value 99H
42
Addressing Modes
4. Register indirect addressing mode:
❖ The address of the memory location where the operand resides is held
by a register.
❖ SI, DI and BX registers are used as the pointers to hold the offset
addresses.
❖ They must be combined with DS to generate the 20-bit physical address
Ex: MOV AL, [BX] ; moves into AL the contents of the memory location pointed to by DS:BX
43
MOV Instruction EMU8086 Tutorial – Page 6
44
MOV Instruction
45
Assembly Program Structure
Write an 8086-assembly
located at offset A1 to
46
Textbook
47