Skip to content
Low-level programming · Higher-level thinking
Login Register
Forum / Bugs and Fixes

local variable error

A community discussion post from the original code:zIRON forum.

Emil_halim member
Posts
639
Threads
104
Location
Alex, Egypt

local variable error

this works fine , note well , right is global variable here

program WIN32CUI 'test';

#include 'console.zir';
#include 'zirutils.zir';

char right[5100];
dword indx;
char tmp[255];

   indx = 2;
      
   eax = indx
   imul  eax,255
   Edx = @right
   add eax,Edx
   push eax
   IntToStr(indx,@tmp);
   print(@tmp,'\r\n');
   pop eax
   IntToStr(eax,@tmp);
   print(@tmp,'\r\n');  
   
   indx = 3;
      
   eax = indx
   imul  eax,255
   Edx = @right
   add eax,Edx
   push eax
   IntToStr(indx,@tmp);
   print(@tmp,'\r\n');
   pop eax
   IntToStr(eax,@tmp);
   print(@tmp,'\r\n');  
wait_key(nil);
ExitProcess(0);              

but this does not work correctly , try compile the 2 examples and see the results

not well right is local variable here

program WIN32CUI 'test';

#include 'console.zir';
#include 'zirutils.zir';

procedure main()
{
char right[5100];
dword indx;
char tmp[255];

   indx = 2;
      
   eax = indx
   imul  eax,255
   Edx = @right
   add eax,Edx
   push eax
   IntToStr(indx,@tmp);
   print(@tmp,'\r\n');
   pop eax
   IntToStr(eax,@tmp);
   print(@tmp,'\r\n');  
   
   indx = 3;
      
   eax = indx
   imul  eax,255
   Edx = @right
   add eax,Edx
   push eax
   IntToStr(indx,@tmp);
   print(@tmp,'\r\n');
   pop eax
   IntToStr(eax,@tmp);
   print(@tmp,'\r\n');  
}

main();   
wait_key(nil);
ExitProcess(0);          

i think getting address of local variable has some problem.

http://www.freewebs.com/ogremagic/index.htm

Replies

Colin admin
Posts
928
Threads
49
Location
Bulgaria

What is the problem? I do not have any error.

Emil_halim member
Posts
639
Threads
104
Location
Alex, Egypt

ok , run the 2 code and compare the results , it is logical error.

http://www.freewebs.com/ogremagic/index.htm

Colin admin
Posts
928
Threads
49
Location
Bulgaria

ok i see.. it is because you are often using EAX ... too often :P

local variables called like so: @var, will use eax trashing the value... in your case:

   IntToStr(eax,@tmp);

this code here is destroyed before being pushed onto the stack.. so it produces something like:

lea eax, [ebp-...]
push eax
push eax
call ...

what you could do is...

ecx = @tmp;
IntToStr(eax, ecx);

which should produce:

lea ecx, [ebp-...]
push ecx
push eax
call ...

I will come up with a better solution for this in a newer version, once i finish the new lea handler ;)

Colin admin
Posts
928
Threads
49
Location
Bulgaria
program WIN32CUI 'test';

#include 'console.zir';
#include 'zirutils.zir';

procedure main() {
	char right[5100];
	dword indx;
	char tmp[255];

   indx = 2;
      
   eax = indx
   imul  eax,255
   Edx = @right
   add eax,Edx
   push eax
   IntToStr(indx,@tmp);
   print(@tmp,'\r\n');
   pop eax
   ecx = @tmp;
   IntToStr(eax,ecx);
   print(@tmp,'\r\n');  
   
   indx = 3;
      
   eax = indx
   imul  eax,255
   Edx = @right
   add eax,Edx
   push eax
   IntToStr(indx,@tmp);
   print(@tmp,'\r\n');
   pop eax
   ecx = @tmp;
   IntToStr(eax,ecx);
   print(@tmp,'\r\n');  
}

main();   
wait_key(nil);
ExitProcess(0);            
Colin admin
Posts
928
Threads
49
Location
Bulgaria

slightly better version:

program WIN32CUI 'test';

#include 'console.zir';
#include 'zirutils.zir';

procedure main() {
	char right[5100];
	dword indx;
	char tmp[255];

	indx = 2;
      
	eax = indx
	imul eax, 255
	edx = @right
	eax += edx;
	ecx = @tmp;
	IntToStr(indx, ecx);
	print(@tmp,'\r\n');   
	ecx = @tmp;
	IntToStr(eax, ecx);
	print(@tmp,'\r\n');  
   
	indx = 3;
      
	eax = indx
	imul eax, 255
	edx = @right
	eax += edx;
	ecx = @tmp;
	IntToStr(indx, ecx);
	print(@tmp,'\r\n');
	ecx = @tmp;
	IntToStr(eax,ecx);
	print(@tmp,'\r\n');  
}

main();   
wait_key(nil);
ExitProcess(0);