Skip to content

Instantly share code, notes, and snippets.

@shybovycha
Created September 24, 2013 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shybovycha/6689206 to your computer and use it in GitHub Desktop.
Save shybovycha/6689206 to your computer and use it in GitHub Desktop.
Inline assembly in MSVC
#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;
}
.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
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