Aim: - This paper provides in depth knowledge of using 8086 string instructions to perform a variety of operations on a sequence of data words in memory with examples. These can be used to repeat some operations on a sequence of data words in memory. It also compares string instructions with other 8086 looping instructions. This paper tells how to write and use an assembler macro and compares it with calling a procedure.
I. Strings
Introduction: -A string is a series of bytes or words stored in successive memory locations. Often a string consists of a series of ASCII character codes. When you use a word processor or text editor program, you are actually creating a string of this sort as you type in a series of characters. One important feature of a word processor is the ability to move a sentence or group of sentences from one place in the text to another. Doing this involves moving a string of ASCII characters from one place in memory to another. The 8086 Move String instruction”MOVS”, allows you to do operations such as this very easily.
Another important feature of most word processors is the ability to search through the text looking for a given word or phrase. The 8086 Compare String instruction “CMPS”, can be used to do operations of such type. In a similar manner, the 8086 “SCAS”, instruction can be used to search a string to see whether it contains a specified character.
Moving a String: -. Suppose that you have a string of ASCII characters in successive memory locations in the data segment. You want to move the string to some new sequence of locations in the data segment. The basic algorithm which can be used to perform this is:-
REPEAT: MOVE BYTE FROM SOURCE STRING TO DESTINATION STRING
UNTIL: ALL BYTES ARE MOVED
A little more expanded algorithm can be written as: -
INITIALIZE SOURCE POINTER, SI
INITIALIZE DESTINATION POINTER, DI
INITIALIZE COUNTER, CX
REPEAT: COPY BYTE FROM SOURCE TO DESTINATION
INCREMENT SOURCE POINTER
INCREMENT DESTINATION POINTER
DECREMENT COUNTER
UNTIL: COUNTER = 0
As it turns out, the single 8086 instruction MOVSB will perform all the actions in the REPEAT-UNTILL loop. The MOVSB instruction will copy a byte from the location pointed to by the SI register to a location pointed to by the DI register. It will then automatically increment SI to point to the next source location, and increment DI to point to the next destination location. If you add a special prefix called the repeat prefix in front of the MOVSB instruction, the MOVSB instruction will be repeated and CX decremented until CX is counted down to zero. In other words, the REP MOVSB instruction will move the entire string from the source location to the destination location if the pointers are properly initialized.
Comparing a String: -. Suppose that we want to compare a user entered password with the correct password stored in memory. If the passwords do not match we want to sound an alarm. If the passwords match, we want to allow the user access to the computer and continue with the mainline program. A normal REPEAT-UNTIL and IF-THEN-ELSE algorithm to perform this can be written as:-
INITIALIZE PORT DEVICE FOR OUTPUT
INITIALIZE SOURCE POINTER, SI
INITIALIZE DESTINATION POINTER, DI
INITIALIZE COUNTER, CX
REPEAT: COMPARE SOURCE BYTE WITH DESTINATION BYTE
INCREMENT SOURCE POINTER
INCREMENT DESTINATION POINTER
DECREMENT COUNTER
UNTIL: (STRING BYTES NOT EQUAL) OR (COUNT = 0)
IF: STRING BYTES NOT EQUAL
THEN: SOUND ALARM
STOP
ELSE: DO NEXTMAINLINE INSTRUCTION
The Compare String instruction CMPS can be used to help translate this algorithm to assembly language program.
Scanning a String: -. SCAS compares a byte in AL or a word in AX with a byte or word pointed to by DI in ES. Therefore the string to be scanned must be in the extra segment, and DI must contain the offset of the byte or the word to be compared. If the direction flag is cleared (0), then DI will be incremented after SCAS. If direction flag is set (1) then DI will be decremented after SCAS. For byte strings DI will be incremented or decremented by 1 and for word strings DI will be incremented or decremented by 2. For example:-
Algorithm Of SCAS:
SCAN A TEXT STRING OF 80 CHARACTERS
FOR A CARRIAGE RETURN, 0DH
PUT OFFSET OF STRING INTO DI
MOVE BYTE TO BE SCANNED FOR INTO AL
INITIALIZE COUNTER CX
CLEAR DF SO THAT DI AUTO-INCREMENTS
COMPARE BYTE IN STRING WITH BYTE IN AL
Scanning is repeated as long as the bytes are not equal and the end of the string has not been reached.
II. Macros
Procedures versus Macros: -. Whenever we need to use a group of instructions several times throughout a program, there are two ways we can avoid having to write the group of instructions each time we want to use it. One way is to write the group of instructions as a separate procedure. We can then just call the procedure whenever we need to execute that group of instructions. A big advantage of using procedures is that the machine codes for the group of instructions in the procedure only have to be put in memory once. Disadvantages of using it are the need for a stack and the overhead time required to call the procedure and return to the calling program.
When the repeated group of instructions is too short or not appropriate to be written as a procedure, we use a macro. A macro is a group of instructions we bracket and give a name to at the start of our program. Each time we call the macro in our program, the assembler will insert the defined group of instructions in place of the call. Since the generated machine codes are right in-line with the rest of the program, the processor does not have to go off to a procedure and return.
The main comparison is:-
MACRO is accessed during assembly with name given to macro when defined. Machine code generated for instructions each time called. Parameters passed as part of statement which calls macro.
PROCEDURE is accessed by calling and returning mechanism during program execution. Machine codes for instructions only put in memory once. Parameters passed in registers, memory locations or stack.
Macros are very popular with most of the Windows software?
1. Many applications provide macro capability inbuilt, like MS Excel, MS Word etc. These applications provide macro recording capabilities as well as some kind of macro language. For example: you can record a macro in Excel to perform calculations, format cells or just about anything you do in Excel.
2. Few software vendors provide general-purpose macro programs that can work across all Windows software.
What Can You Do With Macros?
Tons. Anything and everything that is repetitive can be done using macros. All of us perform many repetitive actions day after day on our computers. Once you are aware that these are repetitive actions, you can easily create a macro for it and next time perform it with a click of a button.
Examples: Login and check your multiple mail accounts, format data, insert date and copyright notices onto your documents, generate reports, copy data between applications. There are just a zillion things you can do.
Can I Use Macros?
If you think you are doing the same task again and again and it is frustrating and wasting your time and energy, you are ready to use macros. Even if it is not getting on your nerve, using a macro is a smart and fun way of working
Use Workspace Macro for all kinds of tasks. Below are few examples.
Syntax of a Macro
The parts which make a macro are:
1>Declaration of the macro
2>Code of the macro
3>Macro termination directive
The declaration of the macro is done the following way:
Name Macro MACRO [parameter1, parameter2...]
Even though we have the functionality of the parameters it is possible to create a macro which does not need them.
The directive for the termination of the macro is: ENDM
An example of a macro, to place the cursor on a determined position on the screen is:
Position MACRO Row, Column
PUSH AX
PUSH BX
PUSH DX
MOV AH, 02H
MOV DH, Row
MOV DL, Column
MOV BH, 0
INT 10H
POP DX
POP BX
POP AX
ENDM
To use a macro it is only necessary to call it by its name, as if it were another assembler instruction, since directives are no longer necessary as in the case of the procedures.
Macro Libraries
One of the facilities that the use of macros offers is the creation of libraries, which are groups of macros which can be included in a program from a different file.The creation of these libraries is very simple, we only have to write a file with all the macros which will be needed and save it as a text file.
To call these macros it is only necessary to use the following instruction Include NameOfTheFile, on the part of our program where we would normally
write the macros, this is, at the beginning of our program, before the declaration of the memory model.
The macros file was saved with the name of MACROS.TXT, the instruction Include would be used the following way:
;Beginning of the program
Include MACROS.TXT
.MODEL SMALL
.DATA
;The data goes here
.CODE
Beginning:
;The code of the program is inserted here
.STACK
;The stack is defined
End beginning
;Our program ends
Example of string:
To write 8086 Assembly Language Program to display number of character present in a string.
Answer:
MODEL SMALL
.STACK 100
.DATA
CR EQU 0DH
LF EQU 0AH
LEN DB 04 DUP(0)
MSG1 DB 'ENTER THE STRING.INPUT=','$'
MSG2 DB CR,LF,'THE LENGTH OF THE STRING=','$'
DISP MACRO MSG ; Micro to display a string
MOV AH,09H
MOV DX,OFFSET MSG
INT 21H
ENDM
.CODE
START:
MOV AX,@DATA
MOV DS,AX
DISP MSG1
MOV CX,00H
READ: MOV AH,01H ; Take the input string at run time
INT 21H
CMP AL,CR ; Detect the end of the string
JE AHEAD
INC CX
JMP READ
AHEAD:DISP MSG2
MOV SI,OFFSET LEN
MOV AX,CX
CALL HEX2ASC
DISP LEN
INT 03H
HEX2ASC PROC NEAR ; Procedure to convert number of character in HEX to ASCII
MOV BX,0001H
MUL BX
AAM
OR AX,3030H
MOV [SI],AH
MOV [SI+1],AL
MOV AL,'$'
MOV [SI+2],AL
INT 03H
HEX2ASC ENDP
END START
INPUT: ENTER THE STRING
.INPUT = Microprocessor lab
OUTPUT: 'THE LENGTH OF THE STRING= 18
IV. References
[1] http://ftp.utcluj.ro/pub/users/nedevschi/CA/I8086/8086InstrSet/8086inst.pdf
[2]. http://www.baskent.edu.tr/~herdem/downloads/EEM332/ders_notlari/8086ins.pdf
[3] www.cpu-world.com/CPUs/8086/index.html
[4] www.toodoc.com/8086-program-in-string-manipulation-ebook.html
[5] www.fourmilab.ch/autofile/www/section2_10_8.html
[6] www.du.edu/~etuttle/math/8086.htm
[7] http://www.google.co.in/#hl=en&q=assembler+MACRO+instructions&meta=&aq=f&oq=assembler+MACRO+instructions&fp=b3835e69fbd48a0c
[8] http://www.laynetworks.com/assembly%20tutorials5.htm
[9] http://www.cs.tufts.edu/comp/40/handouts/umasm.pdf