      .286C
      extrn   cls:far,move_cursor:far,disp_message:far,get_num:far
      sseg  segment stack
	       db  128 dup("stack---")
      sseg  ends
      dseg  segment
      
      menu         db   13,10,15 dup (32), '1- add  record ' 
		   db   13,10,15 dup (32), '2- list records'
		   db   13,10,15 dup (32), '3- exit program'
		   db   13,10,14 dup (32), ' Select (1--3): ',"$"

      more         db   'prees enter to continue $'              
      open_error   db   13,10,"open error in file TEL.dat " ,"$"
      read_error   db   13,10, "read error " ,"$"
      close_error  db   13,10,"close error " ,"$"
      write_error  db   13,10, "write error " ,"$"
      
      namepar    label   byte                             ;parameter list:
      maxlen     db      35                               ;maximum  length
      namelen    db      ?                                ;actual   length
      tel_name    db      35   dup (20h)  ,13,10         ;name and  tel
      len_name   dw       ?
      

      prompt       db    3 dup (13,10),'enter name and tel_no:',"$"
      key          db    32
      out_tel_name db      37   dup (32),"$"       ;output name and tel
      
      file_name   db     'tel.dat' , 0
      in_handle   dw     ?
      byte_read   dw      ?
      lenmax      dw      35
      line        db      0
      rowcol      dw      ? 
      crea        db      0
      monitor     equ     0001h
      keyboard    equ     0000h
      read        equ     3fh
      write       equ     40h
      open        equ     3dh
      close       equ     3eh
      bottom      equ     02h 
      top         equ     00h
      create      equ     3ch
      dseg    ends
     cseg    segment
     assume   cs:cseg,ss:sseg,ds:dseg
	    page
	    
    example  proc far

	    push   ds
	    push   0
	    mov    ax, dseg
	    mov    ds, ax
	    
	    clc                                ;clear carry
	    call   open_file                   ;file open
	    mov    in_handle, ax
       while1:     
	    call    cls                         ;clear screen
	    mov     byte ptr rowcol+1, 10       ;row=0
	    mov     byte ptr rowcol, 0          ;col=0
	    push    rowcol  
	    call    move_cursor
	    push    offset menu
	    call    disp_message
	    lea     dx, key
	    call    get
	    cmp     byte ptr key, '1'
	    jne     cmp_2 
	    call    append_record 
	    jmp     while1 
	  cmp_2:  
	    cmp     byte ptr key, '2'
	    jne     cmp_3 
	    call    list_record 
	    jmp     while1 
	  cmp_3:  
	    cmp     byte ptr key, '3'
	    je      end1 
	    jmp     while1 
	 end1:
	      mov   bx, in_handle
	      call  close_file
	ret
    example    endp
;************************************
;************************************
get      proc           
	    mov   ah, 10h            ;request read key
	    int   16h
	    mov    ah, 02             ;request display a char
	    mov    dl, al             ;char for display
	    int    21H
	    mov   key, al
	    
	    ret
get      endp
;************************************
;************************************
append_record      proc           
	   mov     al, bottom
	   call    go_recno
	   push    offset prompt
	   call    disp_message
	   call    get_tel_name
	   cmp    namelen, 00 
	   jz     ret_app                     ;=0 goto ret_app
	   mov    bx, in_handle              ;bx=out_handle
	   call   write_file                  ;write_file(out_handle)
	   ret_app:
	   ret

append_record   endp

;************************************
;************************************
list_record      proc           
	 
	 call   cls                         ;clear screen
	 mov    byte ptr rowcol+1, 0        ;row=0
	 mov    byte ptr rowcol, 0          ;col=0
	 push   rowcol  
	 call   move_cursor                 ;move_cursor(row,col)
	 mov    line, 0                     ;line=0
	 mov    al, top                     ;recno=top
	 call   go_recno                    ;go top
	 list1:   
	    call   read_file                    ;read_file()
	    mov    byte_read, ax                ;byte_read=number of read
	    cmp    byte_read, 0                 ;byte_read ? 0
	    je     ret_list                     ;= then goto ret_list
	    inc    line                         ;line=line+1
	    cmp    line, 22                     ;if line < 22
	    jne    line_1_21                    ; then   goto line_1_22
						;begin else
	    push   offset more                  ; 
	    call   disp_message                 ;disp_message(more)
	    lea    dx, key                      ;
	    call   get                          ;get(key)
	    
	    call   cls                          ;cls()
	    mov    byte ptr rowcol+1, 0         ;row=0
	    mov    byte ptr rowcol, 0           ;col=0
	    push   rowcol  
	    call   move_cursor                  ;move_cursor(row,col)
	    mov    line, 0                      ;line=0
						;end else
	 line_1_21:   
	    
	    push   offset out_tel_name
	    call   disp_message                 ;disp_message(tel_name)
	    jmp    list1                        ;goto loop1
	 
	 ret_list:
						;begin else
	    push   offset more                  ; 
	    call   disp_message                 ;disp_message(more)
	    lea    dx, key                      ; dx=offset key 
	    call   get                          ;get(key)
	
	ret
	   
list_record   endp


;************************************
;************************************
open_file      proc           
	    
	    lea    dx, file_name          ;file name
	    mov    ah, open               ;repuest  function  
	    mov    al, 02h                ;open input  output file
	    int    21h
	    jc    create_open             ;if error  then go create_open
	    ret
	 create_open: 
	     clc
	     mov   ah, create              ;request  create file
	     mov   al, 00h                 ;create  file
	     mov   cx, 0000h
	     int   21h                    
	     jc    error_open              ;if  error  then goto error_open
	     ret
	 error_open:   
	    push   offset open_error
	    call   disp_message            ;display   open error
	    add    sp, 2                   ;pop two  bytes
	    jmp    end1
open_file   endp
;************************************
;************************************
read_file       proc           
	    
	    mov    ah, read                  ;request  read
	    lea    dx, out_tel_name          ;buffer
	    mov    bx, in_handle             ;set device
	    mov    cx, 37                    ;length
	    int    21h
	    jc    error_read                 ;if error then goto error_read 
	    ret
	 error_read:   
	    push   offset read_error
	    call   disp_message              ;display error read  message
	    add    sp, 2                     ;pop two bytes
	    jmp    end1
read_file   endp
;************************************
;************************************
get_tel_name      proc           
	    
	    mov   ah, 0ah                       ;request  input
	    lea   dx, namepar                   ;accept   name
	    int   21h
	    cmp   namelen, 00 
	    jz    ret_get                       ;no eixt
	    mov   si,0                       ;blank for storing
	  find_enter:  
	    cmp   si,34                      ;tel name len
	    jg    ret_get
	    cmp   tel_name[si],13
	    je    enter_exist
	    inc   si
	    jmp   find_enter
	 enter_exist:   
	    mov   al, 32                       ;blank
	    mov   tel_name[si], al             ;move blanks  to tel_name
	    inc   si                           ;si=si+1
	    cmp   si, 35                       ;if si< 35 then
	    jl    enter_exist                  ;goto  enter_exist
	  ret_get: 
	    
	    
	    ret
get_tel_name    endp
;************************************
;************************************
write_file       proc           
	    
	    mov   ah, write                     ;request write
	    mov   bx, in_handle                 ;set  file handle
	    lea   dx, tel_name                  ;set file name
	    mov   cx, 37                        ;35 byte name tel,2 byte cr/lf
	    int   21h                           ;call inttrupt service
	    jc   error_write                    ;if error then goto error_write
	    ret
	  error_write:  
	    push   offset write_error
	    call   disp_message                 ;disp_message(write_error)
	    add    sp, 2                        ;pop   two byte
	    jmp   end1                          ;goto  end1
	    
write_file   endp


;************************************
;************************************
close_file       proc           
	    
	    mov    ah, close               ;request close
	    int    21h                     ;call inttrupt
	    jc    error_close              ;if error then  goto error_close 
	    ret
	 error_close:   
	    push   offset close_error
	    call   disp_message            ;display   error close file
	    add    sp, 2                   ;pop two bytes
	    jmp    end1
	    
close_file   endp

;************************************
;************************************
go_recno      proc
	      mov      ah, 42h           ;request   move pointer
	      mov      bx, in_handle     ;set file  handle  
	      mov      cx, 0             ;upper portion of offset
	      mov      dx, 0             ;lower porton  of offset 
	      int      21h               ;call interrupt 
	      ret
go_recno      endp   
   cseg   ends
	  end    example
