Problem – Write an assembly language program to transfer a block of bytes from one memory location to another memory location by using string instruction.
- In this example, the counter value stored in CX register is 4.
- The block of data which is stored from memory location starting from 501 to 504 offset is transferred to another memory location which is starting from 600 to 603 offset.
CLD instruction is used to clear the directional flag, i.e., DF=0. Now, value of SI and DI will be increased.
REP instruction is used to repeat the step until value of CX is not equal to zero and value of CX is decremented by one at every step, i.e.,
MOVSB instruction is used to transfer bytes only from source memory location (MADS) to destination memory location (MAES).
MADS-->MAES where MADS=DS*10+SI MAES=ES*10+DI
Here, value of SI and DI is updated automatically.
if DF=0, SI=SI+1 and DI=DI+1
- set the value of offset SI equal to 500.
- set the value of offset DI equal to 600.
- load the value 0000 into register AX.
- load the data of AX register into DS(data segment).
- load the data of AX register into ES(extra segment).
- load the data of offset SI into CL register and load value 00 into CH register.
- increment the value of SI by one.
- clear the directional flag so that data is read from lower memory to higher memory location.
- check the value of CX, if not equal to zero then repeat step 10 otherwise go to step 11.
- transfer the data from source memory location to destination memory location and decrease the value of CX by one.
|0400||MOV SI, 500||SI<-500|
|0403||MOV DI, 00||DI<-600|
|0406||MOV AX, 0000||AX<-0000|
|0409||MOV DS, AX||DS<-AX|
|040B||MOV ES, AX||ES<-AX|
|040D||MOV CL, [SI]||CL<-[SI]||0410||MOV CH, 00||CH<-00|
|0413||CLD||clears the directional flag|
|0414||REP||repeat until CX is not equal to zero
and CX=CX-1 at every step
|0415||MOVSB||transfer the data from source to destination memory location|
- MOV SI, 500: load the value 500 into offet SI.
- MOV DI, 600: load the value 600 into offset DI.
- MOV AX, 0000: load the value 0000 into AX register.
- MOV DS, AX: load the value of AX register into DS (data segment).
- MOV ES, AX: load the value of AX register into ES (extra segment).
- MOV CL, [SI]: load the data of offset SI into CL register.
- MOV CH, 00: load value 00 into CH register.
- INC SI: increment the value of SI by one.
- CLD: clears the directional flag i.e. DF=0.
- REP: repeat until value of CX is not equal to zero and decrement the value of CX by one at each step.
- MOVSB: transfer the data from source memory location to destination memory location.
- HLT: end.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.