The follwing code is from a simple custom edit control. How does the 'buffer' statement work?

#define Buffer( x, y ) *( pBuffer + y * cxBuff + x )

      case WM_CREATE:
         hdc = GetDC( hwnd );
         SelectObject( hdc, GetStockObject( SYSTEM_FIXED_FONT ) );
         GetTextMetrics( hdc, &tm );
         cxChr = tm.tmAveCharWidth;
         cyChr = tm.tmHeight + tm.tmExternalLeading;
         ReleaseDC( hwnd, hdc );                    

      case WM_SIZE:
         cyWin = HIWORD( lParam );
         cxWin = LOWORD( lParam );
         cxBuff = max( 1, cxWin / cxChr );
         cyBuff = max( 1, cyWin / cyChr );
         if( pBuffer != NULL ) free( pBuffer );
         if( (LONG) cxBuff * cyBuff > MAX_BUFF ||
             ( pBuffer = malloc( cxBuff * cyBuff ) )
             == NULL )
            MessageBox( hwnd, "Insufficient memory --"
                        "Reduce window size ...",
                        "Editor Report",
                        MB_ICONEXCLAMATION | MB_OK );
            for( y=0; y
Posted on 2001-07-01 07:53:00 by MovingFulcrum
just a correction: what Fulcrum wants to know is how it actually works. he doesn't want you to convert it to assembly. however, i would like to see
 #define(x,y) *(pBuffer+y * cxBuff+x) 
in masm...
Posted on 2001-07-01 07:58:00 by disease_2000
the #define Buffer( x, y ) *( pBuffer + y * cxBuff + x ) is a instruction to the preprocessor, which will change line: Buffer( x, y ) = ' '; to: *(pBuffer + y * cxBuff + x) = ' '; before the compiler is involved. It has nothing to do with C/C++. If you can get a preprocessor as a single exe (like CPP.EXE in Borland C 4.0), you can use it for ASM files as well (which will "understand" '#define's or '#includes' then). This message was edited by japheth, on 7/1/2001 12:04:17 PM
Posted on 2001-07-01 12:00:00 by japheth
I still cant get anything? What exactly does it do in wm_size and wm_char?
It has nothing to do with C/C++. If you can get a preprocessor as a single exe (like CPP.EXE in Borland C 4.0), you can use it for ASM files as well (which will "understand" '#define's or '#includes' then).
what has nothing to do with c/c++??? i really cant get u japeth
Posted on 2001-07-01 12:21:00 by MovingFulcrum

#define Buffer( x, y ) *( pBuffer + y * cxBuff + x )
...I believe would translate into something like:

cxBuff EQU 12345
pBuffer EQU Defined_Memory_Address

Buffer MACRO x:REQ, y:REQ
...and use it like this:

    mov eax, Buffer(21, 34)
...very cool, huh? ;)
Posted on 2001-07-01 12:42:00 by bitRAKE
Hi MovingFullcrum, my interpretation of what this code does in WM_SIZE is: - it gets new size in cxWin/cyWin - it transformes this pixel values in "char" values (say 800x600 to 80x25) - it allocates memory for a buffer for this chars (80x25=2000 bytes), pointer is pBuffer - it "clears" that Buffer with blanks (not very efficiently, i suppose. This is where this Buffer(x,y) "statement" gets involved - it sets the "cursor" to row 0 , col 0 i thing this code is part of a "telnet" app (or host emulation or whatever) with a "character mode" user interface. "it has nothing to do with C/C++)". What I mean is: the code Buffer(x, y) looks like a C function, but it is not, it is just a C preprocessor macro and in this sense "has nothing to do with the C/C++" language. -------------------------------------------- to bitRake: im afraid your macro will not work. As far as i know you cannot define macros which substitute just operands. And I think if masm could do that, it wont be able to translate the resulting code in opcodes, for the "adressing" mode is too complex (memory operands and constants mixed to get an EA) This message was edited by japheth, on 7/1/2001 2:16:15 PM
Posted on 2001-07-01 13:55:00 by japheth
rake, help me to convert this c style to asm... since we're on the conversion topic. :)

typedef void (APIENTRY * YOUMAKEMELAUGH)(int face, int hair);
(by the way, this is not a joke...) if you don't know how to translate the above code to asm, perhaps you can tell me how it actually works? thanx!
Posted on 2001-07-01 14:17:00 by disease_2000
japheth, the code does work! You can break it tho. :) Yes, macros can get very tricky in MASM. disease_2000, you got me stumped. What is the context?
Posted on 2001-07-01 18:45:00 by bitRAKE
bitRAKE, you are right. I wonder why I had the opinion this won't work. Maybe older versions of masm had problems there.
Posted on 2001-07-02 04:20:00 by japheth
Disease2000, It's a typedef declaration for a function prototype. The function takes two arguments and returns nothing. The calling convention is APIENTRY (far pascal). I don't think it's useful in ASM, it's mostly used for type checking.
Posted on 2001-07-02 06:41:00 by karim
disease_2000, in ASM the declaration would look like:

tYOUMAKEMELAUGH typedef proto face:dword, hair:dword

now YOUMAKEMELAUGH is a pointer to such a function and you are able to code:

pYouMakeMeLaugh YOUMAKEMELAUGH myYouMakeMeLaugh

myYouMakeMeLaugh proc face:dword,hair:dword
myYouMakeMeLaugh endp

main      proc

          invoke pYouMakeMeLaugh,1,2
          invoke myYouMakeMeLaugh,1,2

the code produced by masm is different for each invoke japheth
Posted on 2001-07-02 07:02:00 by japheth
thanx heth. (i didn't see this msg until today. too busy fixing the infected hdd).
Posted on 2001-07-02 19:48:00 by disease_2000