Procedure Declaration PROC name type
; body of procedure
ENDP name
The CALL Instruction call name where name is the name of a procedure, and indirect call address_expression (not generally recommended) where address_expression specifies a register or memory location containing the address of a procedure Executing a CALL The RET instruction ret pop_value is executed Examples of Procedures String Instructions The Direction Flag CLD and STD cld ;clear direction flag std ;set direction flag Moving a String DATASEG
string1 DB "HELLO"
string2 DB 5 DUP (?)
movsb ;move string byte MOVSB example mov ax,@data
mov ds,ax     ;initialize ds
mov es,ax     ; and es
lea si,[str1] ;si points to source string
lea di,[str2] ;di points to dest string
cld           ;set df=0 (increasing)
movsb         ;move first byte
movsb         ;move second byte
The REP Prefix REP Example mov ax,@data
mov ds,ax     ;initialize ds
mov es,ax     ; and es
lea si,[str1] ;si points to source string
lea di,[str2] ;di points to dest string
cld           ;set df=0 (increasing)
mov cx,5      ;# of chars in string1
rep movsb     ;copy the string
MOVSW movsw ;move string word The STOSB and STOSW Instructions stosb ;store string byte stosw ;store string word Code using STOSB mov ax,@data
mov es, ax     ;initialize es
lea di,[str]   ;di points to str
cld            ;process to the right
mov al,'A'     ;al has char to store
stosb          ;store an 'A'
stosb          ;store another one
Reading and Storing a Character String chars_read = 0
read a character (using int 21h, fcn 1)
while character is not CR do
    if char is BS then
        chars_read = chars_read - 1
        back up in string
        store char in string
        chars_read = chars_read + 1
    read another character
Code to Read a String         cld         ;process from left
        xor bx,bx   ;BX holds no. of chars read
        mov ah,1    ;input char function
        int 21h     ;read a char into AL
WHILE1: cmp al,0Dh  ;<CR>?
        je ENDWHLE1 ;yes, exit
    ;if char is backspace
        cmp al,08h  ;is char a backspace?
        jne ELSE1   ;no, store in string
        dec di      ;yes, move string ptr back
        dec bx      ;decrement char counter
        jmp read    ;and go to read another char
ELSE1:  stosb       ;store char in string
        inc bx      ;increment char count
READ:   int 21h     ;read a char into AL
        jmp WHILE1  ;and continue loop
The LODSB Instruction lodsb ;load string byte lodsw ;store string word Code using LODSB         DATASEG
str     DB  'ABC'    ;define string
        mov ax,@data
        mov ds, ax   ;initialize ds
        lea si,[str] ;si points to str
        cld          ;process left to right
        lodsb        ;load first byte in al
        lodsb        ;load second byte in al
Displaying a Character String for count times do
    load string character into al
    move it to dl
    output the character
Code to Display a String         cld           ;process from left
        mov cx,number ;cx holds no. of chars
        jcxz ENDFOR   ;exit if none
        mov ah,2      ;display char function
        lodsb         ;char in al
        mov dl,al     ;move it to dl
        int 21h       ;display character
        loop TOP      ;loop until done
Scan String scasb     ;scan string byte SCASW SCASB Example         DATASEG
str     DB    'ABC'     ;define string
        mov   ax,@data
        mov   es,ax     ;initialize es
        cld             ;process left to right
        lea   di,[str]  ;di points to str
        mov   al,'B'    ;target character
        scasb           ;scan first byte
        scasb           ;scan second byte
REPNE, REPNZ, REPE, and REPZ Comparing Strings cmpsb     ;compare string byte cmpsw     ;compare string word Example of CMPSB         mov  ax,@data
        mov  ds,ax         ;initialize ds
        mov  es,ax         ; and es
        lea  si,[string1]  ;si points to first string
        lea  di,[string2]  ;di points to second string
        cld                ;left to right processing
        mov  cx,10         ;# of chars in strings
        repe cmpsb         ;compare string bytes
        jl   S1_1st        ;string1 precedes string2
        jg   S2_1st        ;string2 precedes string1
        mov  ax, 0         ;put 0 in ax, string1=string2
        jmp  EXIT          ;and exit
        mov  ax, 1         ;put 1 in ax, string1>string2
        jmp  EXIT          ;and exit
        mov  ax, -1        ;put -1 in ax, string1<string2