MASM has become easier and very convenient to use ever since it was appended with a number of high level language constructs. Such constructs are available only for
MASM 6.0 and higher versions ..They are designated as ‘dot commands’ as they are preceded by a dot .These constructs make MASM a ‘high level assembler’. A few of these constructs are listed below
i) IF, ELSEIF and ELSE…..ENDIF
ii) REPEAT …UNTIL….ENDW
iii) REPEAT …WHILE ….ENDW
iv) BREAK
v) CONTINUE
vi) REPEAT …..UNTILCXZ
Except the last one, these constructs function as they do in a high level language (e.g.
C,C++) . For the last construct ,the loop repeats until register CX=0 .
Now we will endeavor to use these constructs in a few interesting programs.
Example 4-24 is a simple program which takes in data through the keyboard (without echo) and verifies three conditions.
i) Is the value of the number within 0 and 5?
ii) Is the value of the number within 6 and 9?
iii) Is the key pressed ‘not a number’?
These three questions are resolved using IF, ELSEIF and ELSE statements and appropriate messages are displayed. The symbol ‘&&” is used for ‘and’ just as in a high level language.
Example 4-24a
.MODEL SMALL .DATA
FIRST DB "THE VALUE IS WITHIN 0 AND 5$"
SECOND DB "THE VALUE IS WITHIN 6 AND 9$"
OTHER DB "NOT A NUMBER$"
.CODE .STARTUP
MOV AH,08 ;read in key without echo
INT 21H
.IF AL>='0' && AL<='5' ;first IF condition
LEA DX,FIRST ;display the first message MOV AH,09
INT 21H
.ELSEIF AL >='6' && AL<='9' ;second IF condition
LEA DX,SECOND ;display the second message MOV AH,09
INT 21H
.ELSE ;ELSE
LEA DX,OTHER ;display the third message MOV AH,09
INT 21H .ENDIF
.EXIT END
Let us see what exactly these high level language constructs are .Obviously ,they get converted to assembly language statements .We use the .LISTALL directive to find out.
Example 4-24b shows the listing for the code segment alone up to the .ENDIF statement.
We find the equivalent assembly code for the high level language constructs ,which in this case are compare and jump instructions.
Example 4-24b
0017 B4 08 MOV AH,08 0019 CD 21 INT 21H
.IF AL>='0' && AL<='5' 001B 3C 30 * cmp al, '0'
001D 72 0E * jb @C0001 001F 3C 35 * cmp al, '5' 0021 77 0A * ja @C0001
0023 8D 16 0000 R LEA DX,FIRST 0027 B4 09 MOV AH,09 0029 CD 21 INT 21H
.ELSEIF AL >='6' && AL<='9' 002B EB 1A * jmp @C0004
002D *@C0001:
002D 3C 36 * cmp al, '6' 002F 72 0E * jb @C0005 0031 3C 39 * cmp al, '9' 0033 77 0A * ja @C0005
0035 8D 16 001D R LEA DX,SECOND 0039 B4 09 MOV AH,09 003B CD 21 INT 21H
.ELSE
003D EB 08 * jmp @C0008 003F *@C0005:
003F 8D 16 003A R LEA DX,OTHER 0043 B4 09 MOV AH,09 0045 CD 21 INT 21H
.ENDIF
Next we will do a program using the REPEAT…. WHILE construct.
Example 4-25 shows how the .WHILE statement is used to repeat execution of a loop while a condition is satisfied, and to exit the loop when the condition no longer holds.
As long as CX is not equal to zero ,the loop repeats .The end of the loop is specified with a .ENDW construct. In this program ,a string consisting of all the letters of the alphabet, stored in location starting from DAT is to be reversed and displayed .After the reversed string is stored in location DAT1 onwards, it is appended with a ‘$’ so that it can be displayed with DOS INT 21H function number 9. Before the reversed string is displayed, the message REVERSED STRING: is first displayed.
Example 4-25
.MODEL SMALL
.DATA
DAT DB 'ABCDEFGHIJKLMNOPQRSTUVWXYZ$' MES1 DB 0DH,0AH,'REVERSED STRING: $' DAT1 DB 27 DUP(?)
.CODE .STARTUP
MOV BX,OFFSET DAT+25 ;address end of DAT MOV SI,OFFSET DAT1 ;address DAT1 MOV CX,26 ;load count in CX
.WHILE CX!=0 ;loop while CX!=0 MOV AL,BYTE PTR[BX] ;move byte to AL MOV BYTE PTR[SI],AL ;save AL in DAT1 INC SI
DEC BX
DEC CX
.ENDW ;end of while loop MOV BYTE PTR[SI+1],'$' ;append string with $ MOV DX,OFFSET MES1
MOV AH,09
INT 21H
MOV DX,OFFSET DAT1 ;display the reversed string
MOV AH,09
INT 21H
.EXIT END
The output of the above program is as shown below.
REVERSED STRING: ZYXWVUTSRQPONMLKJIHGFEDCBA C:\masm6.14\BIN>
For both the above problems ,the high level language constructs are written indented to the left. This is done only to improve the readability of the program.
The above problem can be worked out using the REPEAT ….UNTIL construct. The loop is repeated until CX==0 .CX becoming zero is the stopping condition for the loop.
There is also a .UNTILCXZ construct available that can be used in this example .The .UNTILCXZ instruction uses the CX register as a counter to repeat a loop a fixed number of times. This example can be rewritten using a .REPEAT ...UNTILCXZ loop also . Now let us see a case of nested loops. Example 4-26 shows the case of an IF loop nested within a repeat-until loop. In this , a character is to be searched within a string and the number of occurances of that character in the string is to be found out. To make the program inter-active for the user, messages asking for the character and the string are displayed using DOS INT 21H function 9. Both the character and the string are entered through the keyboard. The string is terminated when the ‘enter’ key is pressed. This corresponds to the ‘carriage return’ character 0DH and this is the termination condition of the repeat ..until loop.
Example 4.26
.MODEL SMALL
.DATA
MES1 DB 0DH,0AH,'ENTER THE CHARACTER: $' MES2 DB 0DH,0AH,'ENTER THE STRING: $'
MES3 DB 0DH,0AH,'NUMBER OF OCCURENCES: $' STRING DB 50 DUP('?')
.CODE
.STARTUP
MOV DX,OFFSET MES1 ;address MES1 MOV AH,09H ;display message INT 21H ; for the character MOV AH,01H
INT 21H ;enter character
MOV BL,AL ;copy character to BL MOV DX,OFFSET MES2 ;address MES2
MOV AH,09H ;display message INT 21H ;for entering string MOV CL,0 ;initialize counter
.REPEAT ;repeat loop MOV AH,01H ;enter string, one character
INT 21H ;at a time
.IF (AL==BL) ;compare with reference INC CL ;if matching ,increment CL
.ENDIF ;end IF loop .UNTIL AL==0DH ;exit when ‘enter’ is pressed
ADD CL,30H ;convert to ASCII MOV DX,OFFSET MES3 ;address MES3 location MOV AH,09H
INT 21H ;display message MOV DL,CL ;move count to DL MOV AH,06H ;display the count
INT 21H
.EXIT END
The output of the Example 4-26 for a character ‘t’ and string entered through the keyboard is shown below..
ENTER THE CHARACTER: t
ENTER THE STRING: tyu8iolhttttfeerttt NUMBER OF OCCURENCES: 8
C:\masm6.14\BIN>
Now that we have seen the use of the high level language constructs ,it is obvious that many of our previous programs can be re-written using these constructs .
Key points of this chapter
String instructions are used to simplify coding when bulk data is moved, scanned, compared etc.
Then , SI and DI registers are used as pointers to the data segment and extra segment, and the direction flag is used for auto increment/.decrement .
The conditional/unconditional REP prefix is used for repetitive string operations.
Procedures are subprograms which are ‘called’ by the main program for performing specialized operations.
Procedures may be intra-segment or inter-segment and this is clarified when defining a procedure, by using the ‘near’ or ‘far’ prefix.
Parameters to and from procedures may be passed via registers, memory or stack.
Macros are functionally similar to procedures ,but act in a different way
Number format conversions are necessary as BCD ,ASCII and binary numbers are used frequently.
8086 has specialized instructions for handling ASCII and BCD number operations
Signed number arithmetic involves the use of special instructions.
The overflow flag is very important in signed operations
The high level language constructs of MASM has made assembly language programming easier.
QUIZ
i) What is the role of the direction flag in string instructions?
ii) What is the difference in the functioning of the instructions STD and CLD ? iii) How does the instruction CLD function in the case of the following two
instructions?
REPMOVSB REP MOVSW
iv) Which is the pointer register to be used when using the extra segment in string operations?
v) Why does the LODS instruction not use the REP prefix?
vi) Do the following instructions function differently? In what way?
1. REPNE CMPSB