☻ Understanding assembly code of C compiler
Here we are going to discuss about how compiler generates code in assembly language through that some tricks we will play in programming
Consider the below program.
/* Example 1*/
main()
{
int a,b;
printf("Enter a number : ");
scanf("%d", &a);
a / 3;
printf("Quotient: %d and Remainder: %d",_AX, _DX);
}
There is no machine instruction for finding remainder. for finding remainder we need to use div assembly instruction this will place remainder in dx reginster and quotient in ax register
If u want to find remainder and quotient of a particular number no need to use two operator. but after a particular expression we need to take care the result i.e we need to back up the results becuase results will not be in register for long time (i.e) before processor starts to execute next instruction we should back up all the values.
mov ax, the value that we entered
mov bx, divident
idiv bx
quotient will be in AX register
remainder will be in DX register
========================================
This program is some what different, just look at the program then we will discuss
/* Example 2*/
main()
{
fun("I am stalin");
printf("Inside main : %s",_CX);
}
fun(char *p)
{
printf("Inside function : %s\n",p);
*(p + 5) = '?';
*(p + 6) = '\0';
}
output:
Inside function : I am stalin
Inside main : I am ?
inside main we are calling function fun with argument "I am stalin" once control returned from fun function we are printing _CX register its printing "I am ?" how come?
we are passing string "I am stalin" to function fun but compiler will not pass the string as argument instead of that it will store "I am stalin" in DATA block then it will use the base address of the string. how it will send base address to function fun?. compiler generates code like
/* Example 2*/
mov ax, base address
push ax
call fun
pop cx
once control comes back, the address will be in cx register. so we can use the address for printing the string,
that string we changed in function fun. so if we know how compiler generates code, we can do what ever we want. Improving performance of a product plays wonderful role in software development for that programmer should know operating system, computer architecture and compiler understanding.
========================================
No comments:
Post a Comment