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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Line Numbering

Status
Not open for further replies.

chris921

Technical User
Mar 9, 2004
82
GB
Can anyone help me? I've written some ASM code to print to the screen some text from a specified file, I would like it to print a line number into the file, so for each line there is its number (ie first line is 1...) and then its text.

Can anyone give me any ideas how to go about it?

Cheers

 
Code:
JMP Start
cr	equ	13h
lf	equ	10h
msg1	db	'Cannot Open File, Aborting Program...',cr,lf,'$'
msg2	db	'Cannot Open Output File, Aborting Program...',cr,lf,'$'
msg3	db	'Cannot Write Data to File, Aborting Program...',cr,lf,'$'
msg4	db	'Cannot Reading from File, Aborting Program...',cr,lf,'$'
FileName	db	'original.txt',0
OutputFile	db	'Output.txt',0
FileHandle1	dw	?
FileHandle2	dw	?
Buffer		db	?
Position	dw	0

Start:
	mov ah,3dh
	mov al,0
	lea dx,FileName
	int 21h
	jnc GoodOpen
	lea dx,msg1
	mov ah,9
	int 21h
	jmp PgmExit
GoodOpen:
	mov FileHandle1,ax
	mov ah,3ch
	mov cx,0
	lea dx,OutputFile
	int 21h
	jnc GoodOpen2
	lea dx,msg2
	mov ah,9
	int 21h
	mov ah,3eh
	mov bx,FileHandle1
	int 21h
	jmp PgmExit
GoodOpen2:
	mov FileHandle2, ax
ReadFileLP:
	mov bx,FileHandle1
	mov cx,1
	lea dx,Buffer
	mov ah,3Fh
	int 21h
	jc BadRead
	cmp ax,1
	jz ReadOK
	jmp AtEOF
ReadOK:
	mov al,Buffer
	cmp al,cr
	jb NotLower
	cmp al,lf
	ja LineNo
	mov al, 5fh
NotLower:
	mov Buffer,al
	mov bx,FileHandle2
	mov cx,1
	lea dx,buffer
	mov ah,40h
	int 21h
	jc BadWrite
	cmp ax,1
	jz ReadFileLP
LineNo:
	;here is the line number stuff
BadWrite:
	lea dx,msg3
	mov ah,9
	int 21h
	jmp AtEOF
BadRead:
	lea dx,msg4
	mov ah,9
	int 21h
AtEOF:
	mov bx,FileHandle1
	mov ah,3eh
	int 21h
	mov bx,FileHandle2
	mov ah,3eh
	int 21h
PgmExit:
	mov ah,4ch
	int 21h

that is what i have so far, i am searching for carriage returns or line feeds.

i dont know how to make it tell me the line number.

please can someone help?

thank you.
 
The only way to know the line number is by counting the carriage returns in the file.

I suggest you use the code you have written to count the carriage returns, and when it finds a CR, output the counter value to the second file before the rest of the line is output to the second file.

I hope that makes sense. If not I'll write some code which you'll understand better...
:)

Don't hate the player - hate the game
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top