|
Yes, LEA means "Load effective address". But lead can not only load an address, it can compute it on the fly.
LEA EAX,[ESP+14]
puts the result of (value of ESP) plus 14 into EAX.
And it can compute more complicated calculations:
LEA EAX,[EAX*4+EAX]
works, and as I got from the text you have given, it does not actually use a calculation (involving things like cache access and multiply units). Instead, it uses a table lookup. But that does only work for for some multipliers.
So, in effect, for certain multipliers, LEA is faster than MUL is.
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all. Douglas Adams, "Dirk Gently's Holistic Detective Agency"
|
|
|
|
|
Thanks jhwurmbach,
You bring good point. My further question, in my previous experience with table looking or similar implementation, it only deals with constants' lookup with limited numbers (e.g. lookup one's ID by its name in a large table). So, both input and output are finite.
How could LEA use table to deal with multiplication? My confusion is, since multiplication has infinite possible input and output, how could a constant size table be used to deal with all values?
regards,
George
|
|
|
|
|
Hmm. Here, at least the input is limited to the multiplicants mentioned.
But the remaining number of results would still be very high.
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all. Douglas Adams, "Dirk Gently's Holistic Detective Agency"
|
|
|
|
|
Thanks jhwurmbach,
Could you explain or describe how multiplication could be implemented as table look-up solution please?
regards,
George
|
|
|
|
|
Some more read material: Pentium Optimization Cross-Reference[^].
From the page: LEA is better than SHL on the Pentium because it pairs in both pipes, SHL pairs only in the U pipe.
Also, as CPallini pointed out, the document states that lea can be beneficial than mul only when multiplied by 2, 3, 4, 5, 7, 8, 9.
|
|
|
|
|
Thanks Rajesh,
I read through the related section you referred. Really good.
1.
MUL is implemented through SHL? My previous thought is CPU has an individual multiplication implementation, especially for values to multiply are not power of 2 (e.g. * 3);
2.
Why when multiplying "2, 3, 4, 5, 7, 8, 9" are better to use LEA? How do we get such numbers?
regards,
George
|
|
|
|
|
You need to understand how the effective address is calculated in x86 CPUs.
I'm not an expert, but from here[^]
The offset part of the memory address can be specified either directly as a static value (called a displacement) or through an address computation made up of one or more of the following components:
Displacement—An 8-, 16-, or 32-bit value.
Base—The value in a general-purpose register.
Index—The value in a general-purpose register except EBP.
Scale factor—A value of 2, 4, or 8 that is multiplied by the index value.
An effective address is computed by:
Offset = Base + (Index * Scale) + displacement
So the address generation logic provides a rapid way to compute B + I*S + D, (and this has to be very fast as it is done before the memory access) all the article was saying was that the LEA instruction allows the programmer to access the result of this instruction that clearly executes in one cycle, which back when the article was written, was presumably faster than using the integer ALU. Note that as the Scale factor is restricted to being 1,2,4 or 8 there are limited equations that can be calculated. To multiply by 5 you would calculate base + 4*base etc..
It may not be relevant today.
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
|
|
|
|
Thanks Peter,
I like the article and read through it last evening. Three more comments,
1.
I found two places in the document are conflicting, they are
- "Modern operating system and applications use the (unsegmented) memory model¾ all the segment registers are loaded with the same segment selector so that all memory references a program makes are to a single linear-address space." -- looks like segment is useless since unsegment model is used?
- "The offset which results from adding these components is called an effective address of the selected segment. Each of these components can have either a positive or negative (2's complement) value, with the exception of the scaling factor." -- why still needs segment selector to calculate? Conflicting with last statement, which is unsegment model?
2.
"Note that the value of the EIP may not match with the current instruction because of instruction prefetching. The only way to read the EIP is to execute a CALL instruction and then read the value of the return instruction pointer from the procedure stack." -- my confusion is, EIP is next instruction to execute, and why return address is the same as EIP? Are they related?
3.
"you generally create segment selectors with assembler directives and symbols. The assembler and/or linker then creates the actual segment selectors associated with these directives and symbols." -- what does this mean? Does it mean all segment related instruction will be ignored or modified by linker before execution?
regards,
George
|
|
|
|
|
hi all,
i have an application that installs a hook(WH_GETMESSAGE) that handles mouse events, in case of WM_MOUSEMOVE my application need a notification with the msg structure associated with WM_MOUSEMOVE. I tried SendMessage its not woking ( i think its because of msg is a pointer). is any other way to do this.
Thanks & Regards
|
|
|
|
|
|
Does this[^] helpful for you?
|
|
|
|
|
code for checking an Invalid IP address by using Regular expression,Validators and QLineEdit ? It should show the IP address dialog as ( . . . ) instead of (000.000.000.000).
(i hav done this by setting mask and creating a regular expression and after that setting the validator.. and it works but it shows the IP address dialog as i hav written above)
modified on Wednesday, July 16, 2008 6:59 AM
|
|
|
|
|
Well, good luck!
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
I was trying to figure out your question, or even find a question mark in your post, but I couldn't.
|
|
|
|
|
Can you more explain about your question?
|
|
|
|
|
hey hi, i hav done this by setting mask and creating a regular expression and after that setting the validator.. but it shows the result as 000.000.000.000 but i need that it should be like this ( . . . )
|
|
|
|
|
Hi all ,
i m using this code for hadling keys of keyboard for TreeCtrl.
Here i m use Space key and its working fine.
if i m use VK_RETURN in place ok VK_SPACE fro enter key than its not working,the messagebox not shaw at this time.Please correct me.
CTreeCtrl trCtrl;
void CTest::OnTvnKeydownReportTree(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMTVKEYDOWN pTVKeyDown = reinterpret_cast<LPNMTVKEYDOWN>(pNMHDR);
NMTVKEYDOWN* nmkd = (NMTVKEYDOWN*)pNMHDR;
CString Tree_str;
switch(nmkd->wVKey)
{
case VK_SPACE:
HTREEITEM hItem = trCtrl.GetNextItem(TVI_ROOT, TVGN_CARET );
if (hItem == NULL)
{
AfxMessageBox(_T("There is no selected item);
}
else
{
Tree_str = trCtrl.GetItemText(hItem);
AfxMessageBox(Tree_str);
}
}
*pResult = 0;
}
Thanks in advance.
IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH
|
|
|
|
|
Hello everyone,
My question is during system shutdown/reboot, will OS shutdown/reboot wait for all process to terminate or not? How could OS identify if a process is terminated or not?
I am suffering a problem when write event log returns 0x80004005 error in my own code when system is reboot (I monitor this reboot in Event View), I think OS should always wait for process to complete (e.g. write event log operation) before stops event log services and reboot? Maybe OS does not wait until my process completed event log write?
thanks in advance,
George
|
|
|
|
|
George_George wrote: My question is during system shutdown/reboot, will OS shutdown/reboot wait for all process to terminate or not?
The OS will ask the applications to shut down, but if they don't after a timeout they'll be killed. Surely you've seen the dialog that informs you of this? A good starting point to find out about this process is the documentation for the WM_QUERYENDSESSION[^] message.
George_George wrote: How could OS identify if a process is terminated or not?
The OS manages processes and such so it's not hard to accept it can tell when a process is done. Anyway, you can use the WaitForSingleObject[^] function to tell is a process is done yourself (amongst other methods).
Steve
|
|
|
|
|
Thanks Steve,
I have a great reading for the links you recommended. Actually, my application does not handle WM_QUERYENDSESSION and WM_ENDSESSION. In my scenario, I do not call any system API to let system reboot, and I just press the reboot button from start menu.
My questions,
1.
If my application does not handle the two message types, OS will wait for only some time before shutdown? It means OS may stop event log services rudely (not waiting for process termination) before I write event log?
2.
The WM messages are for Windows Form application (I read the document that it mentioned it sends the message to each Window)? How about a backend service without any GUI?
3.
If I handle the WM_QUERYENDSESSION message (returns TRUE), and then received the WM_ENDSESSION message from OS, then from OS point of view, it will think when WM_ENDSESSION is sent, it will believe the process is stopped (even if the process may running)?
regards,
George
|
|
|
|
|
George_George wrote: My question is during system shutdown/reboot, will OS shutdown/reboot wait for all process to terminate or not
No exactly wait but after period it kills them for example some time you see a window a it has a button "end now".
|
|
|
|
|
Thanks Hamid!
Previously, I think event log is always ready even if during system reboot/shutdown process, so write event log during system reboot never fails (event log service should be the last service which system should stop before all other processes stop). Now, the correct conclusion should be, OS does not ensure the event log service is always avaliable during system reboot?
regards,
George
|
|
|
|
|
Hi all,
i m using ShellExecute function for open an .exe file in another application, i want to close the opened exe file plz tell me which function is for this.
Thanks in advance.
IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH
|
|
|
|
|
In general the only way to close an application is to ask it nicely. Although you could use TerminateProcess[^] doing so could be dangerous as the application could have been in the middle of something important and "killing" it could result in corruption. See here[^] for an article on how to terminate an application "cleanly" in Win32.
Steve
|
|
|
|
|
You can send WM_CLOSE to that program with SendMessage.
|
|
|
|
|