Home > Back-end >  How does a C compiler convert a constant to binary
How does a C compiler convert a constant to binary

Time:08-25

For the sake of specifics, let's consider GCC compiler, the latest version.

Consider the instruction int i = 7;.

In assembly it will be something like

MOV 7, R1

This will insert the value seven to register R1. The exact instruction may not be important here.

In my understanding, now the compiler will convert the MOV instruction to processor specific OPCODE. Then it will allocate a (possibly virtual) register. Then the constant value 7 needs to go in the register.

My question:

How does the 7 is actually converted to binary?

Does the compiler actually repeatedly divide by 2 to get the binary representation? (May be afterwards it will convert to HEX, but let's remain on the binary step).

Or, considering that the 7 is written as a character in a text file, is there a clever look up table based technique to convert any string (representing a number) to a binary value?

If the current GCC compiler uses built in function to convert a string 7 to a binary 0111, then how did the first compiler convert a text based string to a binary value?

Thank you.

CodePudding user response:

How does the 7 is actually converted to binary?

First of all, there's a distinction between the binary base 2 number format and what professional programmers call "a binary executable", meaning generated machine code and most often expressed in hex for convenience. Addressing the latter meaning:

Disassemble with binaries (for example at enter image description here

and assembler is another program that takes this text file and converts it into "binary" format.
Assembler can be also written by c language here we also need a mapping i.e movl->(0000110101110...) but this one is binary not ascii. and we need to write this binary into a file as-is.
Converting numbers into binary format is also redundant because numbers are already in binary form when they are loaded into memory.
the question is how they are converted/placed in to memory is a problem of the loader program of the operating system which exceeds my knowledge.

  • Related