

public  rollback
data segment common
    lines   dw  500 dup(?)
    cur_line  dw  0
    Pgsize  dw  ?
    buf_size    db   80
    s_buf       db  ?
    buf   db    200 dup(?)
    names  db    20 dup(?)
    cur     dw   ?
    handle  dw  ?
    buf_tmp db  24*80 dup(?)
    cur_tmp dw ?
    name_tmp  db  "t0m1p",0
    handle_tmp  dw ?
    mark    db  ?
    
    mess_getname  db  0dh,0ah,"    Please input filename: $"
    mess_err1     db  0ah,0dh,"    Illegal filename ! $"
    mess_err2     db  0ah,0dh,"    File not found !$"
    mess_err3     db  0ah,0dh,"    File read error !$"
    mess_psize    db  0ah,0dh,"    Page Size : $"
    mess_dele     db  0dh,0ah,"    The last page is deleted !"    
    crlf          db  0ah,0dh,"$"
    mess_star     db  0ah,0dh,"********************************************"
		  db  0ah,0dh,"$"
data ends

code1 segment
    assume  cs:code1,ds:data
    rollback proc far

    mov   bx,Pgsize
    shl   bx,1
    mov   cx,bx		; need to back double page for the source file.
    call  count_char	; count the number of chars need to back (dx).

    cmp   dx,0
    jne   next
    ret
next:
    mov   bx,200
    sub   bx,cur
    add   dx,bx		; Double page + chars not displayed in buf to back.
    mov   bx,handle
    call  movefilepointer    

    mov   cx,Pgsize
    sub   cur_line,cx
    jle   ok
    call  count_char	; the temp file move back one page, because
			;    the last page in buf_tmp have not been
			;    writen into the temp file.
    mov   bx,handle_tmp
    call  movefilepointer
    
    mov   cx,Pgsize
    sub   cur_line,cx
    jge   ok
    mov   cur_line,0
ok:
    mov   cur,200
    mov   mark,0
    mov   cur_tmp,0

    ret
rollback endp

count_char proc near
    mov  dx,0
    mov  bx,cur_line
again:
    sub  bx,2
    jl   ok
    add  dx,lines[bx]
    loop  again
ok:
    ret
count_char endp

movefilepointer proc near
    push  dx
    neg   dx
    mov   cx,0
    not   cx
    mov   ah,42h
    mov   al,1
    int   21h
    pop   dx
    ret
movefilepointer endp

code1 ends
end
