Created
September 24, 2013 18:32
-
-
Save shybovycha/6689206 to your computer and use it in GitHub Desktop.
Inline assembly in MSVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
extern "C" int __stdcall my_proc(int, int); | |
int main(void) | |
{ | |
int a = 1, b = 0, c = 2; | |
// calculate b = c^2 - a | |
__asm | |
{ | |
mov eax, c ; eax = c | |
mul eax ; eax = eax * eax = c ^ 2 | |
mov ebx, a ; ebx = a | |
sub eax, ebx ; eax = eax - ebx = c ^ 2 - a | |
mov b, eax ; b = eax | |
} | |
printf("b = (%d ^ 2) - %d = %d\n", c, a, b); | |
// call b = my_proc(c, a) | |
b = my_proc(c, a); | |
printf("called my_proc(%d, %d) = %d\n", c, a, b); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.686 | |
.model flat, stdcall | |
.code | |
my_proc proc public, c1:DWORD, a1:DWORD | |
mov eax, c1 | |
add ebx, a1 | |
mul eax | |
sub eax, ebx | |
ret | |
my_proc endp | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
b = (2 ^ 2) - 1 = 3 | |
called my_proc(2, 1) = 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment