Saturday, 19 October 2013

Helps to write string manipulation programs using MASM 6.14

masm is very easy language if we understand every instruction used in the program and how these instructions executes. most of the instructions are same as intel 8086 microprocessor  instructions other than 8086 instruction set is not a BIG thing. masm helps us to do string operations much easlier than in 8086 kit ( i don't know even they are possible in that kit).
this post is only a try to give my knowledge to readers. just try debugging one program and you fill understand what each instruction performs. even i don't know each and every word in this masm.

lets try to understand the following program
.MODEL SMALL
.STACK
.DATA

MSG1 DB 'ENTER A STRING:$'
MSG2 DB 'REVERSED STRING:$'
CRLF DB 13,10,'$'
ARR DB 100 DUP(?)

PUTS MACRO STR
MOV AH,09H
MOV DX,OFFSET STR
INT 21H
ENDM

.CODE
.STARTUP

PUTS CRLF

PUTS MSG1
MOV ARR,127
MOV AH,0AH
MOV DX,OFFSET ARR
INT 21H
MOV SI,1
MOV AL,ARR[SI]
MOV AH,0
MOV SI,AX
INC SI
PUTS CRLF
PUTS MSG2

PRINT:
MOV DL,ARR[SI]
MOV AH,02
INT 21H
DEC SI
CMP SI,1
JNZ PRINT
.EXIT
END


explanation

.MODEL SMALL                                              
.STACK
.DATA

//defining model and segments

MSG1 DB 'ENTER A STRING:$                                //defining msg1.
MSG2 DB 'REVERSED STRING:$'                             //defining msg2.

//they stores in memory in DS.each string is terminated using a $ sign. its to make understand the compiler that string ended there.

CRLF DB 13,10,'$'               //crlf is a string which contains characters to go next line
ARR DB 100 DUP(?)       // arr is an array of length that initialised to 00 in DS to store words from keyboard

after allocating these strings in memory,
msg1(000E-001D)
MSG2(001E-002E)
CRLF(002F-0031)
ARR(0032-0095)

PUTS MACRO STR                                 //starts a macro. str is a variable
MOV AH,09H                                         //interrupt identification to print a string
MOV DX,OFFSET STR                         //offset address of required string is copied into dx
INT 21H                                                 // interrupted the processor to print the characters.
ENDM                                                      // end a macro

this macro is to print different strings. just like using functions.puts is a function name
int 21h is a interrupt that checks the AH and performs depending upon value of AH.





examples:

  1. mov ah,01      reads character from keyboard to DL
  2. mov ah,02      write character from DL to monitor
  3. mov ah,09      display a string in monitor. that is continues character values from memory till reaches $ sign(hex value of $ is 24). DX should have the address of required string ie offset address.
  4. mov ah,0A      used to buffer a string from keyboard.DX should have the offset address of memory.starting location should have no.of max possible characters. after interrupt execution the         memory will contain the following datas

1- no.of max allowed characters        arr[0]    ie 127=7FH
2-no.of characters entered without enter character,n       arr[1]
3-data entered          arr[1] to arr[n+1]
4-enter character    arr[7]
                            

.CODE
.STARTUP

PUTS MSG1                    //enters into macro.dx will be =000E    
MOV ARR,127               //arr is a place to input keyboard string. no.of max allowed characters is copied to arr[0th place]. generally this should be less than 100 since the size of arr is defined as 100. we wont get a run time error because we are not entering a string with 100 or more characters.

MOV AH,0AH               //interrupt identification. input string from keyboard
MOV DX,OFFSET ARR         // now dx contains offset address of arr.now DX=0032. The characters are copied to this place.
INT 21H                                   //interrupting processor to do all these activities
after this instruction


MOV SI,1                                
MOV AL,ARR[SI]             // now AL contains no.of characters entered.( arr[1]) if string is 'renoy' , AL=5
MOV AH,0
MOV SI,AX                               //SI=0005
INC SI                               //SI=0006
PUTS CRLF                 //  enters into macro.now dx=002F...next line
PUTS MSG2                 // enters into macro. now dx=001E. prints msg2

PRINT:
MOV DL,ARR[SI]        // characters are copied
MOV AH,02                 //interrupt identification to print one character
INT 21H                        // interrupt processor to print the character
DEC SI
CMP SI,1                      //to check if SI =1
JNZ PRINT

this loop runs till SI reaches 1. in each step DL = arr[SI]. SI value decremented by 1 from six to one.
so DL values will be 'y','o','n','e','r'


.EXIT
END



No comments:

Post a Comment