;*******************************************************
; Some esential macros for elegant programming in masm *
;*******************************************************
exit macro
mov ax,4c00h
int 21h
endm
space macro
mov ah,02h
mov dl,20h
int 21h
endm
;----------------------------------------------------
;Accepts single 2-digit hex nos.
;output: input value stored in AL
;regs. affected: AX,BH
;----------------------------------------------------
input macro ;two hex-digit input
local ok,p,ok1,p1
mov ah,01h
int 21h
cmp al,39h ;ascii value of 9
jbe ok
sub al,61h ;ascii value of a
add al,0ah
jmp p
ok: sub al,30h
p: rcl al,01h
rcl al,01h
rcl al,01h
rcl al,01h
mov bh,al
mov ah,01h
int 21h
cmp al,39h ;ascii value of 9
jbe ok1
sub al,61h ;ascii value of a
add al,0ah
jmp p1
ok1:sub al,30h
p1: add al,bh
endm
;---------------------------------------------------
;Prints two hex digits on console
;input: provide the digits in BH
;regs. affected: AX,DX
;---------------------------------------------------
output macro ;two hex-digit output
local ok1,show,ok2,show2
mov al,bh ;the digits to display is in BH
and al,0fh
mov dh,al
mov al,bh
and al,11110000b ;even binary values allowed in masm
rcr al,01h
rcr al,01h
rcr al,01h
rcr al,01h
mov dl,al
cmp dl,09h
jbe ok1
sub dl,0ah
add dl,41h ;hex digit handler
jmp show
ok1:add dl,30h
show:
mov ah,02h
int 21h
mov dl,dh
cmp dl,09h
jbe ok2
sub dl,0ah
add dl,41h ;hex digit handler
jmp show2
ok2:add dl,30h
show2: int 21h
endm
showmsg macro str ;str data pssed through macro
mov ax,@data
mov ds,ax
lea dx,str
mov ah,09h
int 21h
endm
str_length macro str ;computes length of a string
local lp1,done
mov ax,@data
mov ds,ax
mov si,00h
mov cx,0000h ;clear cx
lp1:
cmp str[si],24h ;check if its '$'
je done
;else still chars left
inc si
inc cx
jmp lp1
done:
mov ax,cx
endm
;---------------------------------------------------
;Accepts input, and places them in an array
;array: is the address of the location from where inputs would be stored
;count: the no. of inputs
;---------------------------------------------------
input_array macro array,count,str
local lp
mov cx,count ;the array size
mov si,0000h
mov bh,00h
showmsg str
lp: input ;call the single input macro
mov array[si],al
inc si
space
loop lp
endm
;---------------------------------------------------
;Setting the cursor
;Input: set row number in DH & column number in DL
;Registers affected: AH,BH
;---------------------------------------------------
cursor macro
mov ah,02h ;request set cursor
mov bh,00h ;page number 0
int 10h ;Interrupt-call BIOS
endm
;---------------------------------------------------
;Clearing the screen
;Registers affected: AX,BH,CX,DX
;---------------------------------------------------
clrscrn macro
mov ax,0600h ;ah 06(scroll), al 00 (full screen)
mov bh,71h ;attribute: white (7) on blue (1)
mov cx,0000h ;upper left row:column
mov dx,184fh ;lower right row:column
int 10h ;Interrupt-call BIOS
endm
;---------------------------------------------------
;display array
;Input: Array address, the size of the array and an output message
;---------------------------------------------------
display_array macro array,count,str
local lp
pusha
mov cx,count ;the array size
mov si,0000h
mov bh,00h
showmsg str
lp: mov bh,array[si] ;retrieving the individual terms of the array before calling the output macro
output ;calling output macro
inc si
space
loop lp
pop a
endm