Lab 04
Lab 04
Loop
The ‘Loop’ instruction provides a simple way to repeat a block of statements a specific number of times.
To do this, first we have to put a value in CX (count resister) which will track the number of times the block
of instructions will execute.
Example:
C code
int j;
j = 0;
while ( j < 10 )
{ j++
;
}
Assembly code
MOV AL, 0
MOV CX, 10
INCREMENT:
ADD AL, 1
LOOP INCREMENT
Macro
Defining macros:
• A macro must be defined before it can be used.
• Parameters are optional.
• Each parameter follows the rules for identifiers. It is a string that is assigned a value when
the macro is invoked.
statement-list
ENDM
Example:
INCLUDE 'EMU8086.INC'
.DATA
.CODE
ADD_TWO MACRO R1
MOV AX, R1
ENDM ADD_TWO
MAIN PROC
ENDP MAIN
END MAIN
Library of common functions - emu8086.inc
To make programming easier there are some common functions that can be included in your program. To
make your program use functions defined in other file you should use the INCLUDE directive followed by
a file name. Compiler automatically searches for the file in the same folder where the source file is located,
and if it cannot find the file there it searches in Inc folder.
To use any of the functions in emu8086.inc you should have the following line in the beginning of your
source file:
include 'emu8086.inc'
Example 1
INCLUDE 'EMU8086.INC'
.DATA
.CODE
MAIN PROC
GOTOXY 10, 5
ENDP MAIN
END MAIN
emu8086.inc also defines the following procedures:
• PRINT_STRING - procedure to print a null terminated string at current cursor position,
receives address of string in DS:SI register. To use it declare: DEFINE_PRINT_STRING
before END directive.
• PTHIS - procedure to print a null terminated string at current cursor position (just as
PRINT_STRING), but receives address of string from Stack. The ZERO TERMINATED string
should be defined just after the CALL instruction. For example:
CALL PTHIS db
'Hello World!', 0
• GET_STRING - procedure to get a null terminated string from a user, the received string
is written to buffer at DS:DI, buffer size should be in DX. Procedure stops the input when
'Enter' is pressed. To use it declare: DEFINE_GET_STRING before END directive.
• SCAN_NUM - procedure that gets the multi-digit SIGNED number from the keyboard,
and stores the result in CX register. To use it declare: DEFINE_SCAN_NUM before END
directive.
To use any of the above procedures you should first declare the function in the bottom of your
file (but before the END directive), and then use CALL instruction followed by a procedure
name. For example:
INCLUDE 'EMU8086.INC'
.DATA
MAIN PROC
ENDP MAIN
END MAIN
Task 1
Task 2
Create an array of five elements and search for an element. If the element exists in the array
print ‘Value found’ otherwise ‘Value not found’
Task 3
A palindrome is a word that is same when read from both ends. Example: ‘RACECAR’.
Write a program that will take a string from a user and determine whether the word is a
palindrome. The output may be a ‘Yes’ or a ‘No’.
Example:
racecar yes
hellyeah
no
Task 4
Take two strings as inputs from the user. Compare the two strings and determine if they are the
same or not. If they are not, output the string which is lexicographically larger, else print “They
are the same”.