0% found this document useful (0 votes)
8 views12 pages

Exe Format

The document is a lab manual for a course on Computer Organization and Assembly Language, focusing on the 8086 architecture. It covers the structure of an EXE-format program, including data, code, and stack segments, as well as directives and memory models. Additionally, it provides example assembly programs and lab tasks for students to practice their skills in writing and understanding assembly language.

Uploaded by

mugheesulhaq0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

Exe Format

The document is a lab manual for a course on Computer Organization and Assembly Language, focusing on the 8086 architecture. It covers the structure of an EXE-format program, including data, code, and stack segments, as well as directives and memory models. Additionally, it provides example assembly programs and lab tasks for students to practice their skills in writing and understanding assembly language.

Uploaded by

mugheesulhaq0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Computer Organization and Assembly Lan-

guage
Lab Manual

Topic: Program segment directives, Memory model code segment, Data segment, Stack
segment, Data variable, Program segment structure, the template of an EXE-format program

Course Instructor:
Lab Instructor:

Session:

School of Systems and Technology


UMT Lahore Pakistan
Objectives: INTRODUCTION to Exe template of 8086 and program structure

Basic Structure

1. Data Segment: This section is used to define and store data. Variables are
declared here.
2. Code Segment: This section contains the instructions that the CPU executes.

Example Program

Below is a simple program in assembly language using Emu8086 that adds two
numbers and stores the result in a variable.

; Sample Assembly Program

.model small
.stack 100h

.data
num1 db 5 ; Define num1 and assign it the value 5
num2 db 3 ; Define num2 and assign it the value 3
result db ? ; Define result with an undefined initial value

.code
main proc
mov ax, @data
mov ds, ax ; Initialize data segment

mov al, num1 ; Load num1 into register AL


add al, num2 ; Add num2 to AL
mov result, al ; Store the result in the variable result

mov ax, 4C00h ; Terminate the program


int 21h ; DOS interrupt to exit

main endp
end main
Explanation of the Example Program

1. Header and Segments


o .model small: Defines the memory model. Here, small indicates that
the program will use a single data segment and a single code segment.
o .stack 100h: Reserves 256 bytes for the stack.

2. Data Segment
o .data: Begins the data segment.
o num1 db 5: Defines a byte-sized variable num1 with an initial value of
5.
o num2 db 3: Defines a byte-sized variable num2 with an initial value of
3.
o result db ?: Defines a byte-sized variable result with an undefined ini-
tial value (?).

3. Code Segment
o .code: Begins the code segment.
o main proc: Defines the start of the main procedure.
o mov ax, @data: Loads the address of the data segment into the AX
register.
o mov ds, ax: Initializes the DS register with the address of the data seg-
ment.
o mov al, num1: Loads the value of num1 into the AL register.
o add al, num2: Adds the value of num2 to the AL register.
o mov result, al: Stores the value of the AL register (which now contains
the sum) into the result variable.
o mov ax, 4C00h: Prepares to terminate the program by moving the ter-
minate code into AX.
o int 21h: Calls DOS interrupt 21h to terminate the program.

4. End of Program
o main endp: Marks the end of the main procedure.
o end main: Indicates the end of the program and specifies the entry
point.

Memory Model for 8086 ISA

Program Segment Directives


The key simplified segment directives are STACK_SEG, CODE_SEG,
DATA_SEG, .MODEL.
Memory Models

The .MODEL directive specifies the memory model for an assembler module
that uses the simplified segment directives. The .MODEL directive must
precede .CODE, .DATA, and .STACK. The format of the .MODEL directive is:

.MODEL memory model


The memory model can be TINY, SMALL, COMPACT, MEDIUM, LARGE and
HUGE.

TINY One segment. Thus both program code and data together must
fit within the same 64 Kb segment. Both code and data are near.

SMALL Program code must fit within a single 64 Kb segment, and data
must fit within a separate 64 Kb segment. Both code and data
are near.

MEDIUM More than one code-segment. One data-segment. Thus code


may be greater than 64K.

COMPACT One code-segment. More than one data-segment. Thus data may
be greater than 64K.

LARGE More than one code-segment. More than one data-segment. No


array larger than 64K. Thus both code and data may be greater
than 64K.

HUGE More than one code-segment. More than one data-segment.


Arrays may be larger than 64K. Thus both code and data may be
greater than 64K

All program models but TINY result in the creation of exe-format programs. The
TINY model creates com-format programs.

Stack Segment
STACK_SEG defines the stack segment and controls the size of the stack. For ex-
ample,

STACK_SEG segment STACK 200h


Defines stack 200h (512) bytes long.

Code Segment

CODE_SEG marks the start of your program’s code segment. It contains exe-
cutable instruction.

Main PROC

; Here the lines of executable instructions.

Main endp

CODE_SEG ends

Data Segments

DATA_SEG marks the start of your data segment. You should place your memory
variables in this segment. For example,

DATA_SEG segment ‘data’


Coal db "Welcome to coal"
DATA_SEG ends

• CS: points to the beginning of the code segment


• DS: points to the beginning of the data segment
• SS: points to the beginning of the stack segment
The Intel 8086 Registers
Accumulator register (AX) Accumulator can be used for Arithmetic, I/O
operations and string manipulation.
Base register (BX) BX register usually contains a data pointer used for based,
based indexed or register indirect addressing.
Count register (CX) Count register can be used as a counter in string
manipulation and shift/rotate instructions.
Data register (DX) Data register can be used as a port number in I/O
operations. In integer 32-bit multiply and divide instruction the DX register
contains high-order word of the initial or resulting number.
The following registers are both general and index registers:
Stack Pointer (SP) is a 16-bit register pointing to program stack.
Base Pointer (BP) is a 16-bit register pointing to data in stack segment. BP
register is usually used for based, based indexed or register indirect addressing.
Source Index (SI) is a 16-bit register. SI is used for indexed, based indexed
and register indirect addressing, as well as a source data addresses in string
manipulation instructions.
Destination Index (DI) is a 16-bit register. DI is used for indexed, based
indexed and register indirect addressing, as well as a destination data addresses
in string manipulation instructions.
Other registers:
Instruction Pointer (IP) is a 16-bit register.
Flag register is a 16-bit register.

DATA VARIABLES
symbolic addresses of data items (offsets in the data segment)
Defined by directives DB, DW, DD, DQ
Format for data definition

]name] Dx expression
Coal DB “this is my first program”

Directive Dx:
 determines the variable type (according to the letter x)
 allocates the space in memory (one or more bytes)
 Initializes the contents of the memory locations (does not Initialize, if
the expression is?)

Expression:
can be uninitialized : ?
Can be assigned a constant: such as 25, 21.
Example:
DATAZ DB 21

Allocated Variable type Variable may contain


Directive memory
size in bytes

Signed integer in the range


DB 1 BYTE <-128; 127>
Unsigned integer in the range
<0; 255> Character
Signed integer in the range
DW 2 WORD <-32 768; 32 767>
Unsigned integer in the range
<0; 65 535> 16-bit offset
Signed integer
DD 4 Double Unsigned integer
Word Single precision floating point
number in the range about
±10^38
Far pointer in 16-bit mode, i.e.
address in the segment: offset
form 32-bit offset
Signed integer
DQ 8 Quad Word Unsigned integer
Double precision floating point
number in the range about
±10^308
Comments

 anything following ; on a line


 ; This stuff would be considered a comment

Program Segment Structure

 Select a memory model

 Define the stack size

 Define data segment

 Declare variables

 Define code segment

 Write code

 organize the procedure

 Mark the end of the source file

 define the entry point

Example

Program for the 8086 that prints your name using DOS interrupts:

.model small
.stack 100h

.data
nam db 'Name: John Doe', 13, 10, '$' ; Define name string with carriage return and line feed

.code
main proc
mov ax, @data
mov ds, ax ; Initialize data segment
; Print name
lea dx, nam ; Load address of name into DX
mov ah, 09h ; DOS interrupt function 09h (print string)
int 21h ; Call DOS interrupt

mov ax, 4C00h ; Terminate the program


int 21h ; DOS interrupt to exit

main endp
end main

Explanation

1. Data Segment
o .data: Begins the data segment.
o name db 'Name: John Doe', 13, 10, '$': Defines the string for the name
with a carriage return (13) and line feed (10) to move to the
next line, terminated by $.

2. Code Segment
o .code: Begins the code segment.
o main proc: Defines the start of the main procedure.
o mov ax, @data: Loads the address of the data segment into the
AX register.
o mov ds, ax: Initializes the DS register with the address of the
data segment.

3. Printing the String


o lea dx, name: Loads the effective address of the name string into
the DX register.
o mov ah, 09h: Sets AH to 09h to select the DOS interrupt func-
tion for printing a string.
o int 21h: Calls DOS interrupt 21h to print the string.

4. Terminate Program
o mov ax, 4C00h: Prepares to terminate the program by moving
the terminate code into AX.
o int 21h: Calls DOS interrupt 21h to terminate the program.
Important Note
 Carriage Return (CR): The ASCII value for CR is 13 (0x0D in hexadecimal). It
moves the cursor to the beginning of the current line without advancing to the next
line. It’s like pressing the "Enter" key but without moving down to a new line.

 Line Feed (LF): The ASCII value for LF is 10 (0x0A in hexadecimal). It moves
the cursor down to the next line without returning to the beginning of the line. It's
like pressing the "down arrow" key.

LAB TASKS

Task # 1:
Write an assembly language program using 8086 syntax.
1. Print your Name
2. Print your Registration ID
3. Print your section
4. Output in following format:

Name: xyz
ID: 1234
Section: B
Solution
.model small
.stack 100h

.data
nam db 'Name: John Doe', 13, 10, '$' ; Define name string with carriage return and line feed
id db 'ID: 1234', 13, 10, '$' ; Define ID string with carriage return and line feed
section db 'Section: B', 13, 10, '$' ; Define section string with carriage return and line feed

.code
main proc
mov ax, @data
mov ds, ax ; Initialize data segment

; Print name
lea dx, nam ; Load address of name into DX
mov ah, 09h ; DOS interrupt function 09h (print string)
int 21h ; Call DOS interrupt

; Print ID
lea dx, id ; Load address of ID into DX
mov ah, 09h ; DOS interrupt function 09h (print string)
int 21h ; Call DOS interrupt

; Print section
lea dx, section ; Load address of section into DX
mov ah, 09h ; DOS interrupt function 09h (print string)
int 21h ; Call DOS interrupt

mov ax, 4C00h ; Terminate the program


int 21h ; DOS interrupt to exit

main endp
end main

Explanation

1. Data Segment
o .data: Begins the data segment.
o name db 'Name: John Doe', 13, 10, '$': Defines the string for the name with a
carriage return (13) and line feed (10) to move to the next line, termi-
nated by $.
o id db 'ID: 1234', 13, 10, '$': Defines the string for the ID.
o section db 'Section: B', 13, 10, '$': Defines the string for the section.

2. Code Segment
o .code: Begins the code segment.
o main proc: Defines the start of the main procedure.
o mov ax, @data: Loads the address of the data segment into the AX regis-
ter.
o mov ds, ax: Initializes the DS register with the address of the data seg-
ment.

3. Printing Strings
o lea dx, name: Loads the effective address of the name string into the DX
register.
o mov ah, 09h: Sets AH to 09h to select the DOS interrupt function for
printing a string.
o int 21h: Calls DOS interrupt 21h to print the string.
o The same steps are repeated for the id and section strings.

4. Terminate Program
o mov ax, 4C00h: Prepares to terminate the program by moving the termi-
nate code into AX.
o int 21h: Calls DOS interrupt 21h to terminate the program.

You might also like