Sign in to follow this  
gpulove

Assembly trouble

Recommended Posts

Hi guys. After trying to make more complicated programs in assembly I came across the same problem again and again: running out of registers. How should I compensate for this? I use push and pop to save and restore registers, and this way have more of them to use, but is there a better way? How should I use the stack to overcome this problem? What operations/instructions affect the ESI and EDI registers? Example: I'm trying to raise 1 complex number at a given power n. I run out of registers while doing this. The C++ version of the program would look like this:
#include <iostream>
#include <stdlib.h>
using namespace std;

class Complex
{
	public:
		Complex();
		Complex(int r, int i)
		{
			re = r;
			im = i;
		}
		
		Complex power(int n)
		{
			int r = re;
			int i = im;
			while (n>1)
			{	
				int tre = re;
				re = re*r - im*i;
				im = tre*i + im*r;
								
				n--;			
			}									
						
			return *this;
		} 
		
		int re, im;		
};

ostream& operator << (ostream& o, Complex c)
{
	o << c.re << " " << c.im << endl;	
	return o;
}

int main()
{
	Complex a(1,1);					 
	a.power(10);
	cout << a;

	return 0;
}



But writing all this in assembly tends to complicate things(because I'm a begginer and all :) ) How should I solve the "run out of registers" problem elegantly and efficiently :), well if that asks too much, how should I solve it in the easiest way possible?

Share this post


Link to post
Share on other sites
In most cases, avoid push and pop in favor of direct stack addressing for your local storage. Also, consider the instruction forms that use a register and memory operand instead of using two register operands.

Share this post


Link to post
Share on other sites
Quote:
Original post by gpulove
What operations/instructions affect the ESI and EDI registers?


Instructions that work with the rep/repne instruction

rep movs, rep lods, rep stos, rep cmps, rep scas, etc.

These also affect Ecx

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this