Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Segmentation & Addressing Modes: Name Roll No Section Marks

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

Microprocessor Systems laboratory (EE 351) Page 1 of 6

Lab Manual #3

Name
Roll No
Section
Marks

Segmentation & Addressing modes

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.

After this lab you will be able to


 Describe and use different addressing modes in the 8086 processor.
 Describe and use Physical Segments and Logical Segments in your programs.

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.

Memory model Model Size of Code and Data

TINY Code and data no more than 64KB combined


SMALL Code and data segments must be no more than 64KB each
MEDIUM Code can be more than 64KB, data still limited to no more than 64KB
COMPACT Code limited to no more than 64KB, data can be more than 64KB
LARGE Code and data can each be more than 64K, no array can be larger than
64KB
HUGE Code and data can each be more than 64KB, arrays can be larger than
64KB

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:

 Directive is .data for data segment


 All variables must be declared, and memory space for each allocated.
 Data definition directive can be followed by a single value, or a list of values
separated by commas
 Different data definition directives for different size types of memory
1. DB - define byte (8 bits)
2. DW - define word (16 bits)
3. DD - define double word (32 bits)
4. DQ - define quad word (64 bits)

CODE SEGMENT:

 Directive is .code for code segment


 The "program" resides here
Page 3 of 6

Physical and Offset Addresses:

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

Memory address = (Address of data segment) x 10H + (address of var1)

Data segment
Starting address = 0100H (16 bits)

Offset of Var1 = 0012H (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

MOV AX, @DATA


MOV DS, AX

Or just by adding following directive in the start of the program

.STARTUP

This automatically initializes the CS and DS registers with correct location of code and
data segment respectively.
Page 4 of 6

Different memory addressing schemes are tabulated as follows.


Addressing Source operands
Mode Example Assuming Ds:1000H, BX:0200H, SI:0300H
Working Address Generation Address
Register MOV AX,BX Copies content of BX to AX
Immediate MOV AX,0F7H Copies constant 0F7H to AX
Direct MOV AX,[1234H] Copies contents at memory DS x 10H +1234H 11234H
location 11234H to AX
Register MOV AX,[BX] Copies contents at memory DS x 10H+0200H 10200H
Indirect location pointed by BX (i,e
10200H) to AX
Based MOV AX,[BX+06] Copies contents at memory DS x 10H+0200H + 0006H 10206H
location pointed by BX+6 (i,e
10206H) to AX
Indexed MOV AX,[SI+06] Copies contents at memory DS x 10H+0300H + 0006H 10306H
location pointed by SI+6 (i,e
10306H) to AX
Based- MOVAX,[BX+SI+ Copies contents at memory DS x 10H+0200H+0300H+ 10506H
Indexed 06] location pointed by BX+SI+6 (i,e 0006H
10506H) to AX

Now let’s have some programming using these addressing modes

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

LEA DX, MESSAGE


MOV AH, 09H
INT 21H

MOV AH, 0AH


MOV DX, OFFSET BUF
INT 21H

MOV BH, 00H


MOV BL, BUF[1]
MOV BUF[BX+2], '$'

LEA DX, MESSAGE2


MOV AH, 09H
INT 21H
Page 5 of 6

LEA DX, BUF[2]


MOV AH, 09H
INT 21H

MOV AH, 4CH


INT 21H
END

1. Assemble, Link and Run program1.


2. Use CodeView Debugger to fill in the table associated with program 3.1.
3. Calculate both the effective and physical addresses of each instruction. Put the
results on the given table.

INSTRUCTION SOURCE DESTINATION ADDRESSING MODE

Address, Address, Contents


Register Content Register
before After
Or const. or const.
MOV AX,@DATA
MOV DS,AX
LEA DX,MESSAGE
MOV AH,09H
INT21H Before INT 21H, IP = After INT 21H, IP =
MOV AH, 0AH
MOV DX, OFFSET
BUF
INT21H Before INT 21H, IP = After INT 21H, IP =
LEA
DX,MESSAGE2

MOV AH,09H

INT21H Before INT 21H, IP = After INT 21H, IP =


LEA DX, BUF

MOV AH,09H

INT21H Before INT 21H, IP = After INT 21H, IP =


MOV AX,4C00H

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

You might also like