Assembly Language Programming in 8086

 

In this article we will know about the 8086 assembler directives, and the the format of the assembly language program in 8086. Memory in 8086 systems is segmented, the segments being code segment, data segment, stack segment and extra segment. While writing the assembly the beginning and end of Different segments are to be Clearly indicated. This is done by segment directive. Also the declaration of data types Also should be in format understandable by the assembler. The following paragraph describes the some of the directives required for writing the 8086 assembly program.
So directive in 8086 are the instructions to the assembler, and are not converted into the machine language. Here we discuss various assembler directives:

  • ORG directive: This directive instructs the assembler to start the program in memory from the offset mentioned in the argument of ORG.

Example: ORG 100h will start the next instruction at an offset of 100h in the memory

  • Segment directives:

8086 uses the directive SEGMENT(segment) to identify the beginning of a segment. The name of the segment should precede the keyword SEGMENT (segment). A segment thus declared will contains  the code or the data depending on whether the declared segment is code segment or data segment. The words code or data or any other user defined names can be used to declare a particular segment. Its definition however will be Clear in the code segment when you assign a segment name to the segment register.
The name of the segment should be a unique without any Blank Spaces and may be up to 31 Characters long. Keywords cannot be used as name of the segment.

  • ENDS directive:

8086 make use of ENDS (ends) directive to mean the end of a segment. The keyword ENDS(ends) must be followed with the name of the segment to end.

  • END directive:

The end END(end) directive tell the assembler to stop reading and assembling the program after the end directive. The statements after the end directive will be ignored by the assembler. It should be used as the last statement in a program.

  • PROC directive– It is used to identify the start of a procedure. The term near or far is used to SPecify the type of the procedure.
  • ENDP directive:

The ENDP(endp) directive is used to tell the assembler about the end of the procedure. When using the procedures in a program we need to call them and then at one point the procedure will end and return to the cALling program. The example below CLarifies it.

Sum_2_number proc far ; This identifies that the start of the procedure and instructs the assembler that it is far procedure.
– –
– –
Sum_2_number ENDP

  • ASSUME directive:

SInce there can be 64 total segments in the .EXE format and of which at least four segments one of each type can be active at any time, the ASSUME (assume) directive tells the assembler which segments are active. The format of the ASSUME (assume) directive is :
ASSUME CS:cs_name, DS: ds_name, SS: ss_name, ES: es_name

Segment initialization

The next statement to Assume directive should be instructions. The Different segments can beinitializes as follows:
Suppose that a data segment has been created with ds_segment as name of segment then the next statement to assume directive will be:
MOV AX, ds_segment ; load offset of ds_segment in register AX
MOV DS, AX; copy the ds_segmet offset in DS, so DS points to 0000s

  • OFFSET directive:

The offset directive will be used where the offset from starting of the segment is required in the program. The example of OFFSET is:
MOV SI, OFFSET string1; copies the offset of string1 in SI register

  • Data declaration directives

8086 supports Different data types. The data types unlike in high level language which use keywords like INT, char, float etc., 8086 treats everything as a bytes and according have directives for declaring type for Single byte, two byte ( a word), double word, quad words, ten bytes etc, These are-
DB – Defined Byte. DB declares a variable of type byte and reserves one location in memory for the variable of type byte.

Example
num1 DB 15h, Char1 db ‘A’
numbers db 100 dup(0); Reserve an array of 50 words of memory and initialize All bytes with 00. Array is named as numbers.

DD– Defined double Word, DW declares a variable of type byte and reserves one location in memory for the variable of type byte.

Examples:
num1 DW 1234h
ARR1 DW 1A3Bh, 3A4Bh, 5A6CH ; this declares an array of 3 words and initialized with Specified values.
ARR2 DW 50 DUP(‘0’); Reserve an array of 50 words of memory and initialize All words with 0000. Array is named as ARR2.

DQ-Define Quadword

This directive is used to define a variable of type quadword or to reserve storage location of type quadword in memory. For example
Quad_word DQ 1234123412341234H

DT-Define Ten Bytes

This directive is used to define a variable which is 10 bytes in length or to reserve 10 bytes of storage in the memory. For example
Ten_bytes DT 11223344556677889900

DUP directive: DUP directive is used to duplicate the basic data definition ‘n’ number of times; or saying it the other way is that DUP is used to declare array of Size n.

ARRY DB 10 dup (0), declares array of 10 byte.

EQU – This EQU directive is used to give a name to some value or to a symbol. Each time the assembler finds the name in the program, it will replace the name with the value or symbol you given to that name.

Example
MULTIPLIER EQU 05H ; then subsequently where multiplication by 05h I required we can use this as follows
MUL AL, MULTIPLIER;
The advantage of using EQU in this manner is, if MULTIPLIER is used many no of times in a program and you want to Change the value, All you had to do is Change the EQU statement at
beginning, it will changes the rest of All.

EVEN directive : This directive is used to align the word boundary to next even address for faster access. Suppose we have a data segment named as DS_segment, then:

DS_segment SEGMENT

Num1 db 05h
Word1 dw 1234h

Char db ‘x’; Location counter will point to 0004 when assembler reads next statement
Prices DB 5 DUP(?) ;An array of 5 bytes, after executing this PC will point to 09h which is an odd address
EVEN ; increment location counter to 000AH i.e. an even address
MyData DW 50 DUP(0) ;Array of 50 words will start from an even address.

DS-segment ENDS

  • PUBLIC directive: This directive is used to instruct the assembler that a Specified name or label will be accessed from other modules.

Example: PUBLIC MULIPLIER, INTEREST_RATE

Format of Assembly Language Program 

There are two formats for the assembly language program in 8086, these are:

  1. .COM Format
  2. .EXE Format

.COM format: In your basic computer fundamental course you must have studied about a COMMAND.COM dos utility. Well a .COM program is just a binary image of the assembly language program. This program is loaded in memory at the lowest available segment address beginning at an offset of 100h, the first 1k byte of the memory is used by the interrupt vector table (IVT).
A .COM program is a small utility program which is o Size less than 64KB, so the program including its code, data and stack All fit within a same segment i.e code segment. The code segment in such program is declared explicitly using the segment directive. A sample program of the .COM type is shown below.

CSEG SEGMENT

ASSUME CS: CSEG, DS:CSEG, SS: CSEG

ORG 100h

START:       MOVE AX, CSEG

MOV DS, AX
MOV AL, NUM1
MOV BL, NUM2
ADD AL, BL
MOV ANS, AL
– –
– –
– –
MOV AX, 4C00h
INT 21h
NUM1
NUM2
ANS

CSEG ENDS

END START

.EXE format: The .EXE programs are the utility which are larger than 64KB in Size. And times the Size of the program may be so large that even up to 64 segments may be required but however at any time only four segments will be active. Thus these programs use more than one segment and use code segment for the code, data segment for storing the data and stack or even a separate stack segment.  In this paragraph we present the same example but written in the .EXE format. The EXE header is of 256 bytes long and contains information which is necessary for cALculating the segment and the offset address etc.

.module
.stack

Data segment

Num1 db 5h
Single_Char db ‘5’
Word1 dw 1234h
Word2 dw “AB”

Data ends

Code segment

Assume CS: code, DS: data, SS:stk ES: extra

Start: MOV AX, data
MOV DS, AX
MOV ES, AX;if same data segment is Also used as extra segment
MOV AL, num1
MOV BL, Single_Char
– –
MOV AX, 4C00h
INT 21h

Code ends

end start

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!