Simple add function

Posts 1–6 of 6 · Page 1 of 1
Simple add function
Hello guys, I am trying to learn asm and I am in need of some help. How come this does not work?
Code:
#include <stdio.h>
#include <iostream>
int add(int num1, int num2);
using namespace std;
int main(void)
{
	int result = add(3, 2);
	cout << result;
	system("pause");
}

int add(int num1, int num2)
{
	int result = 0;
	__asm
	{
		mov result, offset num1
		push result
		mov result, offset num2
		push result
	}
	return result;
}
As you can see I am very new to asm.
I'm not great at asm and even worse on mixed programming but I can tell you this: offset means you're going to be getting the memory addresses for num1 and num2. You should be pushing the values themselves, I think. Try using this:

mov result, num1
add result, num2

or

push num1
push num2
fadd
pop result
Also, what error are you getting?

Edit: Right now I can't test the code but later I'll test at home and try to help you. You should also look for assembly command sheets, they're very helpful
Quote Originally Posted by samlarenskoog View Post
Your question
I have tested this at home. I came up with this code:
Code:
#include "stdafx.h"
#include <stdio.h>
#include <iostream>

using namespace std;

int add(int num1, int num2)
{
	int result;
	__asm
	{
		xor eax, eax;
		mov eax, num1;
		add eax, num2;
		mov result, eax;		
	}
	return result;
}

int main()
{
	int result, a, b;
	cout << "Variable 1: ";
	cin >> a;
	cout << "Variable 2: ";
	cin >> b;
	result = add(a, b);
	cout << result << endl;
	system("pause");
}
I don't really know why it doesn't work as I never did c++ and asm together but the error I was getting was improper argument type on both mov lines. By looking at my code you can tell that what was wrong was using result in the mov with num1 and 2. Also, as I said before, offset means the address of the variable and not the actual value stored in that address. I'm not an expert in this but I hope I could help.
I forgot to explain what XOR EAX, EAX is for, it will set EAX to 0, or in other words, clear the address
Quote Originally Posted by FatAssHacker123 View Post
I forgot to explain what XOR EAX, EAX is for, it will set EAX to 0, or in other words, clear the address
Why would you want to do that if you move something into eax anyways right after?

Also, why are you not using naked functions?
Quote Originally Posted by samlarenskoog View Post
Hello guys, I am trying to learn asm and I am in need of some help. How come this does not work?
Code:
#include <stdio.h>
#include <iostream>
int add(int num1, int num2);
using namespace std;
int main(void)
{
	int result = add(3, 2);
	cout << result;
	system("pause");
}

int add(int num1, int num2)
{
	int result = 0;
	__asm
	{
		mov result, offset num1
		push result
		mov result, offset num2
		push result
	}
	return result;
}
As you can see I am very new to asm.
1. Which compiler are you using?
2. In GCC you use asm(), not asm{}.
3. If you are using GCC, you will also need to do a manual clobbering. For VC++, you won't.
Posts 1–6 of 6 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us