i need help, to complete my first function in masm32 "GetFileName" :)...

Started by Dasar, July 07, 2006, 04:18:29 PM

Previous topic - Next topic

Ratch

Dasar,
     OK, here it is all ZIPped up.  I know it is not quite what you asked for, but it does show show how to use the Common Dialog Boxes to interrogate the user for a file name.  There are two executables; one from a MASM build and the other from a C build by Charles Petzold.  You can tell from the size which one is MASM or C.  It's a big program for a beginner, so ask if you have any questions.  Ratch

[attachment deleted by admin]

Hjortur

Hi, I am pretty new to MASM32 assembly, but I have used GAS (Gnu assembler) for a while.  But anyway I want to ask, whould it not be possible to "declear" (not sure how it is spelled) in the .data section something like this


  .data
  mypath db "c:/myfolder/somefile.asd",0
  dummybyte db "A"


and then work your way backwards from the address of dummybyte and look for the first "/" (last?) to show up?
or does MASM do some alignement for you so e.g if  dummybyte's address is 43678 would byte at address 43677 be the 0 at the end of mypath string?

Sorry for my not so good English, I am from Iceland so I am not a native English speaker. But I try to speak correctly

thx in advance

hutch--

Hi Hjortur,

Welcome on board. Let me see if I understand you correctly, you want to be able to overwrite the path "mypath" with some other data ?

If its just getting the file name without the attached path, that easy to do. Start with the address of "mypath", scan forward and store the offset of each "/" character in the same register. When you get to the terminating zero then either copy the data from the offset of the last "/" to another buffer or just return the value of the offset added to the address. Either method will work fine.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Shantanu Gadgil

To hutch,
What he wants to ask (I think) is "Wouldn't be possible to just go _back_ from "dummybyte", rather than going "forward and back" as shown in the code?"

To Hjortur,
Yes, it is possible...but ONLY in this case. :)

The problems with that algorithm:
What if the data is changing in the variable "mypath"?
What if some shorter name, than the original, were to overwrite in place of the declared "mypath", the efffective value of "mypath" would be "C:/ZZZ/\0lder/somefile.asd",0 (The \0 is ASCII ZERO ofcourse :) :) )

See...like this many other problems are there.

So it would be appropriate to declare "mypath" as some big buffer (usually MAX_PATH) and manipulate from front.

There is very less "sanity checking" in the code I gave, as to, what if there is NO '\' in the text :eek :eek

HTH,
Shantanu
To ret is human, to jmp divine!

dsouza123


.data
  mypath db "c:\fairlylongdirectoryname\secondleveldir\filename.ext",0
  dummybyte db "A"

.code

  mypath gets overwritten to
  "c:\small\newname.ext",0,yname\secondleveldir\filename.ext",0


now searching backwards from the "A" returns the wrong filename.

Searching from the end would be quicker but if the string gets modified, it may give the wrong result.

Casper

This is exactly the reason why it is always good programming practice to zero a buffer before it is written to.  By the way, if the buffer is cleared first, then a reverse search would first search for the first occurrence of a nonzero value then parse to the first occurrence of '\' while watching for the start of the buffer which means there is no '\'

A good exercise in programming basics, all around.
Paul


Ratch

Dasar,

     Ok, I think I have something you are looking for and can use.  It is a subroutine that enables you to find the filename in a directory string without knowing the length of the string, performing backward searches,  complex parsing, zeroing buffers, or otherwise getting tied up in a knot.  The subroutine is something I wrote a while back called MFNDCHAR (Multiple Find Characters).  Using an advanced algo, it will search a string for as many characters as you want to push onto the stack.  It will then spit out the address in EAX upon finding the first instance of any of the search characters.  It searches a string in DWORD gulps, instead of teensy BYTE comparisons.  The parameters are "sticky", meaning that the subroutine does not remove them from the stack.  This means they can be used for another search without PUSHing the same parameters all over again.  It also means that the user is responsible for balancing the stack after the last subroutine call.  It is also important that a zero be PUSHed onto the stack before any other parameter.  The example given shows the algo for filename searches.  Simply find each backward slash and put the next character address into a variable called SaveAdr.  When a zero at the end of the string is encountered, the last value of SaveAdr is the filename.  It is all in the zipped file.  Ratch

[attachment deleted by admin]

Hjortur

thx shantanu_gadgil you guessed what I was trying to say.  And thank you for clearing this for me.