News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

FileName from Path

Started by jckl, March 15, 2006, 03:03:16 AM

Previous topic - Next topic

PBrennick

crt__splitpath, that is amazing!  :U

Paul
The GeneSys Project is available from:
The Repository or My crappy website

P1

From MASM32.lib, there is NameFromPath.

The source is available for learning purposes.

Regards,  P1  :8)

PBrennick

P1
I just said that in the posting before MichaelW's and I included the code.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

GregL

crt__splitpath, never noticed that before. Handy.   :thumbu


P1

Quote from: PBrennick on March 15, 2006, 03:44:57 PMI just said that in the posting before MichaelW's and I included the code.
Paul,  I flew by all the posted code, seeing I would have just used the library function.

Plus, I keep a check on the Forum at work, while I wait for computers to finish working on different things.

Sorry,  P1  :8)

Mark Jones

Quote from: lingo on March 15, 2006, 02:55:09 PM
Why not a bit faster.. :lol
[snip...]


Now that's what I call code!  So, who has the decryption key? :toothy
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

Vortex

Hi zooba,

Thanks for the info. Here is a simple demo :

.586
.model flat, stdcall

option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\msvcrt.inc
include     \masm32\include\shlwapi.inc

includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\msvcrt.lib
includelib  \masm32\lib\shlwapi.lib


.data
pszPath db 'C:\masm32\bin\ml.exe',0
format1 db 'The path without file spec is : %s',0

.data?
buffer  db 50 dup(?)

.code

start:

    invoke  PathRemoveFileSpec,ADDR pszPath
    invoke  crt_printf,ADDR format1,ADDR pszPath   
    invoke  ExitProcess,0

END start

lingo

The usage of the high level garbage is a big shame for every  ASSEMBLY programmer

Why garbage..

here is  PathRemoveFileSpec proc  from shlwapi.dll

BOOL __stdcall PathRemoveFileSpecA(LPSTR pszPath)
                 public PathRemoveFileSpecA
PathRemoveFileSpecA proc near       

pszPath         = dword ptr  8

                 mov     edi, edi
                 push    ebp
                 mov     ebp, esp
                 push    esi
                 push    edi
                 mov     edi, [ebp+pszPath]
                 test    edi, edi
                 jz      loc_77FA7285
                 cmp     byte ptr [edi], 0
                 mov     eax, edi
                 mov     esi, edi
                 jz      loc_77FA7254

loc_77FA722B:
                 mov     cl, [eax]
                 cmp     cl, 5Ch
                 jnz        loc_77FA7236
                 mov      esi, eax
                 jmp       loc_77FA7248

loc_77FA7236:
                 cmp     cl, 3Ah
                 jnz     loc_77FA7248
                 lea     ecx, [eax+1]
                 cmp     byte ptr [ecx], 5Ch
                 jnz     loc_77FA7245
                 mov     eax, ecx

loc_77FA7245:
                 lea     esi, [eax+1]

loc_77FA7248:
                 push    eax             ; lpsz
                 call      CharNextA
                 cmp     byte ptr [eax], 0
                 jnz        loc_77FA722B

loc_77FA7254:
                 mov     al, [esi]
                 test      al, al
                 jz      loc_77FA7285
                 cmp     esi, edi
                 jnz    loc_77FA7262
                 cmp     al, 5Ch
                 jz      loc_77FA7271

loc_77FA7262:
                 lea     ecx, [edi+1]
                 cmp     esi, ecx
                 jnz     loc_77FA727D
                 cmp     al, 5Ch
                 jnz    loc_77FA727D
                 cmp     [edi], al
                 jnz     loc_77FA727D

loc_77FA7271:
                 xor     eax, eax
                 cmp     [esi+1], al
                 jz      loc_77FA7287
                 mov     [esi+1], al
                 jmp    loc_77FA7282

loc_77FA727D:
                 mov     byte ptr [esi], 0
                 xor     eax, eax

loc_77FA7282:         
                 inc     eax
                 jmp     loc_77FA7287

loc_77FA7285:       
                 xor     eax, eax
loc_77FA7287:
                 pop     edi
                 pop     esi
                 pop     ebp
                 retn    4
PathRemoveFileSpecA endp





PBrennick

lingo,

QuoteWhy garbage..

here is  PathRemoveFileSpec proc  from shlwapi.dll
Vortex helps everybody and has contributed a lot of tools to this forum and to the community.  I believe that you owe him an apology.  Your hurtful words were unnecessary and this thread was a harmonious bringing together of different people with different ideas.  You are a discordant. I hope you do not intend to continue with old habits.

If you wish to discuss something, what about your code?  You did NOT write that.  It is from a dissassembly. You did not even bother to take the effort to hide the fact.  At least Erol's code was written by Erol

Please be civil,
Paul
The GeneSys Project is available from:
The Repository or My crappy website

lingo

"You can program so there is no reason why you cannot do those tests yourself."
"Good, you have a lot of people to ask then, you had better get busy. "

from www.masmforum.com/simple/index.php?topic=4071.msg30578#msg30578



again people who cannot control themselves try to poison the thread

hutch--

Hey guys,

try and keep this friendly. If you look at how uncritical the task is of chopping the file name off the end of a path, the real contest is how to do it even bigger and slower so it looks like it has been written in C++.  :bg

If I suffer severe brain rot later in the day, I may try a reverse optimisation to try and emulate C++ style performance.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

PBrennick

Hi Hutch,
So this means you do not have a problem with him calling Vortex's code Garbage?

Paul
The GeneSys Project is available from:
The Repository or My crappy website

raymond

This would be my shorter code without relying on any external function. It returns the starting address of the file name in EAX. Unless you have a very specific reason to have a duplicate copy of such a file name, its address should be sufficient in the majority of cases.

NameFromPath proc USES edi src:DWORD
   mov   edi,src
   xor   eax,eax
   mov   ecx,-1
   repnz scasb     ;find terminating 0
   dec   edi       ;return pointer to the 0 byte
   neg   ecx
   dec   ecx       ;ECX = length of path+filename (including terminating 0)
                   ;you could always save this for later use if needed
   mov   al,"\"
   std
   repnz scasb
   cld             ;EDI points to byte preceeding either "\" or start of filename
   jnz   @F        ;no "\" found
   inc   edi       ;EDI now points to "\"
;  inc   ecx       ;adjust if you need to know the length of the path
@@:
   inc  edi       ;EDI now points to start of filename
   mov   eax,edi
   ret
NameFromPath endp


If the total length of the path+filename is saved, the count remaining in the ECX register after backscaning for the "\" could be used to determine the length of the "clean" filename. If you really need to make a duplicate copy, you could use such count with "movsb", or simply copy it byte by byte until you see the terminating 0.

That's the beauty of assembly, so many ways of doing the same thing. :clap: :clap:

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

Timbo

Quotetechnically u got a bug there, if the string doesnt have a backslash in it (and i have seen some return with / instead of \) your code will most likely screw up
best check that edx >= start of string :)

Thanks for pointing this out.  This had ocurred to me after I posted it, and infact I had it in the back of my mind that I would need ecx somewhere within the body of the procedure but alas it slipped my mind. The way it stands I would cause an access violation by walking backwards to god-knows-where when passed a string without a backslash in it.

Other newcomers:  Pay attention to this.  This is how "works most of the time" assembler code gets created. :lol

Tim

jckl

I use the full path aswell as teh file name alone so i would need both which is why i saved the file name to a new string. Thx for all the different ideas though. This helps me go back and look at the different methods and i can learn more from this. I think i am doing pretty well for starting MASM less than 1 month ago  :U