Today, we have encountered a problem that we rarely discuss. Let's analyze it here and give it to those who are interested ~~ In C language, K & R is a C language version earlier than the C standard. I believe everyone is not familiar with it .. although it is rarely used now... after all, at that time, I made great contributions. Many Unix/Linux versions have such contributions... the problem encountered here is about function call parameters.
|
// K & R old version
Int func (P1, P2)
Int P1; int P2;
{
//..
}
// Corresponding standard format
Int func (INT P1, int P2)
{
//...
}
|
Before talking about the problem and analysis, we should first point out the differences between the two:
Extracted:
The parameters are named between the parentheses, and their types are declared beforeopening the left brace; undeclared parameters are taken as Int. no parameter list was permitted, so the compiler cocould not readily check that power was being called correctly. indeed, since by default power wowould have been assumed to return an int, the entire declaration might well have been omitted.
The new syntax of function prototypes makes it much easier for a compiler to detect errors in the number of arguments or their types. the old style of declaration and definition still works in ansi c, at least for a transition period, but we stronugly recommend that you use the new form when you have a compiler that supports it.
In short, it means that the parameter checks in the old function definition are processed at runtime, while the new function calls are checked and confirmed during compilation.
Question: The two functions define AA, BB, and function bodies in the same content, except for the Parameter Definition Format.
|
#include <stdio.h>
long AA(p)
long p;
{
return (p*10);
}
long BB(long p)
{
return (p*10);
}
int main()
{
printf("function AA = %d, BB = %d /n",AA(10.0),BB(10.0));
}
|
The final compiled result AA = 0, BB = 100
Disassembly functions AA and BB, the actual content of the function body is the same, the only difference is that the main function calls the pressure stack part of function AA and BB, the following is the specific code of disassembly
|
AA is the same as BB.
0x08048328 <AA+0>: push %ebp
0x08048329 <AA+1>: mov %esp,%ebp
0x0804832b <AA+3>: mov 0x8(%ebp),%edx
0x0804832e <AA+6>: mov %edx,%eax
0x08048330 <AA+8>: shl $0x2,%eax
0x08048333 <AA+11>: add %edx,%eax
0x08048335 <AA+13>: shl %eax
0x08048337 <AA+15>: leave
0x08048338 <AA+16>: ret
|
Call the main functions of AA and BB
|
0x0804834f <main + 0>: Push % EBP
0x08048350 <main + 1>: mov % ESP, % EBP
0x08048352 <main + 3>: Sub $0x8, % ESP
0x08048355 <main + 6>: and $0xfffffff0, % ESP
0x08048358 <main + 9>: mov $0x0, % eax
0x0804835d <main + 14>: Sub % eax, % ESP
0x0804835f <main + 16>: Sub $0x4, % ESP
0x08048362 <main + 19>: Sub $0x8, % ESP
0x08048365 <main + 22>: Push $ 0xa; press BB parameter 0x0a, that is, 10
0x08048367 <main + 24>: Call 0x8048339 <BB>; call bb
0x0804836c <main + 29>: add $ 0xc, % ESP
0x0804836f <main + 32>: Push % eax; push the BB result to the stack
0x08048370 <main + 33>: Push $0x40240000; press the AA parameter. Pay attention to the floating point number.
0x08048375 <main + 38>: Push $0x0. Therefore, two 4 bytes occupy 64 bits.
0x08048377 <main + 40>: Call 0x8048328 <AA>; Call AA
0x0804837c <main + 45>: add $0x8, % ESP
0x0804837f <main + 48>: Push % eax; stack the AA result
0x08048380 <main + 49>: Push $ 0x804843c
0x08048385 <main + 54>: Call 0x8048268 <printf>; call printf
0x0804838a <main + 59>: add $0x10, % ESP
0x0804838d <main + 62>: Leave
0x0804838e <main + 63>: Ret
|
However, in the call of the AA function, only one long and four bytes are required for implicit conversion. Therefore, only four bytes at the top of the stack are called, ranging from 0 to 0, (Originally, 0x40240000 and 0x0), so the final result can only be 0.
As mentioned above, the K & R call mode does not determine the parameter type during compilation. Therefore, simply push the original data into the stack. It is better for a single 4-byte data, however, when the floating point is reached, it will not work. If implicit conversion is performed on the data, only part of the data is used, leading to errors.
It is necessary to talk about the representation format of floating point numbers in computers.
[Excerpted]
Let's talk about the 32-bit float type.
A floating point number X, expressed:
X = A * 2e
Here e Represents the index, and a represents the ending number. Inside the computer, they are all represented in binary. here, a is represented in binary scientific notation. Since the first digit in scientific notation is always 1 (except 0), the first digit is omitted. E indicates that a negative number is displayed, so 127 is added, and 127 is subtracted from the actual operation.
IEEE stipulates that the 32-bit float is split into the following format, and the left side is high:
0 0000 0000 0000000 00000000 00000000
Highest, 32nd-23, 8-23
Symbol digit index digit tail Digit
0 is positive, 1 is negative-127 ~ + 127 0 ~ 0x 7f FF
Float range:-3.40282 * e38 ~ + 3.40282 * e38
Generally, it is a decimal number, which must be converted to binary. To convert decimal to binary, the part greater than 1 is divided by 2 to take the remainder, and the part smaller than 1 is multiplied by 2 to take the integer.
For example, if 8.5 is converted to a binary value of 1000.1, It is 1.0001*23 if it is processed in scientific notation. Note: Because 1.0001 is removed from the first 1, it is 0001, 3. You need to add 127 to 130, and the binary value is 10000011, which is expressed:
0 10000011 0001000 00000000 00000000
The hexadecimal format is 0x41 98 00 00. Generally, Intel series CPUs use the tail storage, that is, the high byte is placed at the back: 0x00 00 98 41, then a floating point representation is completed.
Note: Floating Point Number 0.0 indicates 0x00 00 00 in the computer.
Analyze the remaining items by yourself ~~