News:

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

2 simple path algos

Started by hutch--, February 27, 2011, 03:56:45 AM

Previous topic - Next topic

hutch--


IF 0  ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                          Build this template with "ASSEMBLE AND LINK"
ENDIF ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include\masm32rt.inc

    app_path PROTO :DWORD
    app_name PROTO :DWORD

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    LOCAL buffer[260]:BYTE
    LOCAL pbuf  :DWORD

    mov pbuf, ptr$(buffer)

    invoke app_path,pbuf
    fn MessageBox,0,pbuf,"app_path",MB_OK

    invoke app_name,pbuf
    fn MessageBox,0,pbuf,"app_path",MB_OK

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

app_path proc buffer:DWORD

  ; ---------------------------------------------------------
  ; call this procedure with the address of a 260 byte buffer
  ; return the path with a trailing "\" at address "buffer"
  ; ---------------------------------------------------------
    invoke GetModuleFileName,NULL,buffer,260

    mov ecx, buffer
    add ecx, eax            ; add length
    add ecx, 1

  @@:                       ; scan backwards to find last "\"
    sub ecx, 1
    cmp BYTE PTR [ecx], "\"
    je @F
    cmp ecx, buffer
    jge @B

  @@:
    mov BYTE PTR [ecx+1], 0 ; truncate string after last "\"

    ret

app_path endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

app_name proc buffer:DWORD

  ; ---------------------------------------------------------
  ; call this procedure with the address of a 260 byte buffer
  ; returns the file name at address "buffer"
  ; ---------------------------------------------------------
    push ebx

    invoke GetModuleFileName,NULL,buffer,260

    mov ecx, buffer
    add ecx, eax                    ; add length
    add ecx, 1

  @@:                               ; scan backwords to find last "\"
    sub ecx, 1
    cmp BYTE PTR [ecx], "\"
    je @F
    cmp ecx, buffer
    jge @B

  @@:
    add ecx, 1
    mov eax, buffer
    or ebx, -1

  @@:                               ; overwrite buffer with file name
    add ebx, 1
    movzx edx, BYTE PTR [ecx+ebx]
    mov [eax+ebx], dl
    test dl, dl
    jnz @B

    pop ebx

    ret

app_name endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

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

sinsi

Since you use ebx in app_name, maybe load it first with buffer, since buffer is used a fair bit - fewer memory accesses.
Then we can do some funky lea stuff instead of 3 ecx lines :bg

Also, with pointers, isn't it better to use unsigned jumps after a compare?
Light travels faster than sound, that's why some people seem bright until you hear them.

jj2007

Quote from: sinsi on February 27, 2011, 04:26:44 AM
Also, with pointers, isn't it better to use unsigned jumps after a compare?

A negative stack is an interesting idea :bg
    LOCAL buffer[260]:BYTE
    ...
    cmp ecx, buffer
    jge @B

hutch--

 :bg

When path lengths exceed signed DWORD range I will pay attention.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php