Skip to main content
GameDev.net gamedev.net
🔒 Locked

Assembly trouble

Started by gpulove Apr 5, 2007 at 10:56 AM 2 replies 1k views
Original Post
gpulove
gpulove
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?
Evil Steve
Evil Steve
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.
Rockoon1
Rockoon1
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.
LessBread
LessBread
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
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.