Modular Program Development and Assembler Directives

Modular Program Development and Assembler Directives

index

1. What is modular programming?

Ans. Instead of writing a large program in a single unit, it is better to write small programs— which are parts of the large program. Such small programs are called program modules or simply modules. Each such module can be separately written, tested and debugged. Once the debugging of the small programs is over, they can be linked together. Such methodology of developing a large program by linking the modules is called modular programming.

2. What are data coupling and control coupling?

Ans. Data coupling refers to how data/information are shared between two modules while control coupling refers to how the modules are entered and exited. Coupling depends on several factors like organisation of data as also whether the modules are assembled together or separately. The modular approach should be such that data coupling be minimized while control coupling is kept as simple as possible.

3. How modular programming helps assemblers?

Ans. Modular programming helps assembly language programming in the following ways :

z Use of macros-sections of code.

z Provide for procedures—i.e., subroutines.

z Helps data structuring such that the different modules can access them.

4. What is a procedure?

Ans. The procedure (or subroutine) is a set of codes that can be branched to and returned from.

The branch to a procedure is known as CALL and the return from the procedure is

known as RETURN.

The RETURN is always made to the instruction just following the CALL, irrespective

of where the CALL is located.

Procedures are instrumental to modular programming, although not all modules are

procedures. Procedures have one disadvantage in that an extra code is needed to link

them— normally referred to as linkage.

The CALL instruction pushes IP (and CS for a far call) onto the stack. When using procedures, one must remember that every CALL must have a RET. Near calls require

near returns and far calls require far returns.

5. What are the two types of procedures? Ans. There are two types of procedures. They are:

z Those that operate on the same set of data always.

z Those that operate on a new set of data each time they are called.

6. How are procedures delimited within the source code?

Ans. A procedure is delimited within a source code by placing a statement of the form

< Procedure name > Proc < attribute > at the beginning of the procedure and the statement < Procedure name > ENDP at the end.

The procedure name acts as the identifier for calling the procedure and the attribute can be either NEAR or FAR—this attribute determines the type of RET statement.

7. Explain how a procedure and data from another module can be accessed.

Ans. A large program is generally divided into separate independent modules. The object codes for these modules are then linked together to generate a linked/executable file.

The assembly language directives: PUBLIC and EXTRN are used to enable the linker to access procedure and data from different modules. The PUBLIC directive lets the linker know that the variable/procedure can be accessed from other modules while the EXTRN directive lets the assembler know that the variable/procedure is not in the existing module but has to be accessed from another module. EXTRN directive also provides the linker with some added information about the procedure. For example,

EXTRN ROUTINE : FAR, TOKEN : BYTE

indicates to the linker that ROUTINE is a FAR procedure type and that TOKEN is a variable having type byte.

8. Discuss the technique of passing parameters to a procedure.

Ans. When calling a procedure, one or more parameters need to be passed to the procedure— an example being delay parameter. This parameter passing can be done by using one of the CPU registers like,

MOV CX, T

CALL DELAY

where, T represents delay parameter.

A second technique is to use a memory location like,

MOV TEMP, T

CALL DELAY

where, TEMP is representative of memory locations.

A third technique is to pass the address of the memory variable like,

MOV SI, POINTER

CALL DELAY

while in the procedure, it extracts the delay parameter by using the instruction MOV

CX, [SI].

This way an entire table of values can be passed to a procedure. The above technique has the inherent disadvantage of a register or memory location being dedicated to hold the parameter when the procedure is called. This problem becomes more prominent when using nested procedures. One alternative is to use the stack to relieve registers/memory locations being dedicated like,

MOV CX, T

PUSH CX

CALL DELAY

The procedure then can pop off the parameters, when needed.

9. Explain the term Assembler Directive.

Ans. There are certain instructions in the assembly language program which are not a part of the instruction set. These special instructions are instructions to the assembler, linker and loader and control the manner in which a program assembles and lists itself. They come into play during the assembly of a program but do not generate any executable machine code.

As such these special instructions—which, as told, are not a part of the instruction set —are called assembler directives or pseudo-operations.

10. Give a tabular form of assembler directives.

Ans. Table 16.1 gives a summary of assembler directives.

Table 16.1: Summary of assembler directives

Directive

Action

ALIGN

ASSUME

COMMENT

DB

DW

DD

DQ

DT

END

ENDM

ENDP

ENDS

EQU

EVEN

EXITM

EXTRN

LABEL

LOCAL

MACRO

MODEL

 

aligns next variable or instruction to byte which is multiple of operand

selects segment register(s) to be the default for all symbol in segment(s)

indicates a comment

allocates and optionally initializes bytes of storage

allocates and optionally initializes words of storage

allocates and optionally initializes doublewords of storage

allocates and optionally initializes quadwords of storage

allocates and optionally initializes 10-byte-long storage units

terminates assembly; optionally indicates program entry point

terminates a macro definition

marks end of procedure definition

marks end of segment or structure

assigns expression to name

aligns next variable or instruction to even byte

terminates macro expansion

indicates externally defined symbols

creates a new label with specified type and current location counter

declares local variables in macro definition

starts macro definition

specifies mode for assembling the program

11. Explain the following assembler directives (a) CODE (b) ASSUME (c) ALIGN Ans. (a) CODE

It provides a shortcut in the definition of the code segment. The format is Code

[name]

Here, the ‘name’ is not mandatory but is used to distinguish between different code

segments where multiple type code segments are needed in a program.

(b) ASSUME

The four physical segments viz., CS, DS, SS and ES can be directly accessed by 8086

at any given point of time. Again 8086 may contain a number of logical segments which can be assigned as physical segments by the ASSUME directive. For example ASSUME CS: Code, DS : Data, SS : Stack

(c) ALIGN

This directive forces the assembler to align the next segment to an address that is

divisible by the number that follows the ALIGN directive. The general format is

ALIGN number

where number = 2, 4, 8, 16

ALIGN 4 forces the assembler to align the next segment at an address that is

divisible by 4. The assembler fills the unused byte with 0 for data and with NOP for

code.

Normally, ALIGN 2 is used to start a data segment on a word boundary while

ALIGN 4 is used to start a data segment on a double word boundary.

12. Explain the DATA directive.

Ans. It is a shortcut definition to data segments. The directives DB, DW, DD, DR and DT are used to (a) define different types of variables or (b) to set aside one or more storage locations in memory-depending on the data type

DB — Define Byte

DW — Define Word

DD — Define Double word

DQ — Define Quadword

DT — Define Ten Bytes

ALPHA DB, 10 H, 16 H, 24 H; Declare array if 3 bytes names; ALPHA

13. Explain the following assembler directives : (a) DUP (b) END (c) EVEN

Ans. (a) DUP: The directive is used to initialise several locations and to assign values to these locations. Its format is: Name Data-Type Num DUP (value)

As an Example:

TABLE DOB 20 DUP(0) ; Reserve an array of 20

; bytes of memory and initialise all 20

; bytes with 0. Array is named TABLE

(b) END: This directive is put in the last line of a program and indicates the assembler that this is the end of a program module. Statement, if any, put after the END directive is ignored. A carriage return is obviously required after the END directive.

(c) EVEN: This directive instructs the assembler to advance its location counter in such a manner that the next defined data item or label is aligned on an even storage boundary. It is very effectively used to access 16 or 32-bits at a time. As an example. EVEN LOOKUP DW 10 DUP (0) ; Declares the array of 10 words

; starting from an even address.

14. Discuss the MODEL directive.

Ans. This directive selects a particular standard memory model. Each memory model is characterised by having a maximum space with regard to availability of code and data. This different models are distinguished by the manner by which subroutines and data are reached by programs.

Table 16.2 gives an idea about the different models with regard to availability of code and data.

Table16.2: The different models

Model

Code segments

Data segments

Small Medium Compact Large

One Multiple One Multiple

One One Multiple Multiple

15. Give a typical program format using assembler directives.

Ans. A typical program format using assembler directives is as shown below:

Line 1.

Line 2.

MODEL SMALL DATA

;

;

selects small model indicates data segment

Line 15.

CODE

;

indicates start of code segment

body of the program

Line 20.

END

;

End of file

16. Discuss the PTR directive.

Ans. This directive assigns a specific type to a variable or a label and is used in situations where the type of the operand is not clear. The following examples will help explain the PTR directive more elaborately.

(a) The instruction INC [BX] does not tell the assembler whether to increment a byte or word pointed to by BX. This ambiguity is cleared with PTR directive.

INC BYTE PTR [BX] ; Increment the byte pointed to by [BX] INC WORD [BX] ; Increment the word pointed to by [BX]

(b) An array of words can be accessed by the statement WORDS, as for examples WORDS DW 1234 H, 8823 H, 12345 H, etc.

But PTR directive helps accessing a byte in an array, like, MOV AH, BYTE PTR WORDS.

(c) PTR directive finds usage in indirect jump. For an instruction like JMP [BX], the assembler cannot decide of whether to code the instruction for a NEAR or FAR jump. This difficulty is overcome by PTR directive.

JMP WORD PTR [BX] and JMP DWORD PTR [BX] are examples of NEAR jump and FAR jump respectively.

17. What is a macro?

Ans. A macro, like a procedure, is a group of instructions that perform one task. The macro instructions are placed in the program by the macro assembler at the point it is invoked.

Use of macros helps in creating new instructions that will be recognised by the assembler. In fact libraries of macros can either be written or purchased and included in the source code which apparently expands the basic instruction set of 8086.

18. Show the general format of macros. Ans. The general format of a macro is

NAME MACRO Arg 1 Arg 2 Arg 3

Statements ……….

……..

ENDM

The format begins with NAME which is actually the name assigned to the MACRO.

‘Arg’s’ represent the arguments of the macro. Arguments are optional in nature and

allows the same macro to be used in different places within a program with different sets

of data. Each of the arguments represent a particular constant, hence a CPU register,

for instance, cannot be used.

All macros end with ENDM.

19. Explain macro definition, macro call and macro expansion.

Ans. Creation of macro involves insertion of a new opcode that can be used in the program.

This code, often called prototype code, along with the statements for representing and

terminating a macro is called macro definition.

The statements that follow a macro definition is called macro call.

When the assembler encounters a macro call, it replaces the call with macro’s code.

This replacement action is referred to as macro expansion.

20. Explain the INCLUDE file.

Ans. A special file, say MACRO.LIB can be created which would contain the definitions of all macros of the user. In such a case, the writing of each macro’s definition at the head of the main program can be dispensed with. The INCLUDE file may look like.

INCLUDE MACRO.LIB

This statement forces the assembler to automatically include all the statements in the MACRO.LIB.

Sometimes it may be undesirable to include the INCLUDE statement when the INCLUDE file is very long and the user may not be using many of the macros in the file.

21. Explain local variables in a macro.

Ans. Within the body of a macro, local variables can be used. A local variable can be defined by using LOCAL directive and is available within the macro and not outside.

For example, a local variable can be used in a jump address. The jump address has to be defined as a local, an error message will be outputted by the assembler.

Local variable(s) must be defined immediately following the macro directive, with the help of local directives.

22. Explain Controlled Expansion (also called Conditional Assembly).

Ans. While inside the macro, facilities are available to either accept or reject a code during macro execution— i.e., expansion of a macro prototype code would depend on the type(s) of actual parameter(s) passed to it by the call. This facility of selecting a code that is to be assembled is called controlled expansion.

The conditional assembly statements in macro are: IF-ELSE-ENDIF Statement

REPEAT Statement

WHILE Statement

FOR Statement

23. For the conditional assembly process, show the (a) forms used for the IF statement (b) relational operators used with WHILE and REPEAT.

Ans. Figure 16.1 and 16.2 show respectively the forms used for the IF statement and the relational operators used with WHILE and REPEAT.

Statement

Function

IF

IFB

IFE

OFDEF

IFNB

IFNDEF

IFIDN

IFDIFWWW

If the expression is true If argument is blank

If the expression is not true

If the label has been defined

If argument is not blank

If the label has not been defined

If argument 1 equals argument 2

If argument 1 does not equal argument 2

Fig.16.1: Forms used for IF statements

Operator

Function

EQ

Equal

NE

Not Equal

LE

Less than or Equal

LT

Less than

GT

Greater than

GE

Greater than or Equal

NOT

Logical inversion

AND

Logical AND

OR

Logical OR

XOR

Logical XOR

Fig.16.2: Relational operators used with WHILE and REPEAT

24. Distinguish between macro and procedure.

Ans. A procedure is invoked with a CALL instruction and terminated with a RET instruction.

Again the code for the procedure appears only once in the programs—irrespective of the

number of times it appears.

A macro is invoked, on the other hand, during program assembly and not when the

program is run. Whenever in the program the macro is required, assembler substitutes

the defined sequence of instructions corresponding to the macro. Hence macro, if used

quite a few number of times, would consume a lot of memory space than that would be

required by procedure.

Macro does not require CALL–RET instructions and hence will be executed faster.

Sometimes, depending on the macro size, the macro may require less number of codes

than is required by the equivalent procedure.

Leave a comment

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