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 );
break;
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 );
else
for( y=0; y
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...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
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 japethdisease_2000,
#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
EXITM
ENDM
...and use it like this:
mov eax, Buffer(21, 34)
...very cool, huh? ;)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
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!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?
bitRAKE, you are right. I wonder why I had the opinion this won't work. Maybe older versions of masm had problems there.
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.
disease_2000,
in ASM the declaration would look like:
tYOUMAKEMELAUGH typedef proto face:dword, hair:dword
YOUMAKEMELAUGH typedef ptr tYOUMAKEMELAUGH
now YOUMAKEMELAUGH is a pointer to such a function and you are able to code:
.data
pYouMakeMeLaugh YOUMAKEMELAUGH myYouMakeMeLaugh
.code
myYouMakeMeLaugh proc face:dword,hair:dword
ret
myYouMakeMeLaugh endp
main proc
invoke pYouMakeMeLaugh,1,2
invoke myYouMakeMeLaugh,1,2
the code produced by masm is different for each invoke
japheththanx heth. (i didn't see this msg until today. too busy fixing the
infected hdd).