Re: Global versus Stack variables

"Jatin Bhateja" <jatin.bhateja@amdocs.com>
26 Nov 2005 00:16:46 -0500

          From comp.compilers

Related articles
Global versus Stack variables shreyas76@gmail.com (shrey) (2005-11-19)
Re: Global versus Stack variables oliverhunt@gmail.com (oliverhunt@gmail.com) (2005-11-21)
Re: Global versus Stack variables gah@ugcs.caltech.edu (glen herrmannsfeldt) (2005-11-21)
Re: Global versus Stack variables henry@spsystems.net (2005-11-21)
Re: Global versus Stack variables torbenm@app-5.diku.dk (2005-11-21)
Re: Global versus Stack variables jatin.bhateja@amdocs.com (Jatin Bhateja) (2005-11-26)
Re: Global versus Stack variables napi@cs.indiana.edu (2005-11-26)
Re: Global versus Stack variables david.thompson1@worldnet.att.net (Dave Thompson) (2005-12-05)
| List of all articles for this month |
From: "Jatin Bhateja" <jatin.bhateja@amdocs.com>
Newsgroups: comp.compilers
Date: 26 Nov 2005 00:16:46 -0500
Organization: Compilers Central
References: 05-11-094
Keywords: code
Posted-Date: 26 Nov 2005 00:16:46 EST
Thread-Topic: Re: Global versus Stack variables

Hi,


>As John said, it depends on your language and your compiler. But in
>C-like languages on modern CPU's (with modern compilers), local
>variables tend to be register allocated (unless their address is
>taken) where global variables are memory allocated. This means that
>access to gobal variables usually require a memory reference, though a
>function can sometimes make a temporary copy in a register and use
>this for several accesses. It must write back before returning,
>calling another function or accessing a variable that might be aliased
>with the global variable.


I feel that accessing both local and global variables requires some memory
access, but it again depends on the architecture and complier.


Kindly consider the following code


#include "stdio.h"


int i = 10;


int main() {
                i = i + 75;
                return i;
}


Now consider the assembly corresponding to it (Turbo compiler)


__DATA segment word public 'DATA'
_i label word
                dw 10
_DATA ends
_TEXT segment byte public 'CODE'
; ?debug L 5
_main proc near
; ?debug L 6
                mov ax,word ptr DGROUP:_i #<---- Here we can see that some memory
access to data segment is made initially
                add ax,75
                mov word ptr DGROUP:_i,ax
; ?debug L 7
                mov ax,word ptr DGROUP:_i
                jmp short @1
@1:
; ?debug L 8
                ret
_main endp
_TEXT ends


Similarly consider following code snippet


#include "stdio.h"


int main() {
                int i = 10;
                i = i + 75;
                return i;
}


Consider the assembly corresponding to it (Turbo compiler)


_TEXT segment byte public 'CODE
; ?debug L 3
_main proc near
              push si#<--- Here we directly allocate the register to the
corresponding local (i).
; ?debug L 4
              mov si,10
; ?debug L 5
              mov ax,si
              add ax,75
              mov si,ax
; ?debug L 6
              mov ax,si
              jmp short @1
@1:
; ?debug L 7
              pop si
              ret
_main endp
_TEXT ends


But when the number of locals are more for eg. Consider the following code
snippet


#include "stdio.h"


int main() {
                int i = 10,j=6,k=6,l=5
                i = i + j + k + l;
                return i;
}


Consider its assembly


_TEXT segment byte public 'CODE'
; ?debug L 3
_main proc near
                push bp
                mov bp,sp
                sub sp,4
                push si
                push di
; ?debug L 4
                mov si,10 #<--- i and j are allocated registers
                mov di,6
                mov word ptr [bp-4],6 #<----- k and l are accessed from stack.
                mov word ptr [bp-2],5
; ?debug L 5
                mov ax,si
                add ax,di
                add ax,word ptr [bp-4]
                add ax,word ptr [bp-2]
                mov si,ax
; ?debug L 6
                mov ax,si
                jmp short @1
@1:
; ?debug L 7
                pop di
                pop si
                mov sp,bp
                pop bp
                ret
_main endp
_TEXT ends




Thus if the architecture does not have large number of GPRs (general purpose
registers)
then the locals are assigned the correct offset in the activation record and
when the activation record is pushed onto the stack then access to these
locals are made by using the base (frame pointer) + offset mechanism thus
memory access is made.




Thanks and Best Regards,
 
Jatin Bhateja
Subject Matter Expert


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.