用VC写Assembly代码(4)
在匯編中使用printf沒什么意義,這里只說明一寫問題,printf 再 __asm中的使用有點(diǎn)復(fù)雜.先看看下面代碼:
void main()
{
?int t = 10;
?char *szformat = "t = %d/n";
?printf(szformat, t);
}
===
輸出
t = 10
Press any key to continue
調(diào)試得到的匯編代碼:
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:?? }
如果我們用感覺上的方法寫個(gè)__asm 代碼,會(huì)寫成這樣(我一開始是寫成這樣的):
#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();
}
===
輸出
lAPress any key to continue
哦,不!怎么和我們要的完全不一樣呢?
怎么辦,先看看他的匯編代碼:
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:?? }
代碼區(qū)別很明顯,很快我得出一下代碼:
#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();
}
===
輸出
t = 10
Press any key to continue
哦!不說了,有什么不清楚,上BAIDU找找吧(雖然GOOGLE更好).
有人愿意說明一下原因嗎?謝謝.
總結(jié)
以上是生活随笔為你收集整理的用VC写Assembly代码(4)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 窥探黑盒-卷积神经网络的可视化
- 下一篇: 林华达视角-概率图模型与计算机视觉