Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Specify user input file

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
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.
 
Try researching the PSP. The complete command line is contained there, and if you specify a file name in the command line it wiould be there.
Use function 62H of INT 21H to get the segment of the PSP, then the command tail (what you are looking for) is located at offset 81H, with the length byte at 80H. Word of caution though, you must read the command tail before using any FCB File operations, as the FCB operations use the same area as a data transfer area.
 
OK. If you want the user to enter the file name here is the code:

Source DB 32 dup(0) ;Place to keep name

MOV AX,SEG Source
MOV DX,OFFSET Source
;------------------------------
MOV DS,AX
MOV AH,0Ah ;Code to read string
MOV SI,DX ;from user
MOV byte ptr [SI],30
INT 21h
;------------------------------
INC SI
XOR CX,CX
MOV CL,[SI] ;CL holds num of bytes read
ADD CX,2 ;(actually there are 2 more)
ADD DX,CX
MOV SI,DX ;Si points to end of string
MOV byte ptr [SI],0 ;put '0' at the end.

Now you can open the file the same way you did.
 
If you want to read the file from the command line here is the code:

Source DB 32 dup(0) ;Place to keep name

MOV BX,80h
XOR CX,CX
MOV CL,[BX] ;Num of chars in CL
SUB CX,1 ;minus the space
MOV BX,82h ;start of string
MOV SI,BX
MOV DI,OFFSET Source
REP MOVSB ;copy string

MOV BX,DI ;end of string
MOV BYTE PTR [BX],0 ;Put '0' the end

You might want to combine both methods. Just check to see whether [80h] == 0, and if it does ask the user for the name of the file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top