Programs on Strings (Assembly Language Programs)

    


  STRINGS

STRING INSTRUCTIONS 

A string is a series of bytes or a series of words in sequential memory locations. A string often consists of ASCII character codes. In the list, a “/” is used to separate different mnemonics for the same instructions. Use the mnemonic which most clearly describe the function of the instruction in a specific application. A “B” in a mnemonic is used to specifically indicate that a string of bytes is to be acted upon. A “W” in the mnemonic is used to indicate that a string of words is to be acted upon.

REP    An instruction prefix. Repeat following instruction until CX=0.

REP is a prefix which is written before one of the string instructions. It will cause the CX register to be decremented and the string instruction to be repeated until CX=0. The instruction REP MOVSB will continue to copy string bytes until the number of bytes loaded into CX has been copied.

                                                MOV CX, 0004H
                                                LEA SI, str1
                                                LEA DI, str2
                                                CLD
                                   REP     MOVSB

REPE/REPZ   An instruction prefix. Repeat instruction until CX = 0 or zero flag      ZF ¹ 1.

REPNE/REPNZ   An instruction prefix. Repeat until CX = 0 or ZF = 1.

MOVS  : Move string
MOVSB : Move string byte
MOVSW : Move string word

This instruction copies a byte or a word from a location in the data segment to a location in the extra segment. The offset the source byte or word in the data segment must be in SI register. The offset of the destination in the extra segment must be contained in the DI register.

                                                MOV SI, offset source_string.
                                                MOV DI, offset destination_string
                                                CLD
                                                MOV CX, 0004H
                              REP           MOVSB

CMPS/CMPSB/CMPSW :   Compare two string bytes or two string words.

A string is a series of the same type of data items in sequential memory locations. The CMPS instruction can be used to compare a byte in one string with a byte in another string or to compare a word in one string with a word in another string. SI is used to hold offset of a byte or a word in source string, and DI is used to hold the offset of a byte or word in another string. The comparison is done by subtracting the byte or word pointed to by DI from the byte or word pointed to by SI. After the comparison SI and DI will automatically be incremented or decremented to point to the next elements in the two strings. 
                             
SCAS : Scan string
SCASB : scan string Byte
SCASW : scan string word.
This instruction Compares a string byte with a byte in AL or a string word with a word in AX. DI contains the offset of a byte or word to be scanned in ES. The scan is performed by subtracting the destination string element from accumulator. This instruction does not destroy contents of the accumulator or memory. The flags will get effected are AF, CF, OF , PF, SF, ZF.

LODS/LODSB/LODSW :    Load string byte into AL or string word into AX.

STOS : Store string
STOSB : store string byte
STOSW : store string word
The instruction STOS transfers a byte from AL or word from AX to the destination string pointed by DI in ES.


;------------------------------------------------------------
; PROGRAM FOR MOVING A STRING FROM
; ONE LOCATION TO ANOTHER IN MEMORY
;------------------------------------------------------------
CODE SEGMENT
ASSUME CS: CODE, DS: DATA
            MOV AX, DATA
            MOV DS, AX

            LEA SI, SOURCE
            LEA DI, DEST
            MOV CX, CNT
            CLD                                       
REP     MOVSB                                 
            HLT
CODE ENDS

DATA SEGMENT                              
            SOURCE DB ‘COLLEGE’              
            DEST DB  7 DUP(0)
            CNT DW 0007H
DATA ENDS
END

Starting address of string to be moved in register SI and starting address of destination in register DI.  Count of characters to be moved in register CX.

MOVSB moves one character from source to destination. Increases SI and DI. REP prefix decreases register CX by 1 and checks whether all characters have been moved or not. If register CX is not 0 MOVSB will be repeated again and again till register CX becomes 0. So that all the character will be moved to destination.


;-----------------------------------------------------------------------------------------------
; PROGRAM FOR COMPARING STRINGS.
; THIS PROGRAM INPUTS A PASSWORD AND SOUNDS AN ALARM
; IF THE PASSWORD IS INCORRECT.
; ----------------------------------------------------------------------------------------------

CODE SEGMENT
ASSUME CS: CODE, DS: DATA, ES: DATA
MOV AX, DATA
MOV DS, AX
MOV ES, AX

            MOV DX, OFFFEH
MOV AL, 99H                       ; SETUP PORT AS  AN OUTPUT PORT
OUT  DX, AL

LEA   SI, PASSWORD
LEA   DI, INPUT_WORD
MOV CX, 0008H
CLD
REP     CMPSB
            JNE SOUND_ALARAM
            JMP OK

SOUND_ALARAM: MOV AL, 01
                                    MOV DX, OFFFAH
                                    OUT DX, AL
                                    HLT
                      OK :      NOP
                     CODE ENDS

DATA SEGMENT
     PASSWORD     DB  ‘FAILSAFE’
     INPUT_WORD DB  8DUP(?)
DATA ENDS
            END


 ; -------------------------------------------------------------------------------
; Write a macro to move ASCII characters from source to destination.
; -------------------------------------------------------------------------------
            CODE SEGMENT
            ASSUME CS:CODE, DS:DATA
            MOVE_ASCII   MACRO     NUMBER, SOURCE, DESTINATION
                                        MOV CX, NUMBER
                                         LEA SI, SOURCE
                                         LEA DI, DESTINATION
                                         CLD
                                REP MOVSB
                                         ENDM
          
                        MOVE_ASCII  0007H, BLOCK_START, BLOCK_DEST
                        HLT
            CODE ENDS
           DATA SEGMENT
                BLOCK_START DB “COLLEGE”
                BLOCK_DEST DB 7DUP(‘’)
           DATA ENDS
           END

;---------------------------------------------------------------------------------------------
;Program to move a block of characters from one location to another location
;---------------------------------------------------------------------------------------------
CODE SEGMENT
ASSUME CS:CODE, DS:DATA, ES:DATA
MOV AX,DATA
MOV DS,AX
MOV ES,AX
LEA SI,SOURCE
LEA DI,DEST
MOV CX,0007H
CLD
REPE MOVSB
HLT
CODE ENDS
DATA SEGMENT
SOURCE DB 'NALANDA'
DEST DB ?
DATA ENDS
END
Starting address of string to be moved in register SI and starting address of destination in register DI.  Count of characters to be moved in register CX.

MOVSB moves one character from source to destination. Increases SI and DI. REP prefix decreases cx by 1 and checks whether all characters have been moved or not. If cx is not 0 MOVSB will be repeated again and again till cx becomes 0. So that all the character will be moved to destination.

CHARACTER   OUTPUT
To display a character on the screen the following 3 statements has to be used:
mov dl,’a’            ; character is moved to register DL
mov ah,2h            ; character output sub program
int 21h

The character we want to output on the screen should be sent to register DL. 2h has to be moved to register AH. Upon executing int 21h the character in register DL will be displayed on the screen.

;------------------------------------------------------
; program to display letter 'a' on the screen
;------------------------------------------------------
code segment
assume cs: code
mov dl, 'a'             ; store ASCII code of 'a' in dl
mov ah, 2h            ; character output function
int 21h                  ; displays character in dl
mov ax,4c00h      ; return to MS-DOS
int 21h
code ends
end
;-----------------------------------------------

CHARACTER  INPUT
To read a character from keyboard we follow:
mov ah, 1h     ; keyboard input subprogram
int 21h            ; character input
mov c,al         ; character is moved to variable c.

To read a character from keyboard we first me 1h to register AH and execute int 21h. upon executing interrupt it reads a character from keyboard and places the character in the register AL.

;------------------------------------------------------------------------------------------
; program to read a character from keyboard and to display it on the screen
;------------------------------------------------------------------------------------------
code segment
assume cs:code
   mov ah,1h         ; keyboard input subprogram
   int 21h               ; read character into AL
   mov dl,al           ; character to be displayed is moved to register DL
   mov ah,2h         ; display subprogram
   int 21h               ; display character in DL
   mov ax,4c00h   ; return to ms-dos
   int 21h
code ends
end


STRING OUTPUT:

To display a string on the screen we first has to move the offset of the string to be displayed to the register DX.(starting address of the string). String output sub routine has to be executed. For this we move 9h to AH thru mov ah,9h and execute    int 21h. The string will be displayed on the screen.

;----------------------------------------------------------------
; program to display the message "nalanda college"
;----------------------------------------------------------------
code segment
assume cs: code, ds: data, es: data
   mov ax, data
   mov ds, ax
   mov es, ax

   mov dx, offset msg    ; copy address of msg to dx
   mov ah,9h                  ; string output
   int 21h                        ; displays string

   mov ax,4c00h            ; return to ms-dos
   int 21h

code ends
data segment
msg db 'nalanda college',13,10,'$'
data ends
end
;--------------------------------------------------------------------


The INT is a software interrupt. The instruction INT activates the service procedure of an interrupt specified by the interrupt type number.


;--------------------------------------------------------------------------------------------------
Using a while-do construct develop a sequence of 8086 instructions that reads a character string from the keyboard and after pressing the enter key the character string is to be displayed again.
;--------------------------------------------------------------------------------------------------

code segment
   assume cs:code, ds:data
   mov ax,data
   mov ds,ax

             lea bx,str1
  back:  mov ah,01h             ; read character
             int 21h

             mov [bx],al            ; store character
             inc bx
             cmp al,13               ; compares character with carriage return
             jnz back

             mov ah,09h            ; to display next line - to go to next line
             lea dx,nextl            ; next line
             int 21h

             mov ah,09h           ;display string
             lea dx,str1             ; address of string to be displayed in DX
             int 21h

             mov ah,4ch
             int 21h

code ends
data segment
    nextl db 10,13,'$'
    str1 db 80 dup('$')
data ends
     end


;----------------------------------------------------------------------
;program to accept a character from keyboard & display it
;-----------------------------------------------------------------------

code segment
assume cs:code, ds:data
mov ax, data
mov ds, ax

mov dl,'?'             ;display character ‘?’
mov ah,02h
int 21h

mov ah,01h          ; read a character
int 21h
mov c,al

lea dx,nextl           ; goto next line
mov ah,09h
int 21h

lea dx, msg            ; msg will be displayed
mov ah,09h
int 21h

mov dl,c               ; display character
mov ah,02h
int 21h

mov ah,4Ch         ; return to ms-dos
int 21h
code ends

data segment
    c       db    '?'
    msg  db    'Typed character is $'
    nextl db   10,13,'$'
data ends

end
Programs on Strings (Assembly Language Programs) Programs on Strings (Assembly Language Programs) Reviewed by Suresh Bojja on 10/28/2015 09:47:00 AM Rating: 5

Subjects & Labs

AC (6) ADS (2) ATMEL (6) AWP (5) C- Language (3) CAO (5) CDAC (1) Cloud Computing (1) CMC (6) CN (10) CONSERVATION & MANAGEMENT Complete Notes (1) Control Sys (2) CVSM (2) Data Analytics (1) DC (5) Digital System Design (3) DIP (17) DLD (10) DLD Lab Manual (1) DS (4) DSD&DICA (9) DSP (6) EDC (45) Electonic Components (2) Embedded (7) EMWTL (6) ENERGY AUDIT (1) Engg. Mechanics (2) Engg.Chemistry (11) Faculty Files (6) Faculty Labs (1) Fuzzy (3) II MID (1) Job Exam Papers (4) LICA (1) Linux OS (1) Materials (1) MATLAB (27) MCU IDE 8051 (1) MEMS (4) MPMC (57) MPMC LAB Manual (3) MTECH (6) MWE (2) OC (1) PDC (9) Projects (8) RADAR (1) Research (9) RVSP (3) SC (1) Softwares (1) SS (2) STLD (19) TCSS (1) TSSN (1) TV Engg (1) VHDL (4) VLSI (8) VLSI Lab manual (1) WCN (1) Wireless Commu. (1) WSN (2)
Theme images by sebastian-julian. Powered by Blogger.