Strange, wierd things comes with titorials.
If I had no needs to discuss it in English, I would be able to write whole novel
about it.
In this post I discuss just a little part of it, and continue if nobody shuts me up.
We start with 3dFrames.asm
First example in Examples1 in MASM32.
I don't post whole original source, you all have it in MASM32.
Let's go to .data section first.
.data
szDisplayName db "3D Frames",0
CommandLine dd 0
hWnd dd 0
hInstance dd 0
1.We don't use CommandLine anywhere in the prog
2.We ALREADY know hInstance for our prog , if we don't change base option it would be 400000h
if we rebase it - we will now new base anyway.
So let's save 8 byte of our prog and make it faster each time we need to use hInstance.
We make it immideate. It's pushed for 1 clock, wich 1 clock faster then to push memory.
.data
szDisplayName db "3D Frames",0
; CommandLine dd 0
hWnd dd 0
; hInstance dd 0
hInstance EQU 400000h
Let's go to start of .code section
invoke GetModuleHandle, NULL
mov hInstance, eax
invoke GetCommandLine
mov CommandLine, eax
.0040107C: 6A00 push 000
.0040107E: E801060000 call .000401684 -------- (1)
.00401083: A312204000 mov [000402012],eax
.00401088: E8F1050000 call .00040167E -------- (2)
.0040108D: A30A204000 mov [00040200A],eax
We don't need any of this code. We don't use command line and we already know hInstance of our
program.
And as you see it takes 22 byte and hundreds of codes.
So we can comment it or delete.
Now we come to the standard WinMain wich I realy hate...
invoke WinMain,hInstance,NULL,CommandLine,SW_SHOWDEFAULT
Look
hInstance even in old version dublicate global variable hInstance wich is accesible for any
proc in the prog.
After we change it to constant we just steal the sence from the thing which already had no sence.
So we don't need this parameter at all.
The second parameter NULL (hPrevInstance) drives me crazy each time I see it?
Who can give a logical explonation what for in asm prog we need hPrevInstance?
As memorial tomb on C grave for Win 3.1?
Through it away!
CommandLine - we don't use CommandLine in the prog.
The last is funny one :)
We suppouse to use it for ShowWindow in the WinMain :)
But:
1. This WinMain doesn't use it at all.
2. It's better to use imm. for this 'cause it faster.
So none of the four parameters is needed.
If you want still use WinMain change the proto to
WinMain PROTO ;and that's all
and cal it:
call WinMain
it will save you 12 bytes and 8 clocks, spent for NOTHING.
Start of our prog will be:
hInstance equ 400000h
.data
hWnd dd 0
szDisplayName db "3D Frames",0
.code
call WinMain
Then we can see further C++ simulation:
WinMain proc hInst :DWORD,
hPrevInst :DWORD,
CmdLine :DWORD,
CmdShow :DWORD
;====================
; Put LOCALs on stack
;====================
LOCAL wc :WNDCLASSEX
LOCAL msg :MSG
LOCAL Wwd :DWORD
LOCAL Wht :DWORD
LOCAL Wtx :DWORD
LOCAL Wty :DWORD
;==================================================
; Fill WNDCLASSEX structure with required variables
;==================================================
mov wc.cbSize, sizeof WNDCLASSEX
mov wc.style, CS_HREDRAW or CS_VREDRAW \
or CS_BYTEALIGNWINDOW
mov wc.lpfnWndProc, offset WndProc
mov wc.cbClsExtra, NULL
mov wc.cbWndExtra, NULL
m2m wc.hInstance, hInst ;<< NOTE: macro not mnemonic
mov wc.hbrBackground, COLOR_BTNFACE+
After reading a few of these I'll be so much incredibly more conscious of optimization then I ever thought I would be :)
I think that people do just "cut and paste" existing code that already works. Not everyone stops to think "do I really need this here" or "can it work if I leave this out or put this here". In reality, I haven't experimented much with that stuff, but while writing my C-2-Asm guides, I've been doing some serious experimentation to yiled different results and Svin's #'s are really helping to stimulate my intellectual juices.
Keep it up Svin. This work is invaluable.
_Shawn