Segmentation & Addressing Modes: Name Roll No Section Marks
Segmentation & Addressing Modes: Name Roll No Section Marks
Segmentation & Addressing Modes: Name Roll No Section Marks
Lab Manual #3
Name
Roll No
Section
Marks
Introduction
In this experiment you will be introduced to physical and logical segmentation of
the memory. You will also deal with the different types of addressing modes, and learn
how to calculate the physical addresses from offset addresses and vice versa.
Overview
Memory has always had a key role to play with microprocessor based systems
since microprocessor manipulates data and memory stores it. Segmentation of memory
(Logical or physical) deals with storage of the data (or program) in memory while the
addressing modes are related with the process to retrieve that stored data from memory.
Let’s first see the segmentation of memory and the directives used to do that.
.MODEL Assembler directive that defines the memory model to be used in the
program. The memory model determines the size of the code, stack
and data segments of the program
.STACK Assembler directive that reserves a memory space called stack
segment for use of program instructions (during interrupts and
function calls)
.DATA Assembler directive that reserves a memory space called data
segment for constants and variables being used in program
.CODE Assembler directive that reserves space called code segment for
storage of program instructions in memory
END Assembler directive that finishes the assembly program
Microprocessor Systems Laboratory (EE 351), Electrical Engineering department at University of Engineering and
Technology, Lahore
Page 2 of 6
.MODEL DIRECTIVE
A few assembly language memory models are tabulated on the basis of the
physical memory they require.
STACK SEGMENT:
Segment means a large contagious chunk of memory in ram which is used for
storing data.
Directive is .stack for stack segment
Should be declared even if program itself doesn't use stack needed for subroutine
calling (return address) and possibly passing parameters
May be needed to temporarily save registers or variable content
Will be needed for interrupt handling while program is running
DATA SEGMENT:
CODE SEGMENT:
Since the allowed physical memory space with an 8086 based processor is 1MB,
which means we need to have 20 bits address registers (2²⁰ = 1 MB ) to retrieve data
from the memory but we only have registers that are 16 bits wide. The solution is, use
segmented memory model and generate the physical address using two registers,
Segment register and offset register. Consider the following example of address
generation.
.model small
.stack 100h
.data
var db “abcdefghij$”
var1 db 10 ; Place 10D in var1
.code
…
…
end
In this case var1 is the offset address and data segments address is segment address so
total physical address would be
Data segment
Starting address = 0100H (16 bits)
Memory address = 01000H (20 bits) + 0012(16 bits) = 01120H (20 bits
address)
But during programming we don’t have to care about this because assembly
languages addressing modes automatically caters for this. What we have to do is to
initialize segment registers in the start of the program. This can be done by following
lines of code
.STARTUP
This automatically initializes the CS and DS registers with correct location of code and
data segment respectively.
Page 4 of 6
PROGRAM 1
.MODEL SMALL
.STACK 200
.DATA
MESSAGE1 DB 'Enter your message: ', 0dh, 0ah, '$'
MESSAGE2 DB 0dh, 0ah, 'The message you just entered :', 0dh, 0ah, '$'
BUF DB 20, 22 DUP (?)
.CODE
MOV AX,@DATA
MOV DS, AX
MOV AH,09H
MOV AH,09H
INT21H
Page 6 of 6
Lab Assignments:
1. Assuming that data segment has starting address 1000H in program#1, calculate
starting address of both message strings and the buffer.
2. Write a program in Assembly language that asks the user to enter two strings each of
them contains Name, Registration Number(2009-EE-XXX) and Marks. Assuming all the
substrings will contain max 15 bytes of memory, Display them separately on a New line
(put $ signs in appropriate positions).Also Swap the marks entered for first
Registration number with the marks of the second.
SAMPLE OUTPUT:
Enter Info: Zubair ,2009-EE-321 ,68
Muazzam , 2009-EE-125 , 88
You Entered
First User
Zubair
2009-EE-321
Marks: 68
Second User
Muazzam
2009-EE-125
Marks: 88
After Swapping
Marks of Zubair: 88
Marks of Muazzam: 68