Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

COMPUTER

ORGANIZATION TOPIC FOR THE WEEK: Memory Segmentation, Stacks, and


Addressing Modes

NOTES: Memory Segments

Only four of these segments can be active at any one time. They are the
code segment, stack segment, data segment, and extra segment.

The locations of the segments of memory that are active are


identified by the value of the address held in the 8086/8088’s
four internal segment registers; CS, SS, DS, and ES.

Code
Segment

CS
Stack
SS
Segment
DS
ES
Segment
Registers

Data
Segment

Extra
Segment

Types of Segments

The segments are:

1. Code Segment. This contains the program or code. The address of


the next instruction executed by the 8086/8088 is generated by
adding the contents of IP (offset address) to the contents of CS x
10H.

2. Data Segment. This contains data referenced by almost all


instructions and many addressing modes. Data are almost always
moved into or out of MM via the data segment. The physical
address of the data is generated by adding the contents of one of
the index or pointer registers (BX, DI, or SI) to the contents of DS x
10H.

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.

4. Extra Segment. This is normally for string instructions. When a


string instruction is executed, the destination location is addressed
by the destination index register (DI) plus ES 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).

The value of SP is initialized to FFFFH upon start-up of the


microcomputer. Combining the value with the current value in SS gives
the highest-addressed location in the stack (bottom of the stack). The
stack grows down in memory from offset FFFFH to 0000H.

Pushing a Data onto the Stack

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.

SP is then decremented by 2 so that the next word of data is


stored in the next available memory location.

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.

Stack Segment = 18000H up to 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.

Top of the Stack = SS x 10H + SP


= 18000H + 3A74H
= 1BA74H

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.

PUSH BX; 1BA73H (BH) = 12H


1BA72H (BL) = 34H

New SP = SP -2
= 3A74H – 2H
= 3A72H

Then we can now determine the new top of the stack using the updated
stack pointer.

New Top of Stack = 1BA72H

Example 1

Assume the following register contents:

AX = 4A30H BP = 0008H CS = 8000


H
BX = 0010H SP = 0040H DS = 8000
H
CX = 1800H DI = 0006H SS = 8002
H
DX = 1234H SI = 0040H ES = 9000
H

After the PUSH BP instruction is executed, what will be the contents of


BP, SS, and SP?

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.

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.

Top of the Stack = SS x 10H + SP


= 80020H + 0040H
= 80060H

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.

PUSH BP; 8005FH (BP) = 00H


8005EH (BP) = 08H

New SP = SP - 2
= 0040H – 2H
= 003EH

Then we can now determine the new top of the stack using the updated
stack pointer.

New Top of Stack = 8005EH

Popping a Data onto the Stack

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.

SP is then incremented by 2. Example:

POP CX

Assume: SS = 1234H SP = 281AH

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.

Stack Segment = 12340H up to 2433FH

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.

Top of the Stack = SS x 10H + SP


= 12340H + 281AH
= 14B5AH

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.

POP CX; CL (14B5AH) - this is the current top of


stack
CH (14B5BH) - top of stack +1

New SP = SP + 2
= 281AH + 2
= 281CH

Then we can now determine the new top of the stack using the updated
stack pointer.

New Top of Stack = 14B5CH

Example 2

Assume the following register contents:

AX = 4A30H BP = 0008H CS = 8000


H
BX = 0010H SP = 0040H DS = 8000
H
CX = 1800H DI = 0006H SS = 8002
H
DX = 1234H SI = 0040H ES = 9000
H

After the POP AX instruction is executed, what will be the contents of


BP, SS, and SP?

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.

Top of the Stack = SS x 10H + SP


= 80020H + 0040H
= 80060H

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.

POP CX; CL (80060H) - this is the current top of


stack
CH (80061H) - top of stack +1

New SP = SP + 2
= 0040 + 2
= 0042H

Then we can now determine the new top of the stack using the updated
stack pointer.

New Top of Stack = 80062H

Exercise 1

Assume the following register contents:

AX = 4A30H BP = 0008H CS = 8000


H
BX = 0010H SP = 0040H DS = 8000
H
CX = 1800H DI = 0006H SS = 8002
H
DX = 1234H SI = 0040H ES = 9000
H

Given the following program segment:

PUSH CX
PUSH BX
PUSH DL

Assume that each instruction is dependent on one another.

CODE: CSP107
Page 6 of 16
COMPUTER Exercise 2
ORGANIZATION
Assume the following register contents:

AX = 4A30H BP = 0008H CS = 8000


NOTES:
H
BX = 0010H SP = 0040H DS = 8000
H
CX = 1800H DI = 0006H SS = 8002
H
DX = 1234H SI = 0040H ES = 9000
H

Given the following program segment:

POP BX
POP BP
POP SI

Assume that instructions are dependent on one another.

Addressing Modes

Addressing modes refer to the way in which an operand is specified.

Case Study: MOV destination, source

Types of Data 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:

MOV AX, CX; AX [CX]


MOV BX, DX; BX [DX]

In both examples, AX and BX are the destination operands while CX and


DX are the source operands.

Immediate Addressing

The source operand is part of the instruction instead of the contents of a


register. Typically, immediate operands represent constant data.

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:

MOV AX, BETA

The above instruction transfers the contents of a memory location


specified by the memory label BETA to the AX register. To determine
the particular location in the main memory determine the physical
address.

AX [DS x 10H + BETA]

If BETA = 1234H and DS = 0200H, then:

To determine the physical address, add the physical base segment


address which is the DS x 10H and the effective address referred to by
the label BETA.

PA = DS x 10H + BETA
= 02000H + 1234H = 03234H

MOV AX, BETA; AX [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:

MOV AX, LIST

The above instruction transfers the contents of a memory location


specified by the memory label LIST to the AX register. To determine the
particular location in the main memory determine the physical address.

AX [DS x 10H + LIST]

If LIST = 000AH and DS = BAAFH, then:

To determine the physical address, add the physical base segment


address which is the value of DS x 10H and the effective address
referred to by the label LIST.

CODE: CSP107
Page 9 of 16
COMPUTER PA = DS x 10H + LIST
ORGANIZATION = BAAF0H + 000AH = BAAFAH

MOV AX, LIST; AX [BAAFAH]


NOTES:
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 [BAAFAH]
AH [BAAFBH]

Register Indirect Addressing

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:

MOV AX, [BX]; AX [DS x 10H + BX]

The above instruction transfers the contents of a memory location whose


address is specified by the content of a base register, BX. To determine
the particular location in the main memory determine the physical
address.

If BX = C15EH and DS = 1829H, then:

To determine the physical address, add the physical base segment


address which is the value of DS x 10H and the effective address
referred to by the content of BX.

PA = DS x 10H + BX
= 18290H + C15EH = 193EEH

MOV AX, [BX]; AX [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:

MOV AX, [SI]; AX [DS x 10H + SI]

The above instruction transfers the contents of a memory location whose


address is specified by the content of an index register, SI. To determine
the particular location in the main memory determine the physical
address.

If SI = 00BEH and DS = 58A2H, then:

To determine the physical address, add the physical base segment


address which is the value of DS x 10H and the effective address
referred to by the content of SI.

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:

If BP is used, then the stack segment is addressed instead of the data


segment

MOV CX, [BP]; AX [SS x 10H + BP]

The above instruction transfers the contents of a memory location whose


address is specified by the content of a base register, BP. To determine
the particular location in the main memory determine the physical
address.

If BP = 1800H and SS = 8050H, then:

To determine the physical address, add the physical base segment


address which is the value of SS x 10H since BP register is used and the
effective address referred to by the content of BP.

PA = SS x 10H + BP
= 80500H + 1800H = 81D00H

MOV CX, [BP]; CX [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:

MOV AX, [BX + 1000H]

The above instruction transfers the contents of a memory location whose


address is specified by the content of a base register BX, a direct
displacement and the segment base address. To determine the
particular location in the main memory determine the physical address.

AX [DS x 10H + BX + 1000H]

If BX = 0100H and DS = 0200H, then:

To determine the physical address, add the physical base segment


address which is the value of DS x 10H since BX register is used, the
displacement value 1000H and the to the content of BX.

PA = DS x 10H + BX + 1000H
= 02000H + 0100H + 1000H
= 03100H

MOV AX, [BX + 1000H]; AX [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:

MOV AL, [BX] + BETA

The above instruction transfers the contents of a memory location whose


address is specified by the base register BX, an indirect displacement
and the segment base address. To determine the particular location in
the main memory determine the physical address

AX [DS x 10H + BX + BETA]

If BX = 1000H, BETA = 1234H and DS = 0200H, then:

To determine the physical address, add the physical base segment


address which is the value of DS x 10H since BX register is used, the
effective address referred to by the memory label BETA and the to the
content of BX.

PA = DS x 10H + BX + BETA
= 02000H + 1000H + 1234H
= 04234H

MOV AL, [BX] + BETA;

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:

MOV DI, SET[BX]

The above instruction transfers the contents of a memory location whose


address is specified by the base register BX, an indirect displacement
and the segment base address. To determine the particular location in
the main memory determine the physical address

DI [DS x 10H + SET + BX]

If BX = FB04H, SET = 001AH, and DS = 0210H, then:

To determine the physical address, add the physical base segment


address which is the value of DS x 10H since BX register is used, the
effective address referred to by the memory label SET and the to the
content of BX.

PA = DS x 10H + SET + BX
= 02100H + 001AH + FB04H
= 11C1EH

MOV DI, SET[BX]; DI [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

This is used to transfer a byte or word between a register and the


memory location indicated by the sum of a base register and an index
register.

Example 1:

MOV AX, [BX + SI]

The above instruction transfers the contents of a memory location whose


address is specified by the sum of a base register BX and an index
egister SI. To determine the particular location in the main memory
determine the physical address.

AX [DS x 10H + BX + SI]

If SI = 2000H, BX = 1234H, and DS = 0200H, then:

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

MOV AX, [BX + SI]; AX [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:

MOV [BX + DI], SP

The above instruction transfers the content of register SP to a memory


location whose address is specified by the value of the base register BX
and the index register DI. To determine the particular location in the
main memory where the data is to be stored or written, determine the
physical address.

[DS x 10H + BX + DI] SP

If DI = 175CH, BX = 1256H, and DS = 0205H, then:

To determine the physical address, add the physical base segment


address which is the value of DS x 10H, the value of the base register
BX and the value of the index register DI.

PA = DS x 10H + BX + DI
= 02050H + 1256H + 175CH
= 04A02H

MOV [BX + SI], SP; 04A02H [SP]

The value of the low-order 8 bits of SP transferred to the physical


address computed. The value of the high-order 8 bits of SP is
transferred to the memory location specified by the computed physical
address plus 1.

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:

MOV AX, [BX + SI + 0100H];

The above instruction transfers the contents of a memory location whose


address is specified by the sum of a base register BX, an index register
SI and a displacement of 0100H. To determine the particular location in
the main memory determine the physical address.

AX [DS x 10H + BX + SI + 1000H]

If DS = 1000H, BX = 0020H, and SI = 0010H, then:

To determine the physical address, add the physical base segment


address which is the value of DS x 10H, the value of the base register
BX, the value of the index register SI and the displacement value of
1000H.

PA = DS x 10H + BX + SI + 1000H
= 10000H + 0020H + 0010H + 1000H
= 11030H

MOV AX, [BX + SI + 1000H];

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:

MOV AX, FILE[BX + DI];

The above instruction transfers the contents of a memory location whose


address is specified by the sum of a base register BX, an index register
DI and a memory location whose address is specified by the label FILE.
To determine the particular location in the main memory determine the
physical address.

AX [DS x 10H + BX + DI + FILE]

If DS = 1F00H, BX = 3000H, DI = 0015H, AND FILE = 1234H,


then:

To determine the physical address, add the physical base segment


address which is the value of DS x 10H, the value of the base register
BX, the value of the index register DI and the effective address of
memory label FILE.

CODE: CSP107
Page 15 of 16
COMPUTER PA = DS x 10H + BX + DI + FILE
ORGANIZATION = 1F000H + 3000H + 0015H + 1234H
= 23199H

MOV AX, FILE[BX + DI];


NOTES:
AX [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

Identify the addressing mode of the following instructions:

1. ADD AX, FADEH


2. CMP FADE, AX
3. INC DH
4. ADC [BP+1800H], BX
5. AND AGAIN[BP+SI], DS
6. OR DX, [DI]
7. ADC AX, [BP+SI]

CODE: CSP107
Page 16 of 16

You might also like