News:

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

Rookie mistake: spot the error

Started by NoCforMe, October 11, 2011, 10:42:00 AM

Previous topic - Next topic

NoCforMe

See if you can find the problem here:



; First, zero out entire structure:
PUSH EDI
LEA EDI, ofn
MOV ECX, SIZEOF OPENFILENAME
MOV ofn.lStructSize, ECX ;Stuff length while we have it.
XOR EAX, EAX
REP STOSB ;Zero it out.
POP EDI



This one survived about a half-hour of debugging, head scratching and swearing ...
("ofn" is the structure used by GetOpenFileName() )

I post this in hopes that others won't make this same mistake.

jj2007

Better?
LOCAL ofn:OPENFILENAME
push edi
lea edi, ofn
push OPENFILENAME
pop eax
stosd
xchg eax, ecx
sub ecx, 4
xor eax, eax
rep stosb ;zero it out.
pop edi

dedndave

 :red
lea edi, ofn

:P
mov edi,offset ofn

szSaveAs        db 'Save As...',0
szFileTypes     db 'Text Files',0,'*.txt',0,'All Files',0,'*.*',0,0

        .CODE

;EAX = filename buffer address
;ECX = 0
;EDX = filename buffer length

        push    ecx                                                      ;lpTemplateName
        push    ecx                                                      ;lpfnHook
        push    ecx                                                      ;lCustData
        push    offset szFileTypes+13                                    ;lpstrDefExt - 'txt',0
        push    ecx                                                      ;nFileOffset/nFileExtension
        push    OFN_LONGNAMES or OFN_HIDEREADONLY or OFN_OVERWRITEPROMPT ;Flags or OFN_ENABLEHOOK or OFN_EXPLORER
        push    offset szSaveAs                                          ;lpstrTitle
        push    ecx                                                      ;lpstrInitialDir
        push    ecx                                                      ;nMaxFileTitle
        push    ecx                                                      ;lpstrFileTitle
        push    edx                                                      ;nMaxFile
        push    eax                                                      ;lpstrFile
        push    1                                                        ;nFilterIndex
        push    ecx                                                      ;nMaxCustFilter
        push    ecx                                                      ;lpstrCustomFilter
        push    offset szFileTypes                                       ;lpstrFilter
        push    ecx                                                      ;hInstance
        push    hWnd                                                     ;hwndOwner
        push    76                                                       ;lStructSize
        INVOKE  GetSaveFileName,esp
        add     esp,76                                                   ;sizeof OPENFILENAME

sinsi

; First, zero out entire structure
; Then, fill in ofn.lStructSize

edit: jj forgot something?
Light travels faster than sound, that's why some people seem bright until you hear them.

dedndave

it may not be a local - if it is, then use LEA EDI,ofn

push edi
mov edi,offset ofn
push sizeof OPENFILENAME
push (sizeof OPENFILENAME-4)/4
pop ecx
pop eax
stosd
xor eax,eax
rep stosd ;zero out the rest of it
pop edi


of course, we shouldn't do this in the campus, but it's fun   :bg
and, NoCforMe isn't a real n00b, anyways

jj2007

Quote from: sinsi on October 11, 2011, 11:34:05 AM
; First, zero out entire structure
; Then, fill in ofn.lStructSize

edit: jj forgot something?

No :bg

drizz


PUSH EDI
LEA EDI, ofn
MOV ECX, SIZEOF OPENFILENAME
MOV ofn.lStructSize, ECX ;Stuff length while we have it.
XOR EAX, EAX
REP STOSB ;Zero it out.
POP EDI

MOV ofn.lStructSize, SIZEOF OPENFILENAME; :-)

The truth cannot be learned ... it can only be recognized.

jj2007

@drizz: as it stands, 36 bytes; with small changes...
LOCAL ofn:OPENFILENAME
push edi
lea edi, ofn
mov ecx, sizeof OPENFILENAME
;MOV ofn.lStructSize, ECX ;Stuff length while we have it.
xor eax, eax
rep stosb ;zero it out.
pop edi
m2m ofn.lStructSize, sizeof OPENFILENAME;
ret

... down to 31, still 2 more than mine ;)

oex

mov esi, alloc(SIZE OPENFILENAME)
mov [esi].OPENFILENAME.lStructSize, SIZE OPENFILENAME;

Smaller than 31 bytes?

:lol I know technically not but size is relative
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

jj2007

Quote from: oex on October 11, 2011, 12:47:21 PM
Smaller than 31 bytes?

Sorry, it's 32 bytes ;-)

MyTesto proc arg1, arg2
push esi
mov esi, alloc(SIZE OPENFILENAME)
mov [esi].OPENFILENAME.lStructSize, SIZE OPENFILENAME
; ...
free esi
pop esi
ret
MyTesto endp

oex

Quote from: jj2007 on October 11, 2011, 12:55:31 PM
Quote from: oex on October 11, 2011, 12:47:21 PM
Smaller than 31 bytes?

Sorry, it's 32 bytes ;-)

:lol.... Well I beat the almighty Drizz.... I am not ashamed.... And my code is pretty neat and tidy :lol
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

dedndave

Quote from: dedndave on October 11, 2011, 11:37:28 AM
it may not be a local - if it is, then use LEA EDI,ofn

push edi
mov edi,offset ofn
push sizeof OPENFILENAME
push (sizeof OPENFILENAME-4)/4
pop ecx
pop eax
stosd
xor eax,eax
rep stosd ;zero out the rest of it
pop edi

18 bytes if ofn is global
16 bytes if ofn is local

jj2007

Dave,
It's 28 bytes with frame etc, but still: 1 byte shorter than my version - you win :cheekygreen:

dedndave

 :lol

i dunno how i miscounted the bytes, though

jj2007

Quote from: dedndave on October 11, 2011, 01:42:03 PM
:lol

i dunno how i miscounted the bytes, though

it's the overhead.
MyTestdave_s:  ; 28
MyTestdave proc arg1, arg2
LOCAL ofn:OPENFILENAME
dave_s::  ; 16
push edi
lea edi, ofn
push sizeof OPENFILENAME
push (sizeof OPENFILENAME-4)/4
pop ecx
pop eax
stosd
xor eax,eax
rep stosd ;zero out the rest of it
pop edi
dave_endp::
ret
MyTestdave endp
MyTestdave_endp: