Memory Segmentation, Generating Memory Address: Mustafa Shakir
Memory Segmentation, Generating Memory Address: Mustafa Shakir
Memory Segmentation, Generating Memory Address: Mustafa Shakir
Memory segmentation,
generating memory
address
LE CT URE R. RIYADH B ASSIL
MUSTAFA SHAKIR
Memory segmentation, generating memory address
A Reloadable Program: Is one that can be placed into any area of memory and executed
without change.
A Relocated Data: Are data that can be placed in any area of memory and used without
any change to the program.
Q) Why Segment and Offset Addressing Scheme allows programs and data to be
relocated without changing anything in the programs or data.
The memory segment can be moved to any place in the memory system without
changing any of the offset address. This is accomplished by moving the entire program,
as a block, to a new area and then changing only the contents of the segment register.
Memory segmentation, generating memory address
1. A code segment
2. A data segment
3. A stack segment
Memory segmentation, generating memory address
Physical Address Is the 20-bit address that is actually put on the address pins of the
8086 CPU and decoded by the memory interfacing circuitry.
This address can have a range of 00000H to FFFFFH for the 8086. It is an actual
physical location in RAM or ROM within the (1 megabyte) memory range.
Offset Address Is a location within 64K-byte segment range. The offset address can
range from 0000H to FFFFH.
3.Add IP.
Memory segmentation, generating memory address
Example 1:
If CS = 24F6H and IP = 634AH, show: and Calculate
(a)The logical address (c) The physical address
(b)The offset address (d) The lower range
Solution: (e) The upper range of the code segment
The three columns show the logical address of CS:IP, the machine code stored at that
address and the corresponding Assembly language code.
Memory segmentation, generating memory address
Example 2:
If DS = 7EA2H and the offset is 438EH,
(a) Calculate the physical address. (b) Calculate the lower range.
(c) Calculate the upper range of the data segment. (d) Show the logical address.
Solution:
(a) PA= (segment value*10+offset) = (7EA20+438E) = 83DAE
(b) Lower Range = (segment value*10 + 0000) = (7EA20+0000) = 7EA20
(c) Upper Range = (segment value * 10 + FFFF) = (7EA20 + FFFF) = 8FA1F
(d) 7EA2:438E
Memory segmentation, generating memory address
Example 3:Assume that the DS register contains the value is 578CH. To access a given
byte of data at physical memory location 67F66H, does the data segment cover the range
where the data is located? If not, what changes need to be made?
Solution:
Lower Range = (segment value * 10 + 0000) = (578C + 0000) = 578C0
Upper Range = (segment value * 10 + FFFF) = (578C0 + FFFF) = 678BF
No, since the range is 578C0 to 678BF, location 67F66 is not included in this range. To
access that byte, DS must be changed so that its range will include that byte.
Memory segmentation, generating memory address
MOV [1500H], AX
In cases like this, the low byte goes to the low memory location and the high bytes
goes to the high memory address.
Example 4:
Assume memory locations with the following contents: DS:6826 = 48
and DS:6827 = 22. Show the contents of register BX in the instruction
MOV BX, [6826H]
Solution:
DS:6826 = 48
DS:6827 = 22
Memory segmentation, generating memory address
Each of the memory banks provides half of the 8086's 16-bit data bus.
• The lower bank transfers bytes of data over data lines D0 through D7,
• To synchronize the internal and external operations of the CPU a clock (CLK) input
signal is used.
• The CLK can be generated by the 8284 clock generator IC.
• The 8086 is manufactured in three speeds: 5 MHz, 8 MHz and 10 MHz
Addressing mode, Machine
langage Coding.
LECTURER. RIYADH BASSIL
Addressing mode, Machine langage Coding.
Addressing Modes: Are various ways used by the CPU to access operands in the main
memory.
The 8086 provides 7 different addressing modes.
1- Immediate Addressing Mode. In this mode, the operand is a constant data.
Note: When the instruction is assembled, the operand comes immediately after the code.
Ex: MOV CX, 4929 H
ADD AX, 2387 H
MOV AL, FF H
Note: Immediate addressing mode can be used to load data into registers except the
segment registers and flag register.
Addressing mode, Machine langage Coding.
2- Register Addressing Mode. In this mode, the operand to be accessed resides in an
internal register.
Example: MOV AX, BX
This means copy the 16-bit contents of register BX (which is the source operand), to
register AX (which is the destination operand).
Ex: MOV BX, DX
MOV ES, AX
ADD AL, BH
Note: It should be noted that the source and destination registers must match in size. i.e.
MOV CL, AX , will give an error.
Addressing mode, Machine langage Coding.
3- Direct Addressing Mode.
In this mode the instruction opcode is followed by an effective address (EA) instead of
data,.
so that the (EA) is used directly as the offset to calculate the physical address (PA).
Effective Address is the distance in bytes from the beginning of the segment to the
operands location
Notes:
We can’t use this instruction to copy the contents of one segment register to another segment register
directly.
• We can’t use the code segment register CS as the destination with this instruction.
Data Transfer Instructions
Examples:
1) What is the effect of executing this instruction MOV CX, [20H] if you know
that DS=1A00H.
Solution: Executing this instruction results in:
Physical Address = Segment Value * 10 + Offset PA=DS*10+ Offset
PA = 1A00H * 10 +20H=1A020H
CL will be loaded with the contents held at the memory address: 1A020H
(DS:20)→ (1A00:20)→(CL)
Physical Address of Next location= 1A000H+20H+1H=1A021H
CH will be loaded with the contents held at the memory address: 1A021H
{(DS:20)+1H}→ (1A00:21) →(CH)
Data Transfer Instructions
2) Write assembly instruction program to transfer the data loaded on the
address (1A020H) to the address (1A010H), DS=1A00H.
Solution:
Physical Address = Segment Value * 10 + Offset
Physical Address=DS*10+ Offset
Offset1 = Physical Address1 – (DS*10) = 1A020H – 1A000H = 20H
Offset2 = Physical Address2 – (DS*10) = 1A010H – 1A000H = 10H
MOV AX, [20H]
MOV [10H], AX
3) Initiate or load the data 2000H into DS and ES registers.
Solution:
MOV AX, 2000H
MOV DS, AX
MOV ES, AX
Data Transfer Instructions
2- XCHG Instruction
This instruction used to exchange the contents of the source and destination
operands (Byte or word).
You can exchange between two registers (except segment registers) or a register
and a memory location.
Data Transfer Instructions
Example: If BX=11AAH, DS=1200H, CS=1100H, IP=0101H, [13234H]=FFH,
[13235H]=00H. what is the result of executing this instruction:
XCHG [1234H], BX
Solution:
Executing this instruction will perform the operation:
[DS:1234H] ←→ (BX)
PA=12000H+1234H=13234
After executing this instruction the contents of BX, (13234H), (13235H) will be:
BX=00FFH
[13234H]=AAH
[13235 ]=11 .
Data Transfer Instructions
3- LEA, LDS, LES instructions:
Data Transfer Instructions
Example: If DI=1000H, BX=20H, what is the result of executing the following
instruction:
LEA SI, [DI+BX+5H]
Solution:
when executing this instruction, it loads SI register with an offset address value.
The value of this offset is represented by the EA:
Effective Address = DI + BX + 5H
EA=1000H+20H+5H=1025H
The value of SI =1025H
Data Transfer Instructions
Example: If IP=0100H, CS=1100H, DS=1200H, [200H]=20H, [201H]=00H,
[202H]=00H, [203H]=13H.
What is the result of executing the following instruction: LDS SI, [200H]
Solution:
SI and DS registers are loaded from the word location in memory where offset
address is 200H.
Physical Address = Segment value * 10 + Offset
PA =DS*10 + Offset
PA = 1200H *10 +200H=12200H
So PA 12200H contains 20H , 12201H contains 00H , 12202H contains 00H , 12203H
contains 13H
The content of (SI) will be = 0020H
The content of (DS) will be =1300H
Arithmetic Instructions:
1. Addition instructions:
Arithmetic Instructions:
Arithmetic Instructions:
Arithmetic Instructions:
Arithmetic Instructions:
Arithmetic Instructions:
2. Subtraction instruction:
Arithmetic Instructions:
Example 1: Assume that the content of BX, CX registers are 1234 H and 0123 H
respectively and carry flag is “0”. What is the result of executing this instruction:
SUB BX, CX and what is the value of CF
Solution:
the operation of this instruction will be as:
(BX) – (CX) → (BX)
Barrow → CF
So we get, BX=1234 H – 0123 H = 1111 H
And CF will remain “0” because there is no borrow.
8086 instructions set Part 2
LECTURER. RIYADH BASSIL
Arithmetic Instructions:
3. Multiplication instruction
Arithmetic Instructions:
Example: Assume that the content of AL and CL are (-1H) and (-2H)
respectively. what result is produced in AX by executing the following
instructions:
1) MUL CL
2) IMUL CL
Solution:
1) The first instruction used 2’s complement
(AL) = -1 = 00000001b= 01H
2’s complement = 1111 1111b = FFH
(CL) = -2 = 0000 0010b = 02H
2’s complement = 1111 1110b = FEH
Therefore (AX) = FFH × FEH = FD02H
7 0 7 0
AH AL
2) Convert word to double word (CWD). Reproduce bit 15 of AX throughout DX
15 0 15 0
DX AX
Arithmetic Instructions:
Example: What is the result of executing the following instructions:
MOV AL, A1H
CBW
CWD
Solution: the first instruction will load A1H into AL
(AL)= (A1H)= 1010 0001B
• executing the second instruction will result
(AL)= A1H= 1010 0001B
(AH)= 1111 1111B= FFH
• executing the third instruction will result:
(DX)= 1111 1111 1111 1111B= FFFFH
(AX)=FFA1H, (DX)=FFFFH
Arithmetic Instructions:
Logic Instructions:
6- Logic Instructions:
The 8086 μp has instructions for performing the logic operations (AND, OR, XOR,
NOT).
Logic Instructions:
Logic Instructions:
Logic Instructions:
Example:Describe the result of executing the following sequence of instructions:
MOV AL, 0101 0101B
AND AL, 0001 1111B
OR AL, C0H
XOR AL, 0FH
NOT AL
Solution:
First instruction: move the immediate operand into AL: (AL)= 0101 0101B=55H
Second instruction:(AL) = (AL). 0001 1111B= 0101 0101B . 0001 1111B= 0001 0101B= 15H
Third instruction: (AL) = (AL) + C0H = 00010101B+11000000B= 11010101B=D5H
Forth instruction: (AL) = (AL) 0FH= 11010101B+00001111B= 11011010B=DAH
Fifth instruction: (AL) = () = 00100101B=25H
Logic Instructions:
Example: Write assembly program to perform:
(1) clearing the upper 12 bits of AX
(2) Setting the bit B4 of CL.
Solution:
(3) We mean to clear, change bit or bits to zero, when a bit is ANDed with logic “0”,
the result is always “0”, while if a bit is ANDed with logic “1” its value remains
unchanged. Thus:
AND AX, 000FH.
(4) If a bit is ORed with a logic “0”, its value remains unchanged. However, if it is
ORed with a logic “1”, the result will be always “1”. Thus:
OR CL, 10H.
Shift Instructions:
7- Shift Instructions:
The 8086 μp has (4) instructions that shifts the content of an 8 or 16 bit register or
memory location one or more positions to the left or right.
The four shift instructions can perform two basic types of shift operations; the
logical shift, and arithmetic shift.
Shift Instructions:
Shift Instructions: :
Shift Instructions:
Shift Instructions:
CF MSB LSB
SAL, SHL
0
MSB LSB CF
SHR 0
MSB LSB CF
SAR
Shift Instructions:
Example: Assume that AL=10110100, CF=1, here is how the four shift
instructions affect AL and CF.
• After SAL AL, 1
AL=01101000 , CF=1
• After SAR AL, 1
AL=11011010 , CF=0
• After SHL AL, 1
AL=01101000 , CF=1
• After SHR AL, 1
AL=01011010 , CF=0
Shift Instructions:
ROL
MSB LSB CF
ROR
CF MSB LSB
RCL
MSB LSB CF
RCR
Rotate Instructions:
Example: Assume that AL=10110100, CF=1, Here is how the 4 rotate
instruction affect AL and CF.
• After ROL AL, 1
AL = 0110 1001, CF=1
• After ROR AL, 1
AL = 0101 1010, CF=0
• After RCL AL, 1
AL = 0110 1001, CF=1
• After RCR AL, 1
AL = 1101 1010, CF=0
Rotate Instructions:
Example: what is the result in BX & CF after execute the following
instruction:
RCR BX, CL
Assume that CL =04H, BX=1234H, and CF=0
Solution:
BX=1234H = 0001 0010 0011 0100B
Since CL =04H, then the instruction means rotate register BX four bits through CF.
BX = 1000 0001 0010 0011 = 8123H
CF=0
8086 Stack Memory,PUSH & POP Instructions.
Memory read & write bus cycles, Idle & Wait state
Every register inside the CPU can be stored in the stack, and brought back into the
CPU from the stack memory, except segment registers and SP.
• Loading the contents of the stack into the CPU register is called a pop.
Stack memory
pushing onto the stack
Example:
Assuming that SP = 1236H, AX = 24B6H, DI = 85C2H, and DX = 5F93H, show the
contents of the stack as each of the following instructions is executed:
PUSH AX
PUSH DI
PUSH DX
Solution:
Stack memory
pushing onto the stack
Stack memory
pushing onto the stack
Review Questions:
1. Which registers are used to access the stack?
2. With each PUSH instruction, the stack pointer register SP is (circle one)
incremented/decremented by 2.
3. With each POP instruction, SP is (circle one) incremented/decremented by 2.
4. List three possible logical addresses corresponding to physical address 143F0.
5. The ADD instruction can affect which bits of the flag register?
6. The carry flag will be set to 1 in an 8-bit ADD if there is a carry out from bit ___.
7. CF will be set to 1 in a 16-bit ADD if there is a carry out from bit ____.
BUS CYCLES & TIME STATES
Bus Cycle: It is A Sequence of events at which the CPU communicates with an
external device, which starts with an address being output on the system bus followed
by a read or write data transfer.