s

8086 assembly language macros edit button Edit

author
Murugan Andezuthu Dharmaratnam | calendar 12 February 2021 | 4470

Let's learn macros in 8086 assembly language with an example.

If there are 0 or more sets of instructions that you commonly write in 8086 assembly language then you can write a macro for the same. let me start by writing a macro to terminate and return to DOS.

kbhit macro
  mov ah,00
  int 16h
endm

This is one of the simplest macros you can write. Here is the macro definition

name    MACRO  [parameters,...]

             

ENDM

Macros are like procedures but not exactly the same. Macros exist only till you complete the code. At the time of compilation. all macros are replaced with real instructions. If you declared a macro and never used it in your code, the compiler will simply ignore it.

Sample Code To Print String Using Macro

Now we will write a sample code to print a string and then return the control to DOS. In this case, we are going to create two macros for both printing the string and then returning the control to DOS. Here is the sample code. Please remember one thing that macros should always be defined above the code that uses them.

.model tiny
.code
org 100h


; Print String Macro                
printstring macro str 
    mov ah,09
    mov dx,offset str
    int 21h    
endm


;Terminate Macro:
terminate macro
  mov ah,4ch
  mov al,00
  int 21h
endm      

                 
main proc near
    printstring message
    terminate
endp
message db 'hello world$'
end main

As you can see to call a macro you just need to type its name with or without parameters. whereas while calling a procedure you should use a call instruction followed by procedure name. The procedure is like a C function where it resides in a specific memory and every time a procedure it called control is transferred to that part of the memory. the control is returned back to the calling program by RET instruction. Whereas as mentioned above compiler replaces the maco with instructions that are present inside a macro at compile time. This means if I am having 100 printstring in my main proc. there will be 100 blocks of instructions inside by main proc, Unlike a procedure where it will only have the 100 call instructions.