title hw4b.asm: extra procs 2
public get_name,file_name
.model small
if1
include mymacros.lib
endif
.data
Screen_buffer db 240 dup (?),'$'    ;temporarily hold contents of 3 lines

file_buffer  label byte             ;file name string
max_len       db 9
act_len       db ?
file_name        db 13 dup (?)      ;holds asciiz file name
             
ext db '.txt0'
.code
get_name proc near
;lets user input name of file to be saved as
;input: es points to data segment
;output: file_name contains name file will be saved as, in asciiz format

  save_regs <ax,bx,cx,dx,es,di,si>  ;push registers to stack macro
  mov ah,3                          ;get ready to read cursor position
  xor bh,bh                         ;at page 0
  int 10h                           ;read cursor position
  push dx                           ;save cursor position


  mov dx, 0b00h                     ;Dx=position of the beginning of middle  3-
                                    ;rows                         
  cld                               ;left to right string processing

  lea di,Screen_buffer              ;get offset of 3 line screen buffer
loop_column:
  mov cx,80                         ;for i=1to80
  mov ah,2                          ;move cursor
  int 10h      
  loop_row:
  mov ah,8                          ;read character at cursor
  int 10h
  stosb                             ;store char in screen buffer, di is incremented
  inc dl                            ;DX= position of next character in the line
  mov ah,2                          ;move cursor over 1 column
  int 10h   
  loop loop_row                     ;Next i
  xor dl,dl                         ;reset column number
  inc dh                            ;go one row down
  cmp dh,14                         ;Were three lines put in screen buffer?
  jne loop_column                   ;no, repeat

  mov dx,0b00h                      ;yes move cursor to beginning of middle 3-
  mov ah,2                          ;rows
  int 10h    
  mov ah,0ah                        ;write character function
  mov al,20h                        ;write space
  mov cx,240                        ;for 3 lines (clear middle three lines)
  int 10h                           ;execute write instruction
  mov dx,0c00h                      ;move cursor to middle row
  mov ah,2                     
  int 10h                         
  Disp_Str 'Save text file as: $'   ;display string macro
  mov ah,0ah                        ;input string in file buffer
  lea dx,file_buffer               
  int 21h                          
  mov si,dx                         ;move offset of file buffer to si
  xor ch,ch                         ;clear ch
  mov cl,[si+1]                     ;place act_len in cl
  add si,2                          ;si points to first char
  lea di,file_name                  ;di points to file_name
  rep movsb                         ;move chars from file_buffer to file_name
  mov cx,5                          ;add .txt0 to file_name to give it text file-
  lea si,ext                        ;extension and put it in asciiz format
  rep movsb                        
  mov dx,0b00h                      ;place cursor to beginning of middle 3 lines
  mov ah,2                        
  int 10h                         
  lea dx,Screen_buffer              ;get offset of screen buffer
  mov ah,9                          ;restore screen
  int 21h                         
  mov ah,2                          ;move cursor function
  pop dx                            ;restore original cursor location
  int 10h                           ;move cursor
  restore_regs <si,di,es,dx,cx,bx,ax>   ;restore registers
  ret                               ;return to main program
get_name endp
end

