News:

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

New version of NameFromPath procedure.

Started by hutch--, June 28, 2005, 02:28:19 AM

Previous topic - Next topic

hutch--

The previous version worked if the source and target buffers were the same size but read past the end if the target buffer was smaller. rather than mess around debugging it, I have done another version that has identical functionality but has added error checking so that it will return an error if a string that does not contain a "\" is pointer at it. Not that its a problem but this version should be sligntly faster as well.


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    .486
    .model flat, stdcall  ; 32 bit memory model
    option casemap :none  ; case sensitive

    .code

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

align 4

NameFromPath proc src:DWORD,dst:DWORD

    push esi
    push edi

    mov esi, src
    mov ecx, esi
    mov edx, -1
    xor eax, eax

  @@:
    add edx, 1
    cmp BYTE PTR [esi+edx], 0       ; test for terminator
    je @F
    cmp BYTE PTR [esi+edx], "\"     ; test for "\"
    jne @B
    mov ecx, edx
    jmp @B
  @@:
    cmp ecx, esi                    ; test if ECX has been modified
    je error                        ; exit on error if it is the same
    lea ecx, [ecx+esi+1]            ; add ESI to ECX and increment to following character
    mov edi, dst                    ; load destination address
    mov edx, -1
  @@:
    add edx, 1
    mov al, [ecx+edx]               ; copy file name to destination
    mov [edi+edx], al
    test al, al                     ; test for written terminator
    jnz @B

    sub eax, eax                    ; return value zero on success
    jmp nfpout

  error:
    mov eax, -1                     ; invalid path no "\"

  nfpout:

    pop edi
    pop esi

    ret

NameFromPath endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Jibz

hutch--,

Nice work .. I bet it's faster than the original, though as you mention that's hardly going to make any difference in most conceivable uses :U.

I would probably have made it copy the entire string if no '\' was found, based on the interpretation that a filename alone is a path and filename with an empty path.

That would arise one potential problem though, which is a drive designation alone, like 'C:', which I guess would have to be handled also.

On a hypothetical note, you are comparing a pointer to an index with cmp ecx, esi, if a '\' is found. But it is of course extremely unlikely that the pointer will be low enough, or the index high enough, for the two to meet.

Vortex

The new version works fine.
Quote
.386
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc
include \masm32\macros\macros.asm

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib

NameFromPath2 PROTO src:DWORD,dst:DWORD

.data?
buffer db 20 dup(?)
.code
start:

   fn   NameFromPath2,"C:\masm32\include\windows.inc",ADDR buffer
   invoke   StdOut,ADDR buffer
   invoke   ExitProcess,0

END start

hutch--

Yes, the logic was that even with paths of 32k, the EDX counter canot get up into addressing range values so its probably safe until we have megabyte length paths.

Vortex,

Thanks for verifying the algo.  :thumbu
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php