• Advertisement
Sign in to follow this  

Assembly trouble

This topic is 3945 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
The obvious answer is to look at the assembly generated by MSVC and see what it does. All local variables are stored and accessed on the stack, using [ESP-4] etc.

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
Sign in to follow this  

  • Advertisement