Post: x86 assembly: using math (add,subtract,divide...)
05-05-2015, 03:14 AM #1
(adsbygoogle = window.adsbygoogle || []).push({}); hi ngu Happy ive recently been learning abit of x86 assembly, so i decided to share it Smile
ive been using the vs2012 inline assembler to compile my code. there are many advantages to using inline assembly with c++, for example you can reverse values with out calling std::reverse by using the 'rev' instruction.
enjoy Happy
    
int Add(int a,int b)
{
int ret = 0;
__asm {
mov eax, a//move our parameter a to the register eax/ax
mov ebx, b//move our parameter b to the register ebx/bx
add ebx, eax//add the registers eax and ebx
mov ret,ebx//move the added register to our return varible
}
return ret;
}
int Sub(int a,int b)
{
int ret = 0;
__asm {
mov eax, a//move our parameter a to the register eax/ax
mov ebx, b//move our parameter b to the register ebx/bx
sub ebx, eax//subtract the registers eax and ebx
mov ret,ebx//move the subtracted register to our return varible
}
return ret;
}
int Mullie(int a,int b)
{
int ret = 0;
__asm {
mov eax, a//move our parameter a to the register eax/ax
mov ebx, b//move our parameter b to the register ebx/bx
imul ebx,eax//multiply the registers eax and ebx
mov ret,ebx//move the multiplied register to our return varible
}
return ret;
}
int Divide(int a,int b)
{
int ret = 0;
__asm {
mov eax, a//move our parameter a to the register eax/ax, this will be the dividend
mov ebx, b//move our parameter b to the register ebx/bx, this will be the arg
div ebx//divides the dividend(eax) by the arg(ebx)
mov ret,ebx//move the divided register to the return varible
}
return ret;
}

example on how to test:
    
#include <stdio.h>
int j = Sub(2,4);
printf("%i\n",j);
_wsystem(L"pause");

The following 2 users say thank you to milky4444 for this useful post:

John, Pichu
05-05-2015, 03:25 AM #2
Originally posted by milky4444 View Post
hi ngu Happy ive recently been learning abit of x86 assembly, so i decided to share it Smile
ive been using the vs2012 inline assembler to compile my code. there are many advantages to using inline assembly with c++, for example you can reverse values with out calling std::reverse by using the 'rev' instruction.
enjoy Happy
    
int Add(int a,int b)
{
int ret = 0;
__asm {
mov eax, a//move our parameter a to the register eax/ax
mov ebx, b//move our parameter b to the register ebx/bx
add ebx, eax//add the registers eax and ebx
mov ret,ebx//move the added register to our return varible
}
return ret;
}
int Sub(int a,int b)
{
int ret = 0;
__asm {
mov eax, a//move our parameter a to the register eax/ax
mov ebx, b//move our parameter b to the register ebx/bx
sub ebx, eax//subtract the registers eax and ebx
mov ret,ebx//move the subtracted register to our return varible
}
return ret;
}
int Mullie(int a,int b)
{
int ret = 0;
__asm {
mov eax, a//move our parameter a to the register eax/ax
mov ebx, b//move our parameter b to the register ebx/bx
imul ebx,eax//multiply the registers eax and ebx
mov ret,ebx//move the multiplied register to our return varible
}
return ret;
}
int Divide(int a,int b)
{
int ret = 0;
__asm {
mov eax, a//move our parameter a to the register eax/ax, this will be the dividend
mov ebx, b//move our parameter b to the register ebx/bx, this will be the arg
div ebx//divides the dividend(eax) by the arg(ebx)
mov ret,ebx//move the divided register to the return varible
}
return ret;
}

example on how to test:
    
#include <stdio.h>
int j = Sub(2,4);
printf("%i\n",j);
_wsystem(L"pause");

I tried to do this, but could never figure out how to do it.. Thanks!

The following user thanked John for this useful post:

Sen

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo