-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.cpp
72 lines (62 loc) · 1.4 KB
/
example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <chrono>
#include <iostream>
#include "fasmcpp.h"
using namespace std::chrono;
using namespace fasmcpp::size_multipliers;
int main()
{
fasmcpp::Assembler assembler;
try
{
assembler = 100_KB;
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
std::cin.get();
return 1;
}
fasmcpp::Assembly helloWorld;
#if defined _WIN64
helloWorld = "\
use64 \n\
sub rsp, 5*8 \n\
lea rcx, [string] \n\
call [_printf] \n\
add rsp, 5*8 \n\
ret \n\
_printf dq printf \n\
string db 'Hello world (from assembly)!', 13, 10, 0 \n\
";
#elif defined _WIN32
helloWorld = "\
use32 \n\
call $+5 \n\
mov eax, [esp] \n\
add dword[esp], string-5 \n\
call [eax+_printf-5] \n\
add esp, 4 \n\
ret \n\
_printf dd printf \n\
string db 'Hello world (from assembly)!', 13, 10, 0 \n\
";
#endif
helloWorld.setAssembler(&assembler);
helloWorld.addPredefinition("printf", printf);
high_resolution_clock::time_point before = high_resolution_clock::now();
bool success = helloWorld.assemble();
high_resolution_clock::time_point after = high_resolution_clock::now();
if (success)
{
std::cout << helloWorld.getMessages();
std::cout << "Successfully built code in " <<
duration_cast<duration<double, std::micro>>(after - before).count() <<
" microseconds.\n";
helloWorld.run(false);
}
else
{
std::cout << helloWorld.getErrors() << std::endl;
}
std::cin.get();
}