简体   繁体   中英

How are numerical constants represented in a compiled binary?

For example, given a simple C program:

#include <stdio.h>
int main() {
  float x = 3.14f;
  char* y = "abcdefghijklmnopqrstuvwxyz";
  printf("%f %s", x, y);
  return 0;
}

How is the constant 3.14f output to the executable binary?

For strings, it usually shows up directly in the file (findable with grep abcdefghijklmnopqrstuvwxyz a.out or strings ) but I don't see the same for numbers (even in their binary representations).

Is the way that it's compiled to binary general across compilers or is it specific to each compiler/language?

In general a compiler (that writes the machine code into a file directly rather than generating assembly and then invoking an assembler on it - which would circumvent this question altogether) won't write the number to a file any differently than any other program would write the number to a binary file: by first parsing the string into a number using the language's standard number parsing facility and then using the standard binary output method of the language that you're using to write your compiler.

For example in C, the process of writing the value of a floating point constant (stored in the string char *exp ), could look like this (assume that we already checked that the given expression is indeed a float constant and that the output stream is at the position where the value of the float should be placed):

float floatValue;
sscanf(exp, "%f", &floatValue);
fwrite(&floatValue, sizeof(float), 1, outfile);

What fwrite does here is simply to go through floatValue 's bytes in memory and write them one-by-one to the file. So the only interesting bit is what scanf does when reading a number from a string (or for that matter from a file or user input) into a number in memory.

This differs greatly between integers and floats. For floats, on the vast majority of all platforms, the IEEE floating point standard is used. For unsigned integers, the string is simply translated into its normal binary representation. For signed integers, again on the vast majority of all platforms, Two's complement is used, which represents non-negative numbers exactly like in the unsigned case and negative numbers by taking their absolute value, flipping its bits and adding 1 to it.

but I don't see the same for numbers (even in their binary representations

When you say that you didn't find the binary representation, were you looking for a sequence of ASCII '0' and '1' characters, or did you look at the file as binary (or hex) and did not find the given sequence? In the latter case, you either just missed it, compiled with optimizations that removed the constant because it was not needed, or you were wrong about what the correct binary representation was. In the former case you didn't find it because numbers are stored in binary, not as text (not even text representing a binary number).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM