Guest_imported
New member
- Jan 1, 1970
- 0
I've spent some time on a program that reads a specific input file and outputs the contents to the screen and counts alphanumerical characters. (using Turbo Assembler - 8086 Assembly) What I'm having trouble with is being able to allow users to input a file name, and display it's contents to the screen. I mark the EOF with '@' for simplification purposes. Here is my code. I left the code with a default input file. Any ideas?
.MODEL SMALL
.STACK 100h
.DATA
filename1 db '.\church.dat',0
filename2 db '.\out.txt',0
handle_in dw ?
handle_out dw ?
size1 dw 1
buffer1 db ?
buffer2 dw ?
errormsg1 db 10,13,10,13,7,' File I/O Error...' ,10,13,'$'
msg1 db '** 8086 Assembly ** ** File Input/Output' ,'$'
msg2 db 10,13,'Complete...','$'
msg3 db 10,13,10,13,'File handler successful... ', '$'
msg4 db 10,13,'Output file: OUT.TXT ',10,13,10,13, '$'
msg41 db 10,13,'Intput file: CHURCH.DAT ', '$'
msg5 db 10,13,10,13,'Total Count: ', '$'
msg6 db ' <== Aphanumeric characters counted ', '$'
count dw 0
TEN dw 10
.CODE
start:
mov ax, @data
mov ds,ax
mov ah, 09h
mov dx, OFFSET msg1
int 21h
mov ah, 09h
mov dx, OFFSET msg3
int 21h
mov ah, 09h
mov dx, OFFSET msg41
int 21h
mov ah, 09h
mov dx, OFFSET msg4
int 21h
;Open an existing file to read (default: in.txt)
mov dx, OFFSET filename1
mov ah, 03Dh ;Open file code
mov al, 2 ;0-Read 1-Write 2-Both
int 21h
;jc Error ;Error caused by carry
mov [handle_in],ax
;Create a new file to write (default: out.txt)
mov dx, OFFSET filename2
mov ah, 03Ch ;Create new file code
xor cx,cx ;cx=0-normal 1-RO 2-H 3-Sys
int 21h
;jc Error
mov [handle_out],ax
mov dx,1 ;size of record
mov ah,042h ;Reset file code
mov al,0 ;0-file head 1-current pos 2-file tail
mov bx, [handle_in]
int 21h
;jc Error
mov dx,1 ;size of record
mov ah,042h ;Reset file code
mov al,0 ;0-file head 1-current pos 2-file tail
mov bx, [handle_out]
int 21h
;jc Error
R_W:
;Reading file
mov ah, 3Fh ;File-read code
mov bx, [handle_in]
xor cx,cx ;Clear cx
mov cx,1 ;Read 1 bytes
mov dx, OFFSET buffer1 ;buffer1 is db (1 byte)
int 21h
jc Error
mov ah, 02h
xor dx,dx
mov dl, [buffer1]
int 21h
;*********BEGIN -> CHECK IF LOWER CASE************
cmp [buffer1], 97
jnge check_if_num
cmp [buffer1], 122
jnle check_if_num
inc count
;*********END -> CHECK IF LOWER CASE************
;*********BEGIN -> CHECK IF NUMBER (0-9)************
check_if_num:
cmp [buffer1], 48
jnge check_if_upper
cmp [buffer1], 57
jnle check_if_upper
inc count
;*********END -> CHECK IF NUMBER (0-9)************
;*********BEGIN -> CHECK IF UPPER CASE************
check_if_upper:
cmp [buffer1], 65
jnge writeto
cmp [buffer1], 90
jnle writeto
inc count
;*********END -> CHECK IF UPPER CASE************
writeto:
;Writing to file
mov ah, 40h ;File-write code
mov bx, [handle_out]
xor cx,cx
mov cx,1 ;Write 1 byte
mov dx, OFFSET buffer1
int 21h
jc Error
cmp [buffer1],'@'
jne R_W
;Closing files
mov bx, [handle_in] ;Close input file
mov ah, 03Eh
int 21h
jc Error
mov bx, [handle_out] ;Close output file
mov ah, 03Eh
int 21h
jc Error
jmp out_count
Error:
mov ah, 09h ;Display error warning
mov dx, OFFSET errormsg1
int 21h
out_count:
mov ax,0
mov ax,count
mov cx,0
Itoa:
mov dx,0
div TEN ;Product in DX+AX
push dx ;DX:Remainder
inc cx
cmp ax,0 ;Much complicated for large num in DX
jnz Itoa
mov ah, 09h
mov dx, OFFSET msg5
int 21h
Print:
mov dx,0
pop dx
add dx,48
mov ah, 02h
int 21h
loop Print
mov ah, 09h
mov dx, OFFSET msg6
int 21h
Exit:
mov ah, 09h
mov dx, OFFSET msg2
int 21h
mov ah, 4ch
int 21h
END start
-------------------------------------------------------
If it helps, a working executable file that I was working from can be downloaded here. mailto://8086assembly:assembly@assembly.mine.nu
(where is says mailto: should be ftp)
file is demo9c.exe and church.dat
Any help is greatly appreciated. Thanks.
.MODEL SMALL
.STACK 100h
.DATA
filename1 db '.\church.dat',0
filename2 db '.\out.txt',0
handle_in dw ?
handle_out dw ?
size1 dw 1
buffer1 db ?
buffer2 dw ?
errormsg1 db 10,13,10,13,7,' File I/O Error...' ,10,13,'$'
msg1 db '** 8086 Assembly ** ** File Input/Output' ,'$'
msg2 db 10,13,'Complete...','$'
msg3 db 10,13,10,13,'File handler successful... ', '$'
msg4 db 10,13,'Output file: OUT.TXT ',10,13,10,13, '$'
msg41 db 10,13,'Intput file: CHURCH.DAT ', '$'
msg5 db 10,13,10,13,'Total Count: ', '$'
msg6 db ' <== Aphanumeric characters counted ', '$'
count dw 0
TEN dw 10
.CODE
start:
mov ax, @data
mov ds,ax
mov ah, 09h
mov dx, OFFSET msg1
int 21h
mov ah, 09h
mov dx, OFFSET msg3
int 21h
mov ah, 09h
mov dx, OFFSET msg41
int 21h
mov ah, 09h
mov dx, OFFSET msg4
int 21h
;Open an existing file to read (default: in.txt)
mov dx, OFFSET filename1
mov ah, 03Dh ;Open file code
mov al, 2 ;0-Read 1-Write 2-Both
int 21h
;jc Error ;Error caused by carry
mov [handle_in],ax
;Create a new file to write (default: out.txt)
mov dx, OFFSET filename2
mov ah, 03Ch ;Create new file code
xor cx,cx ;cx=0-normal 1-RO 2-H 3-Sys
int 21h
;jc Error
mov [handle_out],ax
mov dx,1 ;size of record
mov ah,042h ;Reset file code
mov al,0 ;0-file head 1-current pos 2-file tail
mov bx, [handle_in]
int 21h
;jc Error
mov dx,1 ;size of record
mov ah,042h ;Reset file code
mov al,0 ;0-file head 1-current pos 2-file tail
mov bx, [handle_out]
int 21h
;jc Error
R_W:
;Reading file
mov ah, 3Fh ;File-read code
mov bx, [handle_in]
xor cx,cx ;Clear cx
mov cx,1 ;Read 1 bytes
mov dx, OFFSET buffer1 ;buffer1 is db (1 byte)
int 21h
jc Error
mov ah, 02h
xor dx,dx
mov dl, [buffer1]
int 21h
;*********BEGIN -> CHECK IF LOWER CASE************
cmp [buffer1], 97
jnge check_if_num
cmp [buffer1], 122
jnle check_if_num
inc count
;*********END -> CHECK IF LOWER CASE************
;*********BEGIN -> CHECK IF NUMBER (0-9)************
check_if_num:
cmp [buffer1], 48
jnge check_if_upper
cmp [buffer1], 57
jnle check_if_upper
inc count
;*********END -> CHECK IF NUMBER (0-9)************
;*********BEGIN -> CHECK IF UPPER CASE************
check_if_upper:
cmp [buffer1], 65
jnge writeto
cmp [buffer1], 90
jnle writeto
inc count
;*********END -> CHECK IF UPPER CASE************
writeto:
;Writing to file
mov ah, 40h ;File-write code
mov bx, [handle_out]
xor cx,cx
mov cx,1 ;Write 1 byte
mov dx, OFFSET buffer1
int 21h
jc Error
cmp [buffer1],'@'
jne R_W
;Closing files
mov bx, [handle_in] ;Close input file
mov ah, 03Eh
int 21h
jc Error
mov bx, [handle_out] ;Close output file
mov ah, 03Eh
int 21h
jc Error
jmp out_count
Error:
mov ah, 09h ;Display error warning
mov dx, OFFSET errormsg1
int 21h
out_count:
mov ax,0
mov ax,count
mov cx,0
Itoa:
mov dx,0
div TEN ;Product in DX+AX
push dx ;DX:Remainder
inc cx
cmp ax,0 ;Much complicated for large num in DX
jnz Itoa
mov ah, 09h
mov dx, OFFSET msg5
int 21h
Print:
mov dx,0
pop dx
add dx,48
mov ah, 02h
int 21h
loop Print
mov ah, 09h
mov dx, OFFSET msg6
int 21h
Exit:
mov ah, 09h
mov dx, OFFSET msg2
int 21h
mov ah, 4ch
int 21h
END start
-------------------------------------------------------
If it helps, a working executable file that I was working from can be downloaded here. mailto://8086assembly:assembly@assembly.mine.nu
(where is says mailto: should be ftp)
file is demo9c.exe and church.dat
Any help is greatly appreciated. Thanks.