Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Macros and procedures

 

Contents

 

6.1 Procedures

6.2 Macros

 

 

6.1 Procedure

 

Definition of procedure

 

A procedure is a collection of instructions to which we can direct the flow of our program, and once the execution of these instructions is over control is given back to the next line to process of the code which called on the procedure.

 

Procedures help us to create legible and easy to modify programs.

 

At the time of invoking a procedure the address of the next instruction of the program is kept on the stack so that, once the flow of the program has been transferred and the procedure is done, one can return to the next line of the original program, the one which called the procedure.

 

Syntax of a Procedure

 

There are two types of procedures, the intrasegments, which are found on the same segment of instructions, and the inter-segments which can be stored on different memory segments.

 

When the intrasegment procedures are used, the value of IP is stored on the stack and when the intrasegments are used the value of CS:IP is stored.

 

To divert the flow of a procedure (calling it), the following directive is used:

 

CALL NameOfTheProcedure

 

The part which make a procedure are:

 

Declaration of the procedure

Code of the procedure

Return directive

Termination of the procedure

 

For example, if we want a routine which adds two bytes stored in AH and AL each one, and keep the addition in the BX register:

 

Adding Proc Near ; Declaration of the procedure

Mov Bx, 0 ; Content of the procedure

Mov B1, Ah

Mov Ah, 00

Add Bx, Ax

Ret ; Return directive

Add Endp ; End of procedure declaration

 

On the declaration the first word, Adding, corresponds to the name of out procedure, Proc declares it as such and the word Near indicates to the MASM that the procedure is intrasegment.

 

The Ret directive loads the IP address stored on the stack to return to the original program, lastly, the Add Endp directive indicates the end of the procedure.

 

To declare an inter segment procedure we substitute the word Near for the word FAR.

 

The calling of this procedure is done the following way:

 

Call Adding

 

Macros offer a greater flexibility in programming compared to the procedures, nonetheless, these last ones will still be used.

 

Macros

 

Contents

 

6.2.1 Definition of a macro

6.2.2 Syntax of a macro

6.2.3 Macro libraries

 

 

6.2.1 Definition of the macro

 

A macro is a group of repetitive instructions in a program which are codified only once and can be used as many times as necessary.

 

The main difference between a macro and a procedure is that in the macro the passage of parameters is possible and in the procedure it is not, this is only applicable for the TASM - there are other programming languages which do allow it. At the moment the macro is executed each parameter is substituted by the name or value specified at the time of the call.



 

We can say then that a procedure is an extension of a determined program, while the macro is a module with specific functions which can be used by different programs.

 

Another difference between a macro and a procedure is the way of calling each one, to call a procedure the use of a directive is required, on the other hand the call of macros is done as if it were an assembler instruction.

 

6.2.2 Syntax of a Macro

 

The parts which make a macro are:

 

Declaration of the macro

Code of the macro

Macro termination directive

 

The declaration of the macro is done the following way:

 

NameMacro MACRO [parameter1, parameter2...]

 

Even though we have the functionality of the parameters it is possible to create a macro which does not need them.

 

The directive for the termination of the macro is: ENDM

 

An example of a macro, to place the cursor on a determined position on the screen is:

 

Position MACRO Row, Column

PUSH AX

PUSH BX

PUSH DX

MOV AH, 02H

MOV DH, Row

MOV DL, Column

MOV BH, 0

INT 10H

POP DX

POP BX

POP AX

ENDM

 

To use a macro it is only necessary to call it by its name, as if it were another assembler instruction, since directives are no longer necessary as in the case of the procedures.

 

Example:

 

Position 8, 6

 

6.2.3 Macro Libraries

 

One of the facilities that the use of macros offers is the creation of libraries, which are groups of macros which can be included in a program from a different file.

 

The creation of these libraries is very simple, we only have to write a file with all the macros which will be needed and save it as a text file.

 

To call these macros it is only necessary to use the following instruction Include NameOfTheFile, on the part of our program where we would normally write the macros, this is, at the beginning of our program, before the declaration of the memory model.

 

The macros file was saved with the name of MACROS.TXT, the instruction Include would be used the following way:

 

;Beginning of the program

Include MACROS.TXT

.MODEL SMALL

.DATA

;The data goes here

.CODE

Beginning:

;The code of the program is inserted here

.STACK

;The stack is defined

End beginning

;Our program ends


 

More debug program examples

 

In this section we provide you several assembler programs to run in the debug program. You can execute each assembler program using the "t" (trace) command, to see what each instruction does.

 

First example

 

-a0100

297D:0100 MOV AX,0006 ; Puts value 0006 at register AX

297D:0103 MOV BX,0004 ;Puts value 0004 at register BX

297D:0106 ADD AX,BX ;Adds BX to AX contents

297D:0108 INT 20 ;Causes end of the Program

 

The only thing that this program does is to save two values in two registers and add the value of one to the other.

 

Second example

 

- a100

0C1B:0100 jmp 125 ; Jumps to direction 125H

0C1B:0102 [Enter]

- e 102 'Hello, How are you ?' 0d 0a '$'

- a125

0C1B:0125 MOV DX,0102 ; Copies string to DX register

0C1B:0128 MOV CX,000F ; Times the string will be displayed

0C1B:012B MOV AH,09 ; Copies 09 value to AH register

0C1B:012D INT 21 ; Displays string

0C1B:012F DEC CX ; Reduces in 1 CX

0C1B:0130 JCXZ 0134 ; If CX is equal to 0 jumps to 0134

0C1B:0132 JMP 012D ; Jumps to direction 012D

0C1B:0134 INT 20 ; Ends the program

 

This program displays on the screen 15 times a character string.

 

Third example

 

-a100

297D:0100 MOV AH,01 ;Function to change the cursor

297D:0102 MOV CX,0007 ;Forms the cursor

297D:0105 INT 10 ;Calls for BIOS

297D:0107 INT 20 ;Ends the program

 

This program is good for changing the form of the cursor.

 

Fourth example

 

-a100

297D:0100 MOV AH,01 ; Funtion 1 (reads keyboard)

297D:0102 INT 21 ; Calls for DOS

297D:0104 CMP AL,0D ; Compares if what is read is a carriage return

297D:0106 JNZ 0100 ; If it is not, reads another character

297D:0108 MOV AH,02 ; Funtion 2 (writes on the screen)

297D:010A MOV DL,AL ; Character to write on AL

297D:010C INT 21 ; Calls for DOS

297D:010E INT 20 ; Ends the program

 

This program uses DOS 21H interruption. It uses two functions of the same: the first one reads the keyboard (function 1) and the second one writes on the screen. It reads the keyboard characters until it finds a carriage

return.

 

Fifth example

 

-a100

297D:0100 MOV AH,02 ; Function 2 (writes on the screen)

297D:0102 MOV CX,0008; Puts value 0008 on register CX

297D:0105 MOV DL,00 ; Puts value 00 on register DL

297D:0107 RCL BL,1 ; Rotates the byte in BL to the left by one bit

; through the carry flag

297D:0109 ADC DL,30 ; Converts flag register to1

297D:010C INT 21 ; Calls for DOS

297D:010E LOOP 0105 ; Jumps if CX > 0 to direction 0105

297D:0110 INT 20 ; Ends the program

 

This program displays on the screen a binary number through a conditional cycle (LOOP) using byte rotation.

 

Sixth example

 

-a100

297D:0100 MOV AH,02 ; Function 2 (writes on the screen)

297D:0102 MOV DL,BL ; Puts BL's value on DL

297D:0104 ADD DL,30 ; Adds value 30 to DL

297D:0107 CMP DL,3A ; Compares 3A value with DL's contents without

; affecting its value only modifying the state of

; the car

297D:010A JL 010F ; jumps if < direction 010f

297D:010C ADD DL,07 ; Adds 07 value on DL

297D:010F INT 21 ; Calls for Dos

297D:0111 INT 20 ; Ends the Program

 

This program prints a zero value on hex digits

 

Seventh example

 

-a100

297D:0100 MOV AH,02 ; Function 2 (writes on the screen)

297D:0102 MOV DL,BL ; Puts BL value on DL

297D:0104 AND DL,0F ; Carries ANDing numbers bit by bit

297D:0107 ADD DL,30 ; Adds 30 to Dl

297D:010A CMP DL,3A ; Compares Dl with 3A

297D:010D JL 0112 ; Jumps if < 0112 direction

297D:010F ADD DL, 07 ; Adds 07 to DL

297D:0112 INT 21 ; Calls for Dos

297D:0114 INT 20 ; Ends the program

 

This program is used to print two digit hex numbers.

 

Eight example

 

-a100

297D:0100 MOV AH,02 ; Function 2 (writes on the screen)

297D:0102 MOV DL,BL ; Puts BL value on DL

297D:0104 MOV CL,04 ; Puts 04 value on CL

297D:0106 SHR DL,CL ; Moves per four bits of your number to the

; rightmost nibble

297D:0108 ADD DL,30 ; Adds 30 to DL

297D:010B CMP L,3A ; Compares Dl with 3A

297D:010E JL 0113 ; Jumps if < 0113 direction

297D:0110 ADD DL,07 ; Adds 07 to DL

297D:0113 INT 21 ; Calls for Dos

297D:0115 INT 20 ; Ends the program

 

This program works for printing the first of two digit hex numbers

 

Ninth example

 

-a100

297D:0100 MOV AH,02 ; Function 2 (writes on the screen)

297D:0102 MOV DL,BL ; Puts BL value on DL

297D:0104 MOV CL,04 ; Puts 04 value on CL

297D:0106 SHR DL,CL ; Moves per four bits of your number to the

;rightmost nibble

297D:0108 ADD DL,30 ; Adds 30 to DL

297D:010B CMP DL,3A ; Compares Dl with 3A

297D:010E JL 0113 ; Jumps if < 0113 direction

297D:0110 ADD DL,07 ; Adds 07 to DL

297D:0113 INT 21 ; Calls for Dos

297D:0115 MOV DL,BL ; Puts Bl value on DL

297D:0117 AND DL,0F ; Carries ANDing numbers bit by bit

297D:011A ADD DL,30 ; Adds 30 to DL

297D:011D CMP DL,3A ; Compares Dl with 3A

297D:0120 JL 0125 ; Jumps if < 125 direction

297D:0122 ADD DL,07 ; Adds 07 to DL

297D:0125 INT 21 ; Calls for Dos

297D:0127 INT 20 ; Ends the Program

 

This program works for printing the second of two digit hex numbers

 

Tenth example

 

-a100

297D:0100 MOV AH,01 ; Function 1 (reads keyboard)

297D:0102 INT 21 ; Calls for Dos

297D:0104 MOV DL,AL ; Puts Al value on DL

297D:0106 SUB DL,30 ; Subtracts 30 from DL

297D:0109 CMP DL,09 ; Compares DL with 09

297D:010C JLE 0111 ; Jumps if <= 0111 direction

297D:010E SUB DL,07 ; Subtracts 07 from DL

297D:0111 MOV CL,04 ; Puts 04 value on CL register

297D:0113 SHL DL,CL ; It inserts zeros to the right

297D:0115 INT 21 ; Calls for Dos

297D:0117 SUB AL,30 ; Subtracts 30 from AL

297D:0119 CMP AL,09 ; Compares AL with 09

297D:011B JLE 011F ; Jumps if <= 011f direction

297D:011D SUB AL,07 ; Subtracts 07 from AL

297D:011F ADD DL,AL ; Adds Al to DL

297D:0121 INT 20 ; Ends the Program

 

This program can read two digit hex numbers

 

Eleventh example

 

-a100

297D:0100 CALL 0200 ; Calls for a procedure

297D:0103 INT 20 ;Ends the program

 

-a200

297D:0200 PUSH DX ; Puts DX value on the stack

297D:0201 MOV AH,08 ; Function 8

297D:0203 INT 21 ; Calls for Dos

297D:0205 CMP AL,30 ; Compares AL with 30

297D:0207 JB 0203 ; Jumps if CF is activated towards 0203 direction

297D:0209 CMP AL,46 ; Compares AL with 46

297D:020B JA 0203 ; jumps if <> 0203 direction

297D:020D CMP AL,39 ; Compares AL with 39

297D:020F JA 021B ; Jumps if <> 021B direction

297D:0211 MOV AH,02 ; Function 2 (writes on the screen)

297D:0213 MOV DL,AL ; Puts Al value on DL

297D:0215 INT 21 ; Calls for Dos

297D:0217 SUB AL,30 ; Subtracts 30 from AL

297D:0219 POP DX ; Takes DX value out of the stack

297D:021A RET ; Returns control to the main program

297D:021B CMP AL,41 ; Compares AL with 41

297D:021D JB 0203 ; Jumps if CF is activated towards 0203 direction

297D:021F MOV AH,02 ; Function 2 (writes on the screen)

297D:022 MOV DL,AL ; Puts AL value on DL

297D:0223 INT 21 ; Calls for Dos

297D:0225 SUB AL,37 ; Subtracts 37 from AL

297D:0227 POP DX ; Takes DX value out of the stack

297D:0228 RET ; Returns control to the main program

 

This program keeps reading characters until it receives one that can be

converted to a hex number


More Assembler programs examples( using TASM program)

 

 

;name of the program:one.asm

;

.model small

.stack

.code

mov AH,1h ;Selects the 1 D.O.S. function

Int 21h ;reads character and return ASCII code to register AL

mov DL,AL ;moves the ASCII code to register DL

sub DL,30h ;makes the operation minus 30h to convert 0-9 digit number

cmp DL,9h ;compares if digit number it was between 0-9

jle digit1 ;If it true gets the first number digit (4 bits long)

sub DL,7h ;If it false, makes operation minus 7h to convert letter A-F

digit1:

mov CL,4h ;prepares to multiply by 16

shl DL,CL ; multiplies to convert into four bits upper

int 21h ;gets the next character

sub AL,30h ;repeats the conversion operation

cmp AL,9h ;compares the value 9h with the content of register AL

jle digit2 ;If true, gets the second digit number

sub AL,7h ;If no, makes the minus operation 7h

digit2:

add DL,AL ;adds the second number digit

mov AH,4CH

Int 21h ;21h interruption

End ; finishs the program code

 

This program reads two characters from the keyboard and prints them on the screen.

 

;name the program:two.asm

.model small

.stack

.code

PRINT_A_J PROC

MOV DL,'A' ;moves the A character to register DL

MOV CX,10 ;moves the decimal value 10 to register cx

;This number value its the time to print out after the A ;character

PRINT_LOOP:

CALL WRITE_CHAR ;Prints A character out

INC DL ;Increases the value of register DL

LOOP PRINT_LOOP ;Loop to print out ten characters

MOV AH,4Ch ;4Ch function of the 21h interruption

INT 21h ;21h interruption

PRINT_A_J ENDP ;Finishes the procedure

 

WRITE_CHAR PROC

MOV AH,2h ;2h function of the 21 interruption

INT 21h ;Prints character out from the register DL

RET ;Returns the control to procedure called

WRITE_CHAR ENDP ;Finishes the procedure

END PRINT_A_J ;Finishes the program code

 

This program prints the a character through j character on the screen

 

;name of the program :three.asm

.model small

.STACK

.code

 

TEST_WRITE_HEX PROC

MOV DL,3Fh ;moves the value 3Fh to the register DL

CALL WRITE_HEX ;Calls the procedure

MOV AH,4CH ;4Ch function

INT 21h ;Returns the control to operating system

TEST_WRITE_HEX ENDP ;Finishes the procedure

 

PUBLIC WRITE_HEX

;........................................................;

; This procedure converts into hexadecimal number the byte is in the register DL and show the digit number;

;Use:WRITE_HEX_DIGIT ;

;........................................................;

 

WRITE_HEX PROC

PUSH CX ;pushes the value of the register CX to the stack memory

PUSH DX ;pushes the value of the register DX to the stack memory

MOV DH,DL ;moves the value of the register DL to register DH

MOV CX,4 ;moves the value numeric 4 to register CX

SHR DL,CL

CALL WRITE_HEX_DIGIT ;shows on the computer screen, the first hexadecimal number

MOV DL,DH ;moves the value of the register DH to the register DL

AND DL,0Fh ;ANDing the upper bit

CALL WRITE_HEX_DIGIT ; shows on the computer screen, the second hexadecimal number

POP DX ;pops the value of the register DX to register DX

POP CX ; pops the value of the register DX to register DX

RET ;Returns the control of the procedure called

WRITE_HEX ENDP

 

PUBLIC WRITE_HEX_DIGIT

;......................................................................;

; ;

; This procedure converts the lower 4 bits of the register DL into hexadecimal ;number and show them in the computer screen ;

;Use: WRITE_CHAR ;

;......................................................................;

 

WRITE_HEX_DIGIT PROC

PUSH DX ;Pushes the value of the register DX in the stack memory

CMP DL,10 ;compares if the bit number is minus than number ten

JAE HEX_LETTER ;No , jumps HEX_LETER

ADD DL,"0" ;yes, it converts into digit number

JMP Short WRITE_DIGIT ;writes the character

HEX_LETTER:

ADD DL,"A"-10 ;converts a character into hexadecimal number

WRITE_DIGIT:

CALL WRITE_CHAR ;shows the character in the computer screen

POP DX ;Returns the initial value of the register DX to register DL

RET ;Returns the control of the procedure called

WRITE_HEX_DIGIT ENDP

 

PUBLIC WRITE_CHAR

;......................................................................;

;This procedure shows the character in the computer screen using the D.O.S. ;

;......................................................................;

 

WRITE_CHAR PROC

PUSH AX ;pushes the value of the register AX in the stack memory

MOV AH,2 ;2h Function

INT 21h ;21h Interruption

POP AX ;Pops the initial value of the register AX to the register AX

RET ;Returns the control of the procedure called

WRITE_CHAR ENDP

 

END TEST_WRITE_HEX ;finishes the program code

 

This program prints a predefined value on the screen

 

 

;name of the program:five.asm

.model small

.stack

.code

 

PRINT_ASCII PROC

MOV DL,00h ;moves the value 00h to register DL

MOV CX,255 ;moves the value decimal number 255. this decimal number

;will be 255 times to print out after the character A

PRINT_LOOP:

CALL WRITE_CHAR ;Prints the characters out

INC DL ;Increases the value of the register DL content

LOOP PRINT_LOOP ;Loop to print out ten characters

MOV AH,4Ch ;4Ch function

INT 21h ;21h Interruption

PRINT_ASCII ENDP ;Finishes the procedure

 

WRITE_CHAR PROC

MOV AH,2h ;2h function to print character out

INT 21h ;Prints out the character in the register DL

RET ;Returns the control to the procedure called

WRITE_CHAR ENDP ;Finishes the procedure

 

END PRINT_ASCII ;Finishes the program code

 

 

This program prints the 256 ASCII code on the screen

 

dosseg

.model small

.stack

.code

write proc

mov ah,2h;

mov dl,2ah;

int 21h

mov ah,4ch

int 21h

write endp

 

end write

 

This program prints a defined character using an ASCII code on the screen.

 

 

.model small; the name of the program is seven.asm

.stack;

.code;

 

EEL: MOV AH,01 ; 1 function (reads one character from the keyboard)

INT 21h ; 21h interruption

CMP AL,0Dh ; compares the value with 0dh

JNZ EEL ;jumps if no equal of the label eel

MOV AH,2h ; 2 function (prints the character out on the screen)

MOV DL,AL ;moves the value of the register AL to the register DL

INT 21h ;21 interruption

MOV AH,4CH ;4C function (returns the control to the D.O.S. operating system)

INT 21h ;21 interruption

 

END ;finishes the program

 

This program reads characters form the keyboard and prints them on the screen until find the return character.

 

 


Date: 2014-12-22; view: 830


<== previous page | next page ==>
FCB method | I. Introduction
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.039 sec.)