0% found this document useful (0 votes)
7 views

Lab Manual 4

Uploaded by

Anees
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lab Manual 4

Uploaded by

Anees
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab 04: Memory segmentation, Loops, Jumps

Introduction

In this lab, you will be introduced to memory segmentation, and difference


between physical and logical addresses of the memory. You will also deal with the
different types of addressing modes, and learn how to calculate the physical addresses
from logical addresses. You will also write programs that will make use of loops and jump
statements.

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.

Assembler directive that defines the memory model to be used in the .MODEL
program. The memory model determines the size of the code, stack and
data segments of the program
Assembler directive that reserves a memory space called stack .STACK
segment for use of program instructions (during interrupts and
function calls)
Assembler directive that reserves a memory space called data .DATA
segment for constants and variables being used in program
Assembler directive that reserves space called code segment for .CODE
storage of program instructions in memory
Assembler directive that finishes the assembly program END

.MODEL DIRECTIVE

A few assembly language memory models are tabulated on the basis of the physical
memory they require.

Model Size of Code and Data Memory model


Code and data no more than 64KB combined TINY
Code and data segments must be no more than 64KB each SMALL
Code can be more than 64KB, data still limited to no more than 64KB MEDIUM
Code limited to no more than 64KB, data can be more than 64KB COMPACT
Code and data can each be more than 64K, no array can be larger than LARGE
64KB
Code and data can each be more than 64KB, arrays can be larger than HUGE
64KB
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, 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

var1 DB 10

.code


end

In this case var1 is the offset address and data segments address is segment address so total physical
address would be

(Address of data segment) x 10H + (address of var1) = Physical 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.

Student Discussion marked in Red


Different memory addressing schemes are tabulated as follows.

Source operands Addressing


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

1. Assemble, Link and Run the above program.

2. Calculate both the logical and physical addresses of each instruction. Put the results on the
given table.

ADDRESSING MODE Logical Address Physical Address INSTRUCTION

MOV AX,@DATA
MOV DS,AX
LEA DX,MESSAGE1
MOV AH,09H
After INT 21H, IP = Before INT 21H, IP = INT21H
MOV AH, 0AH
MOV DX, OFFSET
BUF
After INT 21H, IP = Before INT 21H, IP = INT21H
LEA
DX,MESSAGE2

MOV AH,09H

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


LEA DX, BUF

MOV AH,09H

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


MOV AX,4C00H

Compare instruction:
Jump Instructions:

The jump instructions are used to transfer the flow of the process to the indicated operator.
When the jump address is within the same segment, the jump is called intra- segment jump.
When this address is outside the current segment, the jump is called inter- segment jump.
An overview of all the jump instructions is given in Table 5. 2.

Table 5.2: Jump Instructions of the 8086 Microprocessor

The LOOP Instructions:

The LOOP instruction is a combination of a DEC and JNZ instructions. It


causes execution to branch to the address associated with the LOOP
instruction. The branching occurs number of times equal to the number
stored in the CX register. Al LOOP instructions are summarized in Table 5.
2.
Table 5.2: Summary of the LOOP Instructions.

The Loop Program Structure, Repeat-Until and While-Do:

Like the conditional and unconditional jump instructions which can be used to
simulate the IF-Then-Else structure of any programming language, the Loop
instructions can be used to simulate the Repeat-Until and While-Do loops. These are
used as shown in the following (Table 5. 6).

Table 5.6: The Loop Program Structure.

TASK 2
Write a program that takes a number input between 0 and 9 and then
displays corresponding grade If grade is less than 5 it should display
Grade C If grade is less than 7 it should display Grade B If grade is
greater than or equal 7 it should display Grade A. The program should
continue to run until the user enters a negative number or a number
greater than 9. Attach screenshot of all cases.
TASK 3

Write a program that adds the following series and places results in AX.

• 95+90+85+…+5

You can also utilize the below formula

Where n=20 (No of terms)


a= 95 (First term)
l= 0 (Last term)

You might also like