There's no point in using printf in a compilation, it's just a matter of writing, and the use of printf in __asm is a bit complicated. Let's look at the following code:
void Main ()
{
int t = 10;
char *szformat = "T =%d/n";
printf (Szformat, T);
}
===
Output
t = 10
Press any key to continue
Debug the resulting assembly code:
19:void Main ()
20: {
0040b770 Push EBP
0040b771 mov Ebp,esp
0040b773 Sub esp,48h
0040b776 push EBX
0040b777 push ESI
0040b778 Push EDI
0040b779 Lea edi,[ebp-48h]
0040B77C mov ecx,12h
0040b781 mov eax,0cccccccch
0040b786 Rep stos dword ptr [edi]
21:int T = 10;
0040b788 mov dword ptr [ebp-4],0ah
22:char *szformat = "T =%d/n";
0040b78f mov dword ptr [Ebp-8],offset string "%d/n" (0041FF6C)
23:printf (Szformat, T);
0040b796 mov eax,dword ptr [ebp-4]
0040b799 push EAX
0040B79A mov ecx,dword ptr [ebp-8]
0040B79D push ECX
0040b79e call printf (0040b6f0)
0040B7A3 Add esp,8
24:}
If we write a __asm code in a sensory way, it will be written like this (I wrote it at the beginning):
#include <stdio.h>
void ASM ()
{
int t = 10;
char *szformat = "T =%d/n";
__asm
{
Push T
Lea EAX, Szformat
Push EAX
Call printf
Add ESP, 8
}
}
void Main ()
{
ASM ();
}
===
Output
Lapress any key to continue
Oh, no, it's totally different from what we want.
What to do, first look at his assembly code:
3:void ASM ()
4: {
0040b770 Push EBP
0040b771 mov Ebp,esp
0040b773 Sub esp,48h
0040b776 push EBX
0040b777 push ESI
0040b778 Push EDI
0040b779 Lea edi,[ebp-48h]
0040B77C mov ecx,12h
0040b781 mov eax,0cccccccch
0040b786 Rep stos dword ptr [edi]
5:int T = 10;
0040b788 mov dword ptr [ebp-4],0ah
6:char *szformat = "T =%d/n";
0040b78f mov dword ptr [Ebp-8],offset string "%d/n" (0041FF6C)
7: __asm
8: {
9:push T
0040b796 push DWORD ptr [EBP-4]
10:lea eax, Szformat
0040b799 Lea Eax,[ebp-8]
11:push eax
0040b79f push EAX
12:call printf
0040B7A0 call printf (0040b6f0)
13:add ESP, 8
0040b7a5 Add esp,8
14:}
15:}
The code difference is obvious, and soon I get the code:
#include <stdio.h>
void ASM ()
{
int t = 10;
char *szformat = "T =%d/n";
__asm
{
mov eax, t
Push EAX
mov ecx, DWORD ptr [Ebp-8]
Push ECX
Call printf
Add ESP, 8
}
}
void Main ()
{
ASM ();
}
===
Output
t = 10
Press any key to continue
Oh, do not say, what is not clear, Baidu find it (although Google better).
Does anyone want to explain why?