Program Segments
• A segment is an area of memory that includes up to 64K bytes
• Begins on an address evenly divisible by 16
• 8085 could address a max. of 64K bytes of physical memory
- it has only 16 pins for the address lines (216 = 64K)
• 8088/86 stayed compatible with 8085
- Range of 1MB of memory, it has 20 address pins (220 = 1 MB)
- Can handle 64KB of code, 64KB of data, 64KB of stack
• A typical Assembly language program consist of three segments:
• Code segments
• Data segment
• Stack segment
Program Segments…a sample
Program Segments
Program Segments
Code segment
The 8086 fetches the instructions (opcodes and operands) from the code segments.
The 8086 address types:
– Physical address
– Offset address
– Logical address
• Physical address
– 20-bit address that is actually put on the address pins of 8086
– Decoded by the memory interfacing circuitry
– A range of 00000H to FFFFFH
– It is the actual physical location in RAM or ROM within 1 MB mem. range
• Offset address
– A location within a 64KB segment range
– A range of 0000H to FFFFH
• Logical address
– consist of a segment value and an offset address
Program Segments…example
Define the addresses for the 8086 when it fetches the instructions
(opcodes and operands) from the code segments.
• Logical address:
– Consist of a CS (code segment) and an IP (instruction pointer)
format is CS:IP
• Offset address
– IP contains the offset address
• Physical address
– generated by shifting the CS left one hex digit and then adding it to the IP
– the resulting 20-bit address is called the physical address
give me some numbers…ok
Program Segments…example
Suppose we have:
CS 2500
IP 95F3
• Logical address:
– Consist of a CS (code segment) and an IP (instruction pointer)
format is CS:IP 2500:95F3H
• Offset address
– IP contains the offset address which is 95F3H
• Physical address
– generated by shifting the CS left one hex digit and then adding it to the IP
25000 + 95F3 = 2E5F3H
Program Segments
Data segment
Data segment refers to an area of memory set aside for data
• Format DS:BX or DI or SI
• example:
DS:0200 = 25
DS:0201 = 12
DS:0202 = 15
DS:0203 = 1F
DS:0204 = 2B
Program Segments
Data segment
Example:
Add 5 bytes of data: 25H, 12H, 15H, 1FH, 2BH
Not using data segment
MOV AL,00H ;clear AL
ADD AL,25H ;add 25H to AL
ADD AL,12H
ADD AL,15H
ADD AL,1FH
ADD AL,2BH
Program Segments
Data segment
Example:
Add 5 bytes of data: 25H, 12H, 15H, 1FH, 2BH
using data segment with a constant offset
Data location in memory: Program:
DS:0200 = 25 MOV AL,0
DS:0201 = 12 ADD AL,[0200]
DS:0202 = 15 ADD AL,[0201]
DS:0203 = 1F ADD AL,[0202]
DS:0204 = 2B ADD AL,[0203]
ADD AL,[0204]
Program Segments
Data segment
Example:
Add 5 bytes of data: 25H, 12H, 15H, 1FH, 2BH
using data segment with an offset register
Program:
MOV AL,0
MOV BX,0200H
ADD AL,[BX]
INC BX ;same as “ADD BX,1”
ADD AL,[BX]
INC BX
ADD AL,[BX]
INC BX
ADD AL,[BX]
Endian conversion
• Little endian conversion:
In the case of 16-bit data, the low byte goes to the low
memory location and the high byte goes to the high memory
address. (Intel, Digital VAX)
• Big endian conversion:
The high byte goes to low address. (Motorola)
Example:
Suppose DS:6826 = 48, DS:6827 = 22,
Show the contents of register BX in the instruction MOV BX,[6826]
Little endian conversion: BL = 48H, and BH = 22H
Program Segments
Stack segment
Stack
A section of RAM memory used by the CPU to store
information temporarily.
• Registers: SS (Stack Segment) and SP (stack Pointer)
• Operations: PUSH and POP
– PUSH – the storing of a CPU register in the stack
– POP – loading the contents of the stack back into the CPU
• Logical and offset address format: SS:SP
Flag Register
• Flag Register (status register)
– 16-bit register
– Conditional flags: CF, PF, AF, ZF, SF, OF
– Control flags: TF, IF, DF
ZF
Flag Register and ADD instruction
• Flag Register that may be affected
– Conditional flags: CF, PF, AF, ZF, SF, OF
Flow Control I
• JMP location
Transfers program control to a different point in the
instruction stream without recording return
information.
jmp eax
jmp 0x00934EE4
Flow Control II
• CMP value, value / Jcc location
The compare instruction compares two values, setting
or clearing a variety of flags (e.g., ZF, SF, OF). Various
conditional jump instructions use flags to branch
accordingly.
cmp eax, 4 cmp [ebp+10h], eax
je40320020 jne 40DC0020
Flow Control III
• TEST value, value / Jcc location
The test instruction does a logical AND of the two
values. This sets the SF, ZF, and PF flags. Various
conditional jump instructions use these flags to branch.
test eax, eax test edx, 0056FCE2
jnz 40DA0020 jz 56DC0F20
Looping using zero flag
• The zero flag is set (ZF=1), when the counter becomes zero
(CX=0)
• Example: add 5 bytes of data
MOV CX,05 ;CX holds the loop count
MOV BX,0200H ;BX holds the offset data address
MOV AL,00 ;initialize AL
ADD_LP: ADD AL,[BX] ;add the next byte to AL
INC BX ;increment the data pointer
DEC CX ;decrement the loop counter
JNZ ADD_LP ;jump to next iteration if counter
;not zero