Overlapped and non-overlapped block transfer of elements

Program Description

Program for non-overlapped and overlapped block transfer of array elements.Takes the array elements from the user and also the number of elements to be overlapped in overlapped transfer.Block transfer here, refers to moving of block of data within the memory to a different location.In non-overlapped transfer we move the data to a completely new location. It is easily accomplished by copying the data using two pointers, one data byte at a time.In overlapped transfer the data block is shifted slightly from the present position,thus, some of the starting elements may overlap with the old position of the last elements in the array. They are therefore copied in reverse order

Program
;**********************************************************************
;   code.cheraus.com   
; 
;  Overlapped and non-overlapped block transfer of elements
;
;**********************************************************************


; Macro Definition

print macro m                   ;macro definition for printing a string
mov ah,09h                      ; 09 function for printing string
mov dx,offset m                 ; content to be printed moved to DX
int 21h
endm                            ; end of macro



.model small                    ; one data segment, one code segment



;************  Start of Data Segment  **************



.data                           ;data segment

src db 20 dup(0)                ; source array
des db 20 dup(11h)              ; destination array
count db ?                      ; count variable to store number of elements

numCount db 10,13, "Enter the number of source elements: $"          ; string
msg db 10,13, "The number is: $"                                     ; string
msg1 db 10,13, "Enter the number: $"                                 ; string
msg2 db 10,13, "The source block is: $"                              ; string
msg3 db 10,13, "Destination block before transfer: $"                ; string
msg4 db 10,13, "Destination block after transfer(non-overlap) : $"   ; string
msg5 db 10,13, "Enter the number of elements to be overlapped: $"    ; string
msg6 db 10,13, "destination block after transfer(overlapped): $"     ; string
newLine db 10,13, "$"                                                ; string

overCount db ?              ; stores the number of elements to be overlapped    



;************* End of Data segment  ****************** 



;************ Start of Code Segment  *******************


.code
start:

             mov ax,@data            ; data segment initialization
             mov ds,ax               ; 

 
             print numCount          ; prints the string "numCount"
             call accept             ; returns the number in "BL" register
             mov count,bl            ; store in count variable

             mov si,offset src       ; point si to src array
             mov ch,count            ; take count in CH register

             print msg5              ; msg5-num of elements to overlap
             call accept             ; function call to accept two digit num
             mov overCount,bl        ; func returns the num in BL,therefore
                                     ;  copy to memmory variable from BL 



;****************  Accept "count"  numbers from the user  ****************


accept_num:  print msg1             ; msg1-enter the num
             call accept            ; take user input, num in BL
             mov [si],bl            ; store the num in memory pointed by SI
                                    ; from BL 
             inc si                 ; increment the pointer SI to point to
                                    ;  next memory location 
             dec ch                 ;  decrement count from CH
             jnz accept_num         ;  accept untill CH is zero



;*************  Print the src array  ************************************

             print newLine          ; goes to next line
             print msg2             ; msg2-src block is
			 print newLine          ; goes to next line
             mov si,offset src
             mov ch,20

print_src:   mov bl,[si]
             call displaynum
             inc si
             dec ch
             jnz print_src


;*************  Print destination array before transfer  *****************


             ;print destination block before transfer
			 print newLine          ; goes to next line(for readability)
             print msg3             ;destination before transfer
			 print newLine          ; goes to next line
             mov si,offset des
             mov ch,20




print_des:   mov bl,[si]
             call displaynum
             inc si
             dec ch
             jnz print_des
     




;**************  Perform block transfer  ******************************

             mov si,offset src         ;point si to src array 
             mov di,offset des         ; point di to des array
             mov al,count              ; copy count in al
             mov ch,al                 ; copy count in cl

move_more:   mov al,[si]               ; copy from src location to al
             mov [di],al               ; copy from al to des location
             inc si                    ; inc si to point to nex loc
             inc di                    ; inc di to point to next loc
             dec ch                    ; decrement count
             jnz move_more             ; repeat untill all elements moved



;**************   Display des block after nonoverlap transfer  **********

          mov ch,20
		  print newLine          ; goes to next line
          print msg4
		  print newLine          ; goes to next line
          mov si,offset des

new_des:  mov bl,[si]
          call displaynum
          inc si
          dec ch
          jnz new_des



;*************  Perform overlapped transfer  *********************


         ; set the SI and DI pointer to appropriate positions

          mov si,offset src
          mov ch,00
          mov cl,count
          dec cx           ;to point to last element
          add si,cx        ; SI is 16 bit therefore 16 register should be used

          mov di,offset src   ;point DI to src array
          mov ch,00
          mov cl,count
          dec cx
          add di,cx        ; DI to last element

          mov ch,00
          mov cl,overCount
          sub di,cx

          mov ch,00
          mov cl,count
          add di,cx

          ; Start moving the elements from the last 

            mov al,count
            mov ch,al
move_next:
          mov al,[si]
          mov [di],al
          dec si
          dec di
          dec ch
          jnz move_next


          mov ch,20
		  print newLine          ; goes to next line
          print msg6    ; msg6-overlapped transfer
		  print newLine          ; goes to next line



;*****************  Print the src array after overlapped transfer  *******

          mov si,offset src

new_src:  mov bl,[si]
          call displaynum
          inc si
          dec ch
          jnz new_src

            ; mov al,count
            ; mov bl,al
            ; print msg
            ; call displaynum



;******************  End of Main Program  *******************************



             mov ah,4ch   ;terminate program
             int 21h






;******************  Display and Accept prodedures  ********************


;accept procedure. Accepts and stores the number in BL

accept proc near

        mov ah,01
        int 21h

        sub al,30h
        cmp al,09
        jbe num
        sub al,07             ;be careful here. This procedure takes care
                              ; of only capital letter(A-F) which are 
num:    mov cl,04             ; correctly converted to proper number
        rol al,cl             ;   For small letters the code will have to
        mov bl,al             ;   be modified as they have different ASCII 
                              ;   values
        mov ah,01
        int 21h
        sub al,30h
        cmp al,09
        jbe num2
        sub al,07

num2:  or bl,al           ; ORing is necessary to store the 8 bit num in
                          ; appropriate format. Adding would also work.
						  ; eg: add bl,al

ret
accept endp


;display procedure. Expects the number to be displayed in "BL"

displaynum proc near


        mov al,bl
        mov cl,04
        and al,0f0h
        shr al,cl
        cmp al,09
        jbe number
        add al,07

number: add al,30h
        mov dl,al
        mov ah,02
        int 21h

        mov al,bl
        and al,00fh
        cmp al,09
        jbe number2
        add al,07
number2: add al,30h
         mov  dl,al
         mov ah,02
         int 21h

         mov ah,02
         mov dl,''
         int 21h

ret
displaynum endp

end start

Output

Comments
comments powered by Disqus