English Amiga Board


Go Back   English Amiga Board > Coders > Coders. System

 
 
Thread Tools
Old 06 February 2014, 03:48   #1
hukka
Wolf-bear thing
 
hukka's Avatar
 
Join Date: Jan 2014
Location: Finland
Age: 41
Posts: 56
Floppy disk Reading files from current directory in assembly

Hello EAB!

I'd like to read a file (the filename is known) from the directory the executable was launched from, regardless of whether it's from a floppy or hard drive. I'm using assembly and would like this to work on a plain Amiga 500. This will be my first time coding any sort of file I/O.

Any pointers or even source code to help me get started?
hukka is offline  
Old 06 February 2014, 04:32   #2
marduk_kurios
move $dff006,$dff180
 
marduk_kurios's Avatar
 
Join Date: Feb 2014
Location: USA
Posts: 66
I've just had seen this, it seems it could help you.

Code:
;*******************************************************
;*     Window Example show how to make some basic      *
;*  library calls. And how to use the include files    *
;*                                                     *
;*      ASM-One example coded by Rune Gram-Madsen      *
;*                                                     *
;*       All rights reserved. Copyright (c) 1990       *
;*******************************************************

	INCDIR	df0:include/

;---  Open library  ---

	include	exec/exec_lib.i
	include	libraries/dos_lib.i
	include	libraries/dos.i
	include	intuition/intuition_lib.i

J	LEA	PATH(PC),A0		; Default CLI message
	MOVE.L	A0,-(A7)		; Store CLI message

	BSR.W	DOS_LIBOPEN		; Open dos library
	BSR.L	WINDOW_OPEN		; Open window
	BSR.L	INT_LIBOPEN		; Open intuition library
	BSR.L	WORKBENCH_FRONT		; Put the workbench to front

	MOVE.L	(A7)+,D1		; Path pointer
	MOVEQ	#-2,D2			; Read mode
	CALLDOS	Lock			; Lock
	TST.L	D0
	BEQ.S	.ERROR			; Not found ??
	MOVE.L	D0,D5

	MOVE.L	D5,D1
	MOVE.L	#FILEINFO,D2
	CALLDOS	EXAMINE			; Examine first lock
	TST.L	D0
	BEQ.S	.ERROR

	MOVE.L	#TEXT2,D2
	MOVEQ	#TEXT2_END-TEXT2,D3
	MOVE.L	_WINDOWHANDLE(PC),D1
	CALLDOS	Write

.LOOP	MOVE.L	#FILEINFO+8,D0		; Disk/Dir/File-name ptr
	BSR.W	TEXT_PRINT		; Print name

	MOVE.L	D5,D1
	MOVE.L	#FILEINFO,D2
	CALLDOS	EXNEXT			; Next name on the disk
	TST.L	D0
	BNE.B	.LOOP

.ERROR	CALLDOS	IOERR			; Get message

;--- Close down ---

	MOVE.L	#TEXT1,D2
	MOVEQ	#TEXT1_END-TEXT1,D3
	MOVE.L	_WINDOWHANDLE(PC),D1
	CALLDOS	Write

	MOVE.L	#RETURN,D2
	MOVEQ	#1,D3
	MOVE.L	_WINDOWHANDLE(PC),D1
	CALLDOS	Read

	BSR.L	WORKBENCH_BACK		; workbench back again
	BSR.W	INT_LIBCLOSE		; close intuition lib
	BSR.S	WINDOW_CLOSE		; Close window
	BSR.S	DOS_LIBCLOSE		; close dos
	RTS

;--- Open dos library ---

DOS_LIBOPEN
	LEA.L	_DOSNAME(PC),A1
	MOVEQ	#0,D0
	CALLEXEC OpenLibrary
	MOVE.L	D0,_DOSBASE
	RTS

;--- Close dos library ---

DOS_LIBCLOSE
	MOVE.L	_DOSBASE(PC),A1
	CALLEXEC CloseLibrary
	RTS

;---  Print text  ---

TEXT_PRINT:
	MOVE.L	D0,A0
.LOOP	TST.B	(A0)+
	BNE.S	.LOOP
	SUB.L	D0,A0
	SUBQ.L	#1,A0
	MOVE.L	A0,D3
	MOVE.L	D0,D2
	MOVE.L	_WINDOWHANDLE(PC),D1
	CALLDOS	Write
	MOVE.L	#RETURN,D2
	MOVEQ	#1,D3
	MOVE.L	_WINDOWHANDLE(PC),D1
	CALLDOS	Write
	RTS

;--- Open window ---

WINDOW_OPEN:
	MOVE.L	#_WINDOWNAME,D1
	MOVE.L	#MODE_OLDFILE,D2
	CALLDOS	Open
	MOVE.L	D0,_WINDOWHANDLE
	RTS

;--- Close window ---

WINDOW_CLOSE
	MOVE.L	_WINDOWHANDLE(PC),D1
	CALLDOS	Close
	RTS

;--- Open intuition library ---

INT_LIBOPEN
	LEA.L	_INTNAME(PC),A1
	MOVEQ	#0,D0
	CALLEXEC OpenLibrary
	MOVE.L	D0,_INTUITIONBASE
	RTS

;--- Close intuition library ---

INT_LIBCLOSE
	MOVE.L	_INTUITIONBASE(PC),A1
	CALLEXEC CloseLibrary
	RTS

;--- Workbench to front ---

WORKBENCH_FRONT
	CALLINT	WbenchToFront
	RTS

;--- Workbench to back ---

WORKBENCH_BACK
	CALLINT	WbenchToBack
	RTS

TEXT1:		DC.B	$9B,'3',$6D,'Press <RETURN> : '
TEXT1_END:
TEXT2:		DC.B	$9B,'3',$6D,'Root name : ',$9B,'0',$6D
TEXT2_END:
RETURN:		DC.B	10
_WINDOWNAME	DC.B	'CON:10/10/400/180/Directory',0
	EVEN
_WINDOWHANDLE	DC.L	0
_DOSNAME	DOSNAME
_DOSBASE	DC.L	0
_INTNAME	DC.B	'intuition.library',0
_INTUITIONBASE	DC.L	0

PATH:		DC.B	'DF0:',0,0

	CNOP	0,4

FILEINFO:	DCB.L	260,0
marduk_kurios is offline  
Old 06 February 2014, 21:03   #3
hukka
Wolf-bear thing
 
hukka's Avatar
 
Join Date: Jan 2014
Location: Finland
Age: 41
Posts: 56
Quote:
Originally Posted by marduk_kurios View Post
I've just had seen this, it seems it could help you.
Thanks! That piece of code seems to be hardcoded to load from DF0:, though.

Anyway, I've got my program to load files now, thanks to a piece of code I found on these forums (star4kd.s)! Only problem is, it doesn't try to figure out the current directory, so it seems to only work properly if the files are in the drive root.

Some more Googling finally revealed that apparently the executable's directory is contained in pr_CurrentDir, so I need to figure out how to read and make use of that in assembly. Again, any help would be appreciated!
hukka is offline  
Old 06 February 2014, 22:23   #4
Asman
68k
 
Asman's Avatar
 
Join Date: Sep 2005
Location: Somewhere
Posts: 828
Quote:
Originally Posted by hukka View Post
Hello EAB!

I'd like to read a file (the filename is known) from the directory the executable was launched from, regardless of whether it's from a floppy or hard drive. I'm using assembly and would like this to work on a plain Amiga 500. This will be my first time coding any sort of file I/O.

Any pointers or even source code to help me get started?

I assume that you are in directory for example sys:work/project/
and then you start the executable no matter if this is workbench or cli.
So its very simple to read the file.

1. open dos.library
2. open file ( filename: dc.b "file",0 )
3. read file
4. close file
5. close dos.library

If you need an asm example then I will post one, just give me a sign.
Asman is offline  
Old 06 February 2014, 23:14   #5
Galahad/FLT
Going nowhere
 
Galahad/FLT's Avatar
 
Join Date: Oct 2001
Location: United Kingdom
Age: 50
Posts: 8,986
Quote:
Originally Posted by hukka View Post
Thanks! That piece of code seems to be hardcoded to load from DF0:, though.

Anyway, I've got my program to load files now, thanks to a piece of code I found on these forums (star4kd.s)! Only problem is, it doesn't try to figure out the current directory, so it seems to only work properly if the files are in the drive root.

Some more Googling finally revealed that apparently the executable's directory is contained in pr_CurrentDir, so I need to figure out how to read and make use of that in assembly. Again, any help would be appreciated!
Change 'DF0:' string to 'SYS:' instead
Galahad/FLT is offline  
Old 07 February 2014, 02:19   #6
hukka
Wolf-bear thing
 
hukka's Avatar
 
Join Date: Jan 2014
Location: Finland
Age: 41
Posts: 56
Thumbs up

Thanks guys! It works now. Turns out my code didn't need any changes, I was just testing it wrong.

While testing, I was running the program from the CLI as "path_to_program/foobar.exe"; to get it to work properly I only needed to first cd to the path and then run the exe. So it's not perfect (since you have to cd to the path first) but good enough for my purposes! (And runs perfectly from Workbench.)
hukka is offline  
Old 07 February 2014, 10:44   #7
phx
Natteravn
 
phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,496
Since OS2.0 you could use PROGDIR: to access the directory where the program was launched from.
phx is offline  
Old 07 February 2014, 11:18   #8
hooverphonique
ex. demoscener "Bigmama"
 
Join Date: Jun 2012
Location: Fyn / Denmark
Posts: 1,624
Quote:
Originally Posted by hukka View Post
Some more Googling finally revealed that apparently the executable's directory is contained in pr_CurrentDir, so I need to figure out how to read and make use of that in assembly. Again, any help would be appreciated!
that's not entirely true.. pr_CurrentDir contains a lock to the current directory, not the directory of the executable itself.. there is a way to figure out the directory of the executable without using "PROGDIR:", but I can't remember it off the top of my head..
hooverphonique is offline  
Old 07 February 2014, 12:54   #9
hukka
Wolf-bear thing
 
hukka's Avatar
 
Join Date: Jan 2014
Location: Finland
Age: 41
Posts: 56
Thanks for the replies. Good to know about PROGDIR:! Too bad it doesn't work on 1.2/1.3.

Quote:
Originally Posted by hooverphonique View Post
there is a way to figure out the directory of the executable without using "PROGDIR:", but I can't remember it off the top of my head..
If anyone happens to have this info I'd be very interested in it!
hukka is offline  
Old 07 February 2014, 17:58   #10
marduk_kurios
move $dff006,$dff180
 
marduk_kurios's Avatar
 
Join Date: Feb 2014
Location: USA
Posts: 66
If it's in the path you can use "which". Otherwise I wouldn't know.
marduk_kurios is offline  
Old 09 February 2014, 18:48   #11
Leffmann
 
Join Date: Jul 2008
Location: Sweden
Posts: 2,269
I've looked at it, and unless there's some undocumented method, it seems to me there's only one way:

Find your process with
FindTask(NULL)
, then look at
Process->pr_CLI
to determine if you've been started as a CLI or as a Workbench program.

For Workbench:
Retrieve the WBStartup message sent to you, then simply call
CurrentDir(WBStartup->sm_ArgList[0].wa_Lock)
and you're done.

For CLI:
Look at
Process->pr_CLI->cli_SetName
and
Process->pr_CLI->cli_CommandName
, these fields are the fully qualified path you're currently in, and the command typed on the command line. Together they can form the fully qualified path to your program.

Strip away the name of the program file by working backwards from the end of the command name and removing characters until you hit either "/" or ":", or until the string is empty.

Then search the string for a ":", if you found one then the string is a fully qualified path, and you just call
CurrentDir(Lock( ))
on it and you're done.

If there was no ":" then the command name includes a relative path, and you just concatenate the path you're in, "/", and the command name, in that order, and you have the fully qualified path to call
CurrentDir(Lock( ))
on and you're done.

You can also just prepend your filenames with the path, to avoid locking and changing the directory. Otherwise you must remember to clean up by calling UnLock, and reply to the WBStartup message if you received one. If you ran as a CLI program then you must also call CurrentDir on the previous lock once you're done, or you will confuse the shell.

I think it should work

Last edited by Leffmann; 09 February 2014 at 19:01.
Leffmann is offline  
Old 09 February 2014, 19:43   #12
hooverphonique
ex. demoscener "Bigmama"
 
Join Date: Jun 2012
Location: Fyn / Denmark
Posts: 1,624
yes.. that's similar to how I've done it in the past on at least one occasion, except that I didn't have to support <2.0, so I used pr_HomeDir instead of cli_SetName.
hooverphonique is offline  
Old 10 February 2014, 21:59   #13
hukka
Wolf-bear thing
 
hukka's Avatar
 
Join Date: Jan 2014
Location: Finland
Age: 41
Posts: 56
Many thanks, Leffmann! Filing the info for future use.
hukka is offline  
Old 11 February 2014, 17:31   #14
Leffmann
 
Join Date: Jul 2008
Location: Sweden
Posts: 2,269
Quote:
Originally Posted by Leffmann View Post
I think it should work
I've just written it up and tested it thoroughly, and can now say it works for sure.

Quote:
Originally Posted by hooverphonique View Post
yes.. that's similar to how I've done it in the past on at least one occasion, except that I didn't have to support <2.0, so I used pr_HomeDir instead of cli_SetName.
Yep 2.0 is a lot more convenient than 1.3, but it's still cumbersome to do many things. The AmigaOS APIs have always been very byzantine in this way.
Leffmann is offline  
Old 10 May 2014, 18:18   #15
Photon
Moderator
 
Photon's Avatar
 
Join Date: Nov 2004
Location: Eksjö / Sweden
Posts: 5,602
ParentDir until you reach :
Photon is offline  
Old 17 May 2014, 22:58   #16
Leffmann
 
Join Date: Jul 2008
Location: Sweden
Posts: 2,269
Hmm not sure that would work. What file lock would you start from?
Leffmann is offline  
Old 19 May 2014, 23:08   #17
Mrs Beanbag
Glastonbridge Software
 
Mrs Beanbag's Avatar
 
Join Date: Jan 2012
Location: Edinburgh/Scotland
Posts: 2,243
let me clear this up, it is only necessary to be able to load files from the executable's directory, not to know the actual path, right? If one could get a lock to that directory you can Dos.library::CurrentDir to it and you're done, you can just use relative paths.

Instead of taking Process->pr_CLI->cli_CommandName and looking for '/' in the string, of course we could just get the lock to the file and then ParentDir from that.
Mrs Beanbag is offline  
Old 20 May 2014, 21:37   #18
Leffmann
 
Join Date: Jul 2008
Location: Sweden
Posts: 2,269
Yes but this is precisely the problem; there is no quick way get the file lock.
Leffmann is offline  
Old 20 May 2014, 22:20   #19
Mrs Beanbag
Glastonbridge Software
 
Mrs Beanbag's Avatar
 
Join Date: Jan 2012
Location: Edinburgh/Scotland
Posts: 2,243
although taking the title of the thread literally, "Reading files from current directory in assembly" this is easy because when launching from CLI you are already in the current directory (i.e. the current directory of the shell, not the path of the executable).
Mrs Beanbag is offline  
Old 21 May 2014, 00:23   #20
Photon
Moderator
 
Photon's Avatar
 
Join Date: Nov 2004
Location: Eksjö / Sweden
Posts: 5,602
In CLI, I'd just use a relative path to the filename and assume the exe is started from the folder it's in.

I'm very weak on Workbench programming, but for a normal "double click icon" program I don't know what other folder it could be started from.

This gives you a lock on the current folder, though I've seen a lock of 0 is valid in some doc.

Code:
CurrDirLock:
	move.l DosBase(PC),a6
	move.l #DirName,d1
	moveq #-2,d2
	jsr Lock(a6)		;d0=0 or valid lock.
	rts
	
DirName: dc.b 0
Photon is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
screenshot of my current project ara Coders. General 10 08 October 2012 17:07
My current mini-project. Desverger support.Hardware 0 21 August 2004 06:44
limit to files in a directory? yadster support.Hardware 7 09 August 2003 15:00
assembly TV L8-X Amiga scene 2 04 August 2002 02:36

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 23:25.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.10403 seconds with 13 queries