Module4 8086 Microprocessor and Peripherals part3 .

4.3.1 Interrupt Programming

Program 4.3

Write a program to a create a file RESULT and store in it 500H bytes from the memory block starting at 1000:1000, if either an interrupt occurs at INTR pin with Type 0AH or an instruction equivalent to the above interrupt is executed.

ASSUME CS:CODE, DS:DATA DATA SEGMENT

FILENAME DB “RESULT”, “$”

MESSAGE DB “FILE WASN’T CREATED SUCCESSFULLY” , 0AH,0DH,”$”

DATA ENDS CODE SEGMENT

START : MOV AX,CODE

MOV DS,AX ; Set DS at code for setting IVT MOV DX,OFFSET ISR0A ; Set DX at offset of ISR0A. MOV AX,250AH ;Set IVT using function value 250AH INT 21H ; in AX under INT 21H

MOV DX,OFFSET FILENAME ; Set pointer to filename. MOV AX,DATA ; Set the DS at DATA for filename. MOV DS,AX

MOV CX,00H

MOV AH,3CH ; Create file with the filename ‘RESULT’ INT 21H

JNC FURTHER ; If no carry, create operation is successful MOV DX,OFFSET MESSAGE ; else display message

MOV AH,09H INT 21H

JMP STOP

FURTHER: INT 0AH ; If the file is created successfully,

MOV AH,4CH ; write into it and return to DOS prompt

INT 21H ISR0A PROC NEAR

MOV BX,AX ; Take file handle in BX, MOV CX,500H ; Byte count in CX MOV DX,1000H ; Offset of block in DX

MOV AX,1000H ; Segment value of block MOV DS,AX ; in DS

MOV AH,40H ; Write in the file and INT 21H ; return

ISR0AH ENDP CODE ENDS END START

Program 4.4

Write a program that gives display ‘IRT2 is OK’ if a hardware Signal appears on IRQ2 pin and ‘IRT3 is OK’ if it appears on IRQ3 pin of PC IO channel.

ASSUME CS:CODE, DS:DATA DATA SEGMENT

MSG1 DB “IRT2 is OK”,0AH,0DH,”$” MSG2 DB “IRT3 is OK”,0AH,0DH,”$” DATA ENDS

CODE SEGMENT

START : MOV AX,CODE

MOV DS,AX ; Set IVT for Type 0AH MOV DX, OFFSET ISR1

MOV AX,250AH ; IRQ2 is equivalent to Type 0AH INT 21H

MOV DX, OFFSET ISR2 ; Set IVT for Type 0BH

MOV AX,250BH ; IRQ3 is equivalent to Type 0BH INT 21H

HERE: JUMP HERE

ISR1 and ISR2 display the message

ISR1 PROC LOCAL MOV AX,DATA MOV DS,AX

MOV DX,OFFSET MSG1 ; Display message MSG1 MOV AH,09H

INT 21H IRET

ISR1 ENDP

ISR1 and ISR2 display the message

ISR1 PROC LOCAL MOV AX,DATA MOV DS,AX

MOV DX,OFFSET MSG1 ; Display message MSG1 MOV AH,09H

INT 21H IRET

ISR1 ENDP

ISR2 PROC LOCAL MOV AX,DATA MOV DS,AX

MOV AX,OFFSET MSG2 ; Display message MSG2 MOV AH,09H

INT 21H IRET

ISR2 ENDS END START

ISR2 PROC LOCAL

MOV AX,DATA MOV DS,AX

MOV AX,OFFSET MSG2 ; Display message MSG2 MOV AH,09H

INT 21H IRET

ISR2 ENDS END START

4.5 MACROS

4.5.1 Disadvantages of Procedure

1. Linkage associated with them.

2. It sometimes requires more code to program the linkage than is needed to perform the task. If this is the case, a procedure may not save memory and execution time is considerably increased.

3. Hence a means is needed for providing the programming ease of a procedure while avoiding the linkage. This need is fulfilled by Macros.

Macro is a segment of code that needs to be written only once but whose basic structure can be caused to be repeated several times within a source module by placing a single statement at the point of each reference.

A macro is unlike a procedure in that the machine instructions are repeated each time the macro is referenced.Therefore, no memory is saved, but programming time is conserved (no linkage is required) and some degree of modularity is achieved. The code that is to be repeated is called the prototype code. The prototype code along with the statements for referencing and terminating is called the macro definition.

Once a macro is defined, it can be inserted at various points in the program by using macro calls. When a macro call is encountered by the assembler, the assembler replaces the call with the macro code. Insertion of the macro code by the assembler for a macro call is referred to as a macro expansion.

In order to allow the prototype code to be used in a variety of situations, macro definition and the prototype code can use dummy parameters which can be replaced by the actual parameters when the macro is expanded. During a macro expansion, the first

actual parameter replaces the first dummy parameter in the prototype code, the second actual parameter replaces the second dummy parameter, and so on.

4.5.1 ASM-86 Macro Facilities

The macro definition is constructed as follows :

image

Macro name has to begin with a letter and can contain letters, numbers and underscore characters. Dummy parameters in the parameter list should be separated by commas. Each dummy parameter appearing in the prototype code should be preceded by a % character. Consider an example that shows the definition of macro for multiplying 2 word operands and storing the result which does not exceed 16 bit.

A macro call has the form

%Macro name (Actual parameter list)

with the actual parameters being separated by commas.

%MULTIPLY (CX,VAR,XYZ[BX]

Above macro call results in following set of codes.

PUSH DX PUSH AX MOV AX,CX IMUL VAR

MOV XYZ[BX],AX POP AX

POP DX

It is possible to define a macro with no dummy parameters, but in this case the call must not include any parameters. Consider a macro for pushing the contents at beginning of a procedure.

Macro definition consists of

%*DEFINE(SAVEREG) ( PUSH AX

PUSH BX PUSH CX PUSH DX

)

This macro is called using the statement

%SAVEREG

The above macro can be called at the beginning of the each procedure to save the register contents. A similar macro could be used to restore the register contents at the end of each procedure.

%*DEFINE (RESTORE) ( POP DX

POP CX POP BX POP AX

)

4.5.2 Local Labels

Consider a macro called ABSOL which makes use of labels. This macro is used to replace the operand by its

absolute value.

%*DEFINE(ABSOL(OPER)) ( CMP %OPER,0

JGE NEXT NEG %OPER

NEXT: NOP

)

When the macro ABSOL is called for the first time, the label NEXT will appear in the program and, therefore it becomes defined. Any subsequent call will cause NEXT to be redefined. This will result in an error during assembly process because NEXT ha been

associated with more than one location. One solution to this problem would be to have NEXT replaced by a dummy parameter for the label. This would require the programmer to keep track of dummy parameters used. One solution to this problem is the use of Local Labels.

Local labels are special labels that will have suffixes that gets incremented each time the macros are called. These suffixes are two digit numbers that gets incremented by one starting from zero. Labels can be declared as local label by attaching a prefix Local.

Local List of Local labels at the end of first statement in the macro definition. Consider a macro which makes use local labels.

%*DEFINE(ABSOL(OPER)) LOCAL NEXT ( CMP %OPER,0

JGE %NEXT NEG %OPER

%NEXT:NOP

)

If this macro is called twice using %ABSOL(VAR) and %ABSOL(BX) would result in following set of codes.

CMP VAR,0 JGE NEXT00 NEG VAR

NEXT00: NOP

.

CMP BX,0 JGE NEXT01 NEG VAR

NEXT01: NOP

4.5.3 Nested Macros

It is possible for a macro call to appear within a macro definition. This is referred to as Macro nesting. The limitation of nested macros is that all macros included in the definition of a given macro must be defined before the given macro is called.

%*DEFINE(DIFSOR(OPR1,OPR2,RESULT))

( PUSH DX PUSH AX

%DIF(%OPR1,%OPR2) IMUL AX

MOV %RESULT,AX POP AX

POP DX

)

%*DEFINE(DIF(X,Y))

MOV AX,%X SUB AX,%Y

.

.

%DIFSOR(VAR1,VAR2,ERROR)

.

.

This results in following set of codes PUSH DX

PUSH AX

MOV AX,VAR1 SUB AX,VAR2 IMUL AX

MOV ERROR,AX POP AX

POP DX

Program 4.5

A program using Macro for saving the contents of GPRs in the stack.

ASSUME CS:CODE, DS:DATA DATA SEGMENT

SAVEREG MACRO PUSH AX

PUSH BX PUSH CX PUSH DX

ENDM

DATA ENDS CODE SEGMENT

START : MOV AX,DATA

MOV DS,AX MOV AX,1234H MOV BX,2345H MOV CX,3456H MOV DX,4567H SAVEREG MOV AH,4CH INT 21H

CODE ENDS END START

• This program executes following set of codes MOV AX,DATA

MOV DS,AX MOV AX,1234H MOV BX,2345H MOV CX,3456H MOV DX,4567H PUSH AX PUSH BX PUSH CX PUSH DX

MOV AH,4CH INT 21H

Leave a comment

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