1. How local variables organized by compiler?
Just consider the below example for how compiler generates code for local variables. We start with writing a simple c program and we compare with assembly program for understanding compiler’s code generation technique.
main()
{
int a, b, c;
a = 10;
b = 20;
c = a + b;
}
In this program we declared three variables all three are local variables we already know local variables are allocated in STACK block. So compiler should generate code for the three variables.
_main proc near
push bp
mov bp, sp
sub sp, 6
mov word ptr [bp-2], 10
mov word ptr [bp-4], 20
mov ax, word ptr [bp-2]
add ax, word ptr [bp-4]
mov word ptr [bp-6], ax
_end _main
The above code generated by turbo c++ compiler. Everybody may have knowledge about stack, that stack will grow from top to bottom. Stack pointer register holds top address of stack segment when ever memory need to allocate in stack, stack pointer will be decrement by some value. Now we need to allocate 6 bytes in stack because three integer variables need 6 bytes. So compiler generates code like “sub sp, 6”.
bp - 2 ---- variable a
bp - 4 ---- variable b
bp - 6 ---- varaible c
From the above information any one can easily understand.
2. How misusing data types will decrease product performance?
We might have heard this statement "Memory should be used very efficiently".
The way of using memory will cause problem
1. Memory not enough.
2. Performance issue.
First one is ok... because the name itself has meaning but the second one is somewhat difficult right?
That is going to explain now with practical example. Consider the below example
main()
{
int a,b,c;
a = 10;
b = 20;
c = a + b;
}
The above program is right version instead of writing like that programmer written like as follows
main()
{
long a,b,c;
a = 10;
b = 20;
c = a + b;
}
The only difference is the data type declaration. So how come this will decrease performance? Integer size is 2 bytes long byte is 4 byte so we can say unused memory is there so its not efficient way of programming, but this inefficient way of programming how will it decrease the performance? Consider the below assembly code then you will realize.
_main proc near
push bp
mov bp,sp
sub sp,12
;
; {
; long a,b,c;
; a = 10;
;
mov word ptr [bp-2],0
mov word ptr [bp-4],10
;
; b = 20;
;
mov word ptr [bp-6],0
mov word ptr [bp-8],20
;
; c = a + b;
;
mov ax,word ptr [bp-2]
mov dx,word ptr [bp-4]
add dx,word ptr [bp-8]
adc ax,word ptr [bp-6]
mov word ptr [bp-10],ax
mov word ptr [bp-12],dx
_main endp
Long data type is occupies 4 bytes but 16-bit turbo c compiler cannot process 32 bit data so compiler has to generate two 16-bit machine instruction for manipulating one 32-bit data so use of long instead of int will cause compiler to generate multiple instruction so once, number of machine instruction increases time taken to execute those machine instructions will increase so product performance will decrease so programmer should be very careful in declaring variable.
3. How constant string formal arguments passed by compiler?
Consider the below example for understanding the question.
main()
{
printf("Hello World\n");
}
printf is a function "Hello World\n" is the argument of the function. I put a question to the readers that
where the string "Hello World\n" will be stroed? whether it will be stored in stack or data block. while calling
printf function, will compiler put the string in stack or only the base address of the string? If it is base address then where the string will be stored?
all will be explained with the following assembly language
_main proc near
push bp
mov bp,sp
;
; {
; printf("Hello World\n");
;
mov ax,offset DGROUP:s@
push ax
call near ptr _printf
pop cx
;
; }
;
pop bp
ret
_main endp
?debug C E9
_TEXT ends
_DATA segment word public 'DATA'
s@ label byte
db 'Hello World'
db 10
db 0
_DATA ends
DATA block code of assembly language
_DATA segment word public 'DATA'
s@ label byte
db 'Hello World'
db 10
db 0
_DATA ends
so from the above code we can conclude Hello World string is stored in DATA block. and the base address used in printf function call for accessing the string
from the above problem we can conclude writitng constant formal arguments will increase usage of DATA block.
No comments:
Post a Comment