You are on page 1of 4

Tips and Tricks in Masm

;Author: Krishna Sangeeth K.S


;Simple udayippu paripadis to save time

1)IF ELSE

You can actually use the much loved if else statements in masm.

Here is the synatax

.IF condition
statement body for executing
.ELSE condition

statement body for executing


.ENDIF

2) Looping is simple with .REPEAT and .UNTIL

suppose we want to keep executing something until a terminating condition is rea


ched. Here's the way to go about

.REPEAT

statement 1

statement 2

...........

statement n

.UNTIL condition

eg:

.REPEAT
;assuming cl as the count
mov ah,01h
int 21h
sub al,30h
mov dl,al
mov ah,02h
int 21h
dec cl

.UNTIL CL==00

;the terminating conditions can be pretty complex too... Think of a condition l


ike AL>=0 && CH<9 ... Isn't that cool? :)

3) Variables can change your program, not life

So you always hoped that there was an extra register out there other than al,ah,
bl,bh,cl,ch.. Well we can't make extra registers to hold values.. :(

Instead we can make variables

Declaration is simple

put this in your data segment part

variable_name byte ?

eg: count byte ?

if you want more than 1 byte , u can use word instead of byte

So next time you wanted to keep a back up copy of that register you badly wants
to clear, just copy it to your variable

eg: mov count,al

4) DEBUG

Worst case : You have got an .exe file out there somewhere hidden in your system
. All you now need is the program code..

Let's assume the .exe file to be string.exe

NOw type debug string.exe


enter p to see the lines of code..

/***** DEBUG IS pretty cool... We can understand our simple mistakes by debuggin
g... So try it out...

/**** We can see exactly what content of registers are after each instruction.
This way we will never get lost on what we have to do :)

5) Packing and unpacking

scenario: you need to take two digits and do some operation with that

problem : masm allows you only to take one digit at a time, since the max an al
could hold is 8 bits

Solution : Packing

How packing is done?

Move the first digit to num1


mov ah,00
Move the second digit to num2
mov al,num1 ---> we got first number in al
mov bl, 0a -----> bl is moved with 10
mul bl----> now al equals al*10
add al,num2 ---> dat's packing for you

Worked out example


let the number we want to enter be 13
num1= 1
num2= 3

mov al,num1 ===> al=01


mov bl,0ah ====> bl=0a

mul bl ====> al=0a


add al,num2 ===> al=0d

Now that we have 0d in al, let's unpack it for printing. Remember for printing a
s decimal number we need to unpack this.
;assuming al has 0dh(13)
mov bl,0ah
div bl
;after this statement al has quotient 1 and ah has remainder 3
;so it's just a matter of printing them by moving to dl

6)

You might also like