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

global variable redeclared

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

0CodErr member
Posts
201
Threads
37
Location
Unknown

global variable redeclared

Cannot compile this:

program RAW_IMAGE 'test';
  
#set bits 32;

dword a, b;

procedure MyProc() {
  dword a, b;
  a = b;
}

MyProc();

But was compiled when i commented first

dword a, b;

.

Also after that i noticed that in generated raw binary MyProc looks:

00000005  55                push ebp
00000006  8BEC              mov ebp,esp
00000008  81EC08000000      sub esp,0x8
0000000E  FF75F8            push dword [ebp-0x8]
00000011  8F45FC            pop dword [ebp-0x4]
00000014  83C408            add esp,byte +0x8
00000017  C9                leave
00000018  C3                ret

Seems that you use

add esp,byte +0x8

to restore stack.

But it can be done also with

mov esp,ebp

But you already use

leave

Next, i give quote from AMD Manual:

LEAVE Releases a stack frame created by a previous ENTER instruction. To release the frame, it copies the frame pointer (in the rBP register) to the stack pointer register (rSP), and then pops the old frame pointer from the stack into the rBP register, thus restoring the stack frame of the calling procedure. The 32-bit LEAVE instruction is equivalent to the following 32-bit operation: MOV ESP,EBP POP EBP

Replies

Colin admin
Posts
928
Threads
49
Location
Bulgaria

The CPU mode should offer this optimisation, the add esp is actually for local variables, where-as leave is for the stack parameters on call.

try

#set cpu 686;

I will also confirm if this is working correctly, if not I will take a look and correct this.

As for global variable, this is because your variable is already declared and you are trying to use the same identifier for the local variable..... I could make it so that they can be redeclared, but it was an intended functionality.

0CodErr member
Posts
201
Threads
37
Location
Unknown
Invalid directive operand cpu
the add esp is actually for local variables, where-as leave is for the stack parameters on call.

I do not understand why you think that it need. I think that just should remove

add esp,byte +0x8
0CodErr member
Posts
201
Threads
37
Location
Unknown

Also, just to let you knew, in this our case we can code esp -= 8; as:

83EC08            sub esp,byte +0x8
Colin admin
Posts
928
Threads
49
Location
Bulgaria

Sorry I misunderstood, you're right, the final add is not needed with stack frame, I will correct this.

It seems set cpu mode is also not working, I will also fix this.

Thanks.

0CodErr member
Posts
201
Threads
37
Location
Unknown

I think it is relevant to this topic.

program RAW_IMAGE 'test';

#set bits 32;

const a = 0;

procedure proc1(){
  const a = 1;
}
Unexpected number 0

Is it supported local constants or not? It would be more convinient :)

Also local variables still not supported.

0CodErr member
Posts
201
Threads
37
Location
Unknown

hmm... strange

This compile -- OK:

program RAW_IMAGE 'test';

#set bits 32;

//dword i;

procedure proc1(){
  dword i; i = 100500;
  while (i <> 0) {i--;}
}
procedure proc2(){
  dword i; i = 100500;
  while (i <> 0) {i--;}
}
procedure proc3(){
  procedure proc4(){
    procedure proc5(){
      dword i; i = 100500;
      while (i <> 0) {i--;}
    }  
    dword i; i = 100500;
    while (i <> 0) {i--;}
    proc5();
  }
  dword i; i = 100500;
  while (i <> 0) {i--;}
  proc4();
}

proc1(); proc2(); proc3();

But if uncomment global

//dword i;

then cannot compile.

Colin admin
Posts
928
Threads
49
Location
Bulgaria

local constants are possible.

program RAW_IMAGE 'test';

#set bits 32;

const a = 0;

procedure proc1(){
  const a = 1;
}

In this case, you are writing const 0 = 1 because you already declared a as 0 and since it is a global const, it can be accessed from anywhere.

As for the second problem, global variables can not be redeclared right now. I may consider to make this possible in one of the following releases.