RE: Assembly as a first language? 02-27-2017, 07:39 AM
#12
Okay so here's an example I just cribbed from Uncle Google:
Spin up Visual c++, create a console project and cut and paste the following code in. Then you can put breakpoints in it and step through it to see what it's doing.
(Puts on flame jacket - yeah I know it's lame as fuck but it's an example of how a total beginner could do it...)
Following code will *work*....
#include <stdio.h>
#include <stdafx.h>
int power2(int num, int power);
int main(void)
{
printf_s("3 times 2 to the power of 5 is %d\n", \
power2(3, 5));
}
int power2(int num, int power)
{
__asm
{
mov eax, num; Get first argument
mov ecx, power; Get second argument
shl eax, cl; EAX = EAX * (2 to the power of CL)
}
// Return with result in EAX
}
Spin up Visual c++, create a console project and cut and paste the following code in. Then you can put breakpoints in it and step through it to see what it's doing.
(Puts on flame jacket - yeah I know it's lame as fuck but it's an example of how a total beginner could do it...)
Following code will *work*....
#include <stdio.h>
#include <stdafx.h>
int power2(int num, int power);
int main(void)
{
printf_s("3 times 2 to the power of 5 is %d\n", \
power2(3, 5));
}
int power2(int num, int power)
{
__asm
{
mov eax, num; Get first argument
mov ecx, power; Get second argument
shl eax, cl; EAX = EAX * (2 to the power of CL)
}
// Return with result in EAX
}


![[+]](https://sinister.li/images/modern/collapse_collapsed.png)