我只能說Linux,但也許它有幫助。您可以使用objdump
來獲得散佈的源-hex-asm輸出。
例(sum.cpp):
#include <iostream>
using namespace std;
int sum (int a, int b)
{
return a + b;
}
int main()
{
int a = 3, b = 4, c;
c = sum (a,b);
cout << c << endl;
return 0;
}
編譯& objdump的:
clang++ -g sum.cpp
objdump -d -S -C -Mintel a.out >objdump.txt
你可以在裏面objdump.txt
搜索和發現會:
00000000004007f0 <sum(int, int)>:
#include <iostream>
using namespace std;
int sum (int a, int b)
{
4007f0: 89 7c 24 fc mov DWORD PTR [rsp-0x4],edi
4007f4: 89 74 24 f8 mov DWORD PTR [rsp-0x8],esi
return a + b;
4007f8: 8b 74 24 fc mov esi,DWORD PTR [rsp-0x4]
4007fc: 03 74 24 f8 add esi,DWORD PTR [rsp-0x8]
400800: 89 f0 mov eax,esi
400802: c3 ret
400803: 66 66 66 66 2e 0f 1f data32 data32 data32 nop WORD PTR cs:[rax+rax*1+0x0]
40080a: 84 00 00 00 00 00
0000000000400810 <main>:
}
int main()
{
400810: 55 push rbp
400811: 48 89 e5 mov rbp,rsp
400814: 48 83 ec 20 sub rsp,0x20
400818: c7 45 fc 00 00 00 00 mov DWORD PTR [rbp-0x4],0x0
int a = 3, b = 4, c;
40081f: c7 45 f8 03 00 00 00 mov DWORD PTR [rbp-0x8],0x3
400826: c7 45 f4 04 00 00 00 mov DWORD PTR [rbp-0xc],0x4
c = sum (a,b);
40082d: 8b 7d f8 mov edi,DWORD PTR [rbp-0x8]
400830: 8b 75 f4 mov esi,DWORD PTR [rbp-0xc]
400833: e8 b8 ff ff ff call 4007f0 <sum(int, int)>
400838: 48 8d 3c 25 40 0d 60 lea rdi,ds:0x600d40
40083f: 00
400840: 89 45 f0 mov DWORD PTR [rbp-0x10],eax
cout << c << endl;
400843: 8b 75 f0 mov esi,DWORD PTR [rbp-0x10]
400846: e8 05 fe ff ff call 400650 <std::ostream::operator<<(int)@plt>
40084b: 48 8d 34 25 b0 06 40 lea rsi,ds:0x4006b0
400852: 00
400853: 48 89 c7 mov rdi,rax
400856: e8 45 fe ff ff call 4006a0 <std::ostream::operator<<(std::ostream& (*)(std::ostream&))@plt>
40085b: b9 00 00 00 00 mov ecx,0x0
return 0;
400860: 48 89 45 e8 mov QWORD PTR [rbp-0x18],rax
400864: 89 c8 mov eax,ecx
400866: 48 83 c4 20 add rsp,0x20
40086a: 5d pop rbp
40086b: c3 ret
40086c: 0f 1f 40 00 nop DWORD PTR [rax+0x0]
最後一步將是開發一個將十六進制轉換爲二進制的程序。
這很不錯,謝謝。我確信我可以編寫一個Python腳本來將十六進制轉換爲二進制,並保留該行的其餘部分。如果你不介意收集更多答案,我會保持這個問題的開放性。 – delrocco 2014-11-04 17:38:32
你知道OSX相當於objdump嗎?它在默認情況下在OSX Mountain Lion上不可用。也許我需要爲OSX找到它的一個版本? – delrocco 2014-11-04 17:40:01
@occorled:我沒有MAC,所以我無法測試它的功能。通過谷歌搜索「osx objdump」我發現:http://www.electricfluid.com/?p=56。 – rkhb 2014-11-04 17:50:47