(Assembly source code) print file program (print files)

Source: Internet
Author: User
Tags exit command line reset save file

INTRODUCTION

The following example is a simple program to read a file and print the contents to a standard printer. It gets the filename of the file to print from the DOS command prompt input line. The prompt input information is passed to the "program" in a buffer area of the "program Segment Prefix (PSP). The address of the "program Segment Prefix are passed to the" in the "ES and DS registers when the" program execution s Tarts. This is the program checks the keyboard between every character printed for an escape key code to terminate execution of the prog Ram.

; This program prints a file defined on the command line
. MODEL Small
; ************* Stack Section *********************
. STACK 500
; ************* Data Section **********************
. DATA
PSP_SEG DW 0
No_cl_mess db "This routine requires that a"
DB ' filename is on the command ' for printing.
DB 0dh,0ah, "Please try with a filename.", 0dh,0ah, "$"
File_bad_open db "Bad File Open", 0dh,0ah, "$"
File_bad_read db "Bad file read", 0dh,0ah, "$"
Printer_bad_mess DB "!! Printer Error!!!! ", 0DH,0AH," $ "
Printing_mess db "A file is being printed,", 0dh,0ah
DB "To stop printing, press ESC key", 0dh,0ah, "$"
FileName DB 128 dup (0)
File_handle DW 0
File_count DW 0
File_pointer DW Offset File_buffer
File_buffer DB 1024 dup (0)
; ************* ----------- *********************
; ************* Code Section *******************
. CODE
Start proc near
;D s and ES are indexing PSP area
mov al,[ds:80h]; load al with size: Data line
MOV dx, @data; Get segment address to data area
mov ds,dx;p oint DS to Data area
MOV psp_seg,es; save PSP Address
CMP al,1;?? Data in DOS command line??
JA get_psp_filename; branch if data found
; If here, there are no data on command line
;d isplay error message to user and terminate
Lea Dx,no_cl_mess
;-------------------------
Terminate_display:
;D isplay message Indexed by DX then terminate
MOV ah,09
int 21H;D OS call
;-------------------------
Terminate_program:
; Terminating the program
MOV ah,4ch set AH for terminating function
mov al,00; set terminating code variable
int 21H; call DOS to terminate
;------------------------------------------
; %%%%%%%%%%%%% ----------- %%%%%%%%%%%%%%%
Get_psp_filename:
; move PSP filename to filename-buffer in We data area
MOV Ax,ds
mov es,ax;p oint es to data segment
MOV ds,psp_seg
mov si,82h si source is PSP data area
Lea Di,filename
CLD; make strings go forward
Get_psp_data_1:
LODSB Load String Data byte
; Check for end of filename
CMP al,21h
; branch if end of string
JB Got_psp_filename
STOSB Store String Data byte
JMP get_psp_data_1
Got_psp_filename:
MOV al,0
STOSB make Asciiz string with zero end
Push ES
Pop ds; Reset data segment pointer
; Try to open file
MOV AH,3DH
Lea Dx,filename
MOV al,0 Read access code
int 21H;D OS call
Jnc FILE_OPEN_OK
Lea Dx,file_bad_open
JMP Terminate_display
;+++++++++++++++++++++++++++++++++++++++++++
;############### +++++++++++ ###############
FILE_OPEN_OK:
; Save file handle
MOV File_handle,ax
Lea dx,printing_mess;d isplay Start message
MOV ah,09
int 21H;D OS call
File_read:
; Read in block of file data
MOV ah,3fh
Lea Dx,file_buffer
MOV cx,1024
MOV Bx,file_handle
int 21H;D OS call
JNC FILE_READ_OK Branch if good read
; Else read file error occurred
; Close File
MOV Ah,3eh
MOV Bx,file_handle
int 21H
; Index exit error message
Lea Dx,file_bad_read
JMP Terminate_display
FILE_READ_OK:
; Check to the If no more file data
CMP ax,0
Je close_file branch if no data left
; Else reset data block size and pointer
MOV File_count,ax
Lea Bx,file_buffer
MOV file_pointer,bx
;!!!!!!!!!!!!!!!!! ^^ ^^ ^^ ^^!!!!!!!!!!!!!!!!!!!!
Print_data_block:
; main loop to print block of file data
; scan keyboard to check for the any keys
MOV ah,1
int 16H
JZ print_data_block_1; branch if no key
; Get key code out of buffer
MOV ah,0
int 16H; Call BIOS keyboard
CMP al,01bh Check Key code
Je close_file; branch if ESC
Print_data_block_1:
MOV Si,file_pointer
MOV Al,[si]
MOV ah,0
MOV dx,0 select LPT1
int 17H; BIOS Call
Test ah,25h
JNZ printer_error
Inc si
MOV File_pointer,si
Dec file_count
JNZ print_data_block; loop if more data
; else go read in next block of file data
JMP File_read
;!!!!!!!!!!!!!!!! ^^ ^^ ^^ ^^!!!!!!!!!!!!!!!!!!!!
Close_file:
MOV Ah,3eh
MOV Bx,file_handle
int 21H;D OS call
JMP Terminate_program
;-------------- ?????????? -------------------
Printer_error:
; Index exit error message
Lea Dx,printer_bad_mess
JMP Terminate_display
;_______________________________________________
Start ENDP end of Start procedure
End start;d efine start as beginning of

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.