Topic 7 - 8 - Memory Segmentation, Stacks and Addressing Modes
Topic 7 - 8 - Memory Segmentation, Stacks and Addressing Modes
Only four of these segments can be active at any one time. They are the
code segment, stack segment, data segment, and extra segment.
Code
Segment
CS
Stack
SS
Segment
DS
ES
Segment
Registers
Data
Segment
Extra
Segment
Types of Segments
3. Stack Segment. This is for the LIFO stack. The physical address
is a combination of the contents stack pointer (SP) plus SS x 10H.
CODE: CSP107
Page 1 of 16
COMPUTER Stack
ORGANIZATION
The stack is implemented in MM of the 8086/8088. It is 64KB long and
is organized from a software point of view as 32K words.
NOTES:
The lowest-addressed byte in the current stack is pointed to by the base
address in the SS register.
Any number of stacks may exist in the 8086/8088. A new stack can be
brought in by simply changing the value in the SS register. However,
only one may be active at any one time.
Stack
The SP contains an offset from the value in SS. The address obtained
in the SS and SP is the physical address of the top of the stack or TOS
(the last storage location in the stack to which data were pushed).
Whenever a word of data is pushed onto the stack, the high-order 8 bits
are placed in the location addressed by SP - 1, and the low-order 8 bits
are placed in the location addressed by SP - 2.
Example:
PUSH BX
Assume: BX = 1234H
SS = 1800H SP = 3A74H
To determine the bottom of the stack (the physical address of the bottom
of the stack); add the physical segment base address and the offset
address. The segment base address is the stack segment since we are
referring to stack and the offset is the last address which is FFFFH.
CODE: CSP107
Page 2 of 16
COMPUTER
ORGANIZATION Bottom of the Stack = SS x 10H + FFFFH
= 18000H + FFFFH
= 27FFFH
NOTES:
So the range of the stack segment is from the physical segment base
address, 18000H up to the bottom of the stack, 27FFFH.
To compute for the top of the stack (physical address), add the physical
segment base address and the offset address which is the value of the
stack pointer.
After executing the command PUSH BX, the value of the high-order 8
bits which is referred to by BH is stored at the address 1BA73H (Top of
the Stack – 1) and the low-order 8 bits referred by BL is stored at the
address 1BA72H (Top of the Stack -2). SP is then decremented by 2 so
that the next word of data is stored in the next available memory location.
New SP = SP -2
= 3A74H – 2H
= 3A72H
Then we can now determine the new top of the stack using the updated
stack pointer.
Example 1
Answer:
To determine the bottom of the stack (the physical address of the bottom
of the stack); add the physical segment base address and the offset
address. The segment base address is the stack segment since we are
referring to stack and the offset is the last address which is FFFFH.
CODE: CSP107
Page 3 of 16
COMPUTER
ORGANIZATION
Bottom of the Stack = SS x 10H + FFFFH
= 80020H + FFFFH
= 901FFH
NOTES:
So the range of the stack segment is from the physical segment base
address, 80020H up to the bottom of the stack, 901FFH.
To compute for the top of the stack (physical address), add the physical
segment base address and the offset address which is the value of the
stack pointer.
After executing the command PUSH BP, the value of the high-order 8
bits of BP is stored at the address 8005FH (Top of the Stack – 1) and
the low-order 8 bits of BP is stored at the address 8005EH (Top of the
Stack -2). SP is then decremented by 2 so that the next word of data is
stored in the next available memory location.
New SP = SP - 2
= 0040H – 2H
= 003EH
Then we can now determine the new top of the stack using the updated
stack pointer.
Whenever a word of data is popped from the stack, the low-order 8 bits
are removed from the location addressed by SP, and the high-order 8
bits are removed from the location addressed by SP + 1.
POP CX
To determine the bottom of the stack (the physical address of the bottom
of the stack); add the physical segment base address and the offset
address. The segment base address is the stack segment since we are
referring to stack and the offset is the last address which is FFFFH.
CODE: CSP107
Page 4 of 16
COMPUTER Bottom of the Stack = SS x 10H + FFFFH
ORGANIZATION = 12340H + FFFFH
= 2433FH
So the range of the stack segment is from the physical segment base
NOTES: address, 12340H up to the bottom of the stack, 1933FH.
To compute for the top of the stack (physical address), add the physical
segment base address and the offset address which is the value of the
stack pointer.
After executing the command POP CX, the low-order 8 bits are removed
from the location addressed by SP (current top of the stack) and store it
to CL and the high-order 8 bits are removed from the location addressed
by SP + 1 (current top of the stack + 1) and store it at CH. SP is then
incremented by 2.
New SP = SP + 2
= 281AH + 2
= 281CH
Then we can now determine the new top of the stack using the updated
stack pointer.
Example 2
Answer:
To determine the bottom of the stack (the physical address of the bottom
of the stack); add the physical segment base address and the offset
address. The segment base address is the stack segment since we are
referring to stack and the offset is the last address which is FFFFH.
CODE: CSP107
Page 5 of 16
COMPUTER Bottom of the Stack = SS x 10H + FFFFH
ORGANIZATION = 80020H + FFFFH
= 901FFH
So the range of the stack segment is from the physical segment base
NOTES:
address, 80020H up to the bottom of the stack, 901FFH.
Stack Segment = 80020H up to 901FFH
To compute for the top of the stack (physical address), add the physical
segment base address and the offset address which is the value of the
stack pointer.
After executing the command POP CX, the low-order 8 bits are removed
from the location addressed by SP (current top of the stack) and store it
to CL and the high-order 8 bits are removed from the location addressed
by SP + 1 (current top of the stack + 1) and store it at CH. SP is then
incremented by 2.
New SP = SP + 2
= 0040 + 2
= 0042H
Then we can now determine the new top of the stack using the updated
stack pointer.
Exercise 1
PUSH CX
PUSH BX
PUSH DL
CODE: CSP107
Page 6 of 16
COMPUTER Exercise 2
ORGANIZATION
Assume the following register contents:
POP BX
POP BP
POP SI
Addressing Modes
1. Register Addressing
2. Immediate Addressing
3. Direct Addressing
4. Register Indirect Addressing
5. Register Relative Addressing or Base Addressing
6. Base-Plus-Index Addressing
7. Base-Relative-Plus-Index Addressing
CODE: CSP107
Page 7 of 16
COMPUTER Register Addressing
ORGANIZATION
The operand to be accessed is specified as residing in an internal
register of the 8086/8088.
NOTES:
Pros
– Limited number of registers
• Shorter instructions
• Faster operand fetch
• No memory access
• Fast instruction execution
Cons
– Very limited address space
Examples:
Immediate Addressing
Examples:
MOV AL, 15H; AL 15H
MOV AX, 1A3F; AX 1A3FH
Pros
▪ No memory reference is required to fetch data
▪ Fast
Cons
▪ Limited range of data
CODE: CSP107
Page 8 of 16
COMPUTER Direct Addressing
ORGANIZATION
The location following the instruction opcode holds an effective memory
address (EA) instead of data. This EA is the 16-bit offset of the storage
location specified by the current value of the DS register.
NOTES:
Pros
▪ Single memory reference to access data
▪ No additional calculations to work out effective address (EA)
Cons
▪ Limited address space 2K
o K = length of address field
Example 1:
PA = DS x 10H + BETA
= 02000H + 1234H = 03234H
The value of the low-order 8 bits is retrieved from the physical address
computed and is transferred to AL. The value of the high-order 8 bits is
retrieved from the physical address + 1 and is stored at AH.
AL [03234H]
AH [03235H]
Example 2:
CODE: CSP107
Page 9 of 16
COMPUTER PA = DS x 10H + LIST
ORGANIZATION = BAAF0H + 000AH = BAAFAH
AL [BAAFAH]
AH [BAAFBH]
Similar to direct addressing but this time, the EA resides in either a base
register (BX, BP) or index register (SI, DI) within the 8086/8088.
Example 1:
PA = DS x 10H + BX
= 18290H + C15EH = 193EEH
The value of the low-order 8 bits is retrieved from the physical address
computed and is transferred to AL. The value of the high-order 8 bits is
retrieved from the physical address + 1 and is stored at AH.
AL [193EEH]
AH [193EFH]
Example 2:
CODE: CSP107
Page 10 of 16
COMPUTER
ORGANIZATION
PA = DS x 10H + SI
= 58A20H + 00BEH = 58ADEH
NOTES:
MOV AX, [SI]; AX [58ADEH]
The value of the low-order 8 bits is retrieved from the physical address
computed and is transferred to AL. The value of the high-order 8 bits is
retrieved from the physical address + 1 and is stored at AH.
AL [58ADEH]
AH [58ADFH]
Example 3:
PA = SS x 10H + BP
= 80500H + 1800H = 81D00H
The value of the low-order 8 bits is retrieved from the physical address
computed and is transferred to AL. The value of the high-order 8 bits is
retrieved from the physical address + 1 and is stored at AH.
CL [81D00H]
CH [81D01H]
CODE: CSP107
Page 11 of 16
COMPUTER Register Relative Addressing or Base Addressing
ORGANIZATION
The physical address of the operand is obtained by adding a direct or
indirect displacement to the contents of either BX or BP and the current
value in DS or SS, respectively.
NOTES:
Example 1:
PA = DS x 10H + BX + 1000H
= 02000H + 0100H + 1000H
= 03100H
The value of the low-order 8 bits is retrieved from the physical address
computed and is transferred to AL. The value of the high-order 8 bits is
retrieved from the physical address + 1 and is stored at AH.
AL [03100H]
AH [03101H]
Example 2:
PA = DS x 10H + BX + BETA
= 02000H + 1000H + 1234H
= 04234H
CODE: CSP107
Page 12 of 16
COMPUTER
ORGANIZATION The value of the low-order 8 bits is retrieved from the physical address
computed and is transferred to AL.
AL [04234H]
NOTES:
Example 3:
PA = DS x 10H + SET + BX
= 02100H + 001AH + FB04H
= 11C1EH
The value of the low-order 8 bits is retrieved from the physical address
computed and is transferred to DILOW. The value of the high-order 8 bits
is retrieved from the physical address + 1 and is stored at DIHIGH.
DIlow [11C1EH]
DIhigh [11C1FH]
Base-Plus-Index Addressing
Example 1:
CODE: CSP107
Page 13 of 16
COMPUTER To determine the physical address, add the physical base segment
ORGANIZATION address which is the value of DS x 10H, the value of the base register
BX and the value of the index register SI.
PA = DS x 10H + BX + SI
NOTES:
= 02000H + 1234H + 2000H
= 05234H
The value of the low-order 8 bits is retrieved from the physical address
computed and is transferred to AL. The value of the high-order 8 bits is
retrieved from the physical address + 1 and is stored at AH.
AL [05234H]
AH [05235H]
Example 2:
PA = DS x 10H + BX + DI
= 02050H + 1256H + 175CH
= 04A02H
04A02H [SPlow]
04A03H [SPhigh]
CODE: CSP107
Page 14 of 16
COMPUTER Base-Relative-Plus-Index Addressing
ORGANIZATION
This is used to transfer a byte or word between a register and the
memory location addressed by a base register and an index register plus
a displacement.
NOTES:
Example 1:
PA = DS x 10H + BX + SI + 1000H
= 10000H + 0020H + 0010H + 1000H
= 11030H
The value of the low-order 8 bits is retrieved from the physical address
computed and is transferred to AL. The value of the high-order 8 bits is
retrieved from the physical address + 1 and is stored at AH.
AX [11030H]
AL [11030H]
AH [11031H]
Example 2:
CODE: CSP107
Page 15 of 16
COMPUTER PA = DS x 10H + BX + DI + FILE
ORGANIZATION = 1F000H + 3000H + 0015H + 1234H
= 23199H
The value of the low-order 8 bits is retrieved from the physical address
computed and is transferred to AL. The value of the high-order 8 bits is
retrieved from the physical address + 1 and is stored at AH.
AL [23199H]
AH [2319AH]
Exercise
CODE: CSP107
Page 16 of 16