Search code examples
assemblymasmdosbox

write assembly code that stores what was written on screen ,clear the screen and then restore what was written


I am required to write assembly code that stores what was written on the screen (in dosbox) in text mode ,clear the screen and then restores what was previously written ,so I wrote this code but it doesn't seem to work correctly..can anyone point why ? I will be really thankful

PS :I am still a noob

Here's my code :

Dataseg segment
    Data_seg dw 2000 dup(?)
Dataseg ends


Codeseg segment

Main PROC  far   

            assume ds:Dataseg,cs:Codeseg,ss:stackseg

        Mov ax,Data_seg
        Mov es,ax
        Mov ax,0b800h
        Mov ds,ax       
        Mov cx,25*80 ;(rows)*cols-default for screen

        Mov di,OFFSET Data_seg
        Mov si,0   
        CLD             ;autoincrement
        Rep movsw


        ;now clearing the screen

        Mov cx,25*80
        Mov ax,0b800h
        Mov es,ax
        Mov ax,0720h
        Mov di,0
        Rep stosw


        ;now restoring the contents of the screen
       Mov ax,Data_seg
       Mov ds,ax
       Mov cx,25*80
       Mov di,0
       Mov si,OFFSET Data_seg
       Rep movsw





     Mov Ah,4ch
     Int 21h


Main endp
Codeseg ends
     end Main

Solution

  • You confused "Dataseg" (Name of a segment) and "Data_seg" (Name of a variable). Mov ax,Data_seg loads the offset of the Variable Data_seg, not the segment. There are two options to load the segment:

    Mov ax, SEG Data_seg    ; Load the segment of variable Data_seg
    

    or

    Mov ax, Dataseg         ; Load the segment called "Dataseg"