-1
$\begingroup$

More generally, how those translating softwares, for example, an assembler, like nasm, know how to transform in machine code the number "10", from this command: add marks, 10.

I know there is other things that needs to be comprehended about compilers, but I can't find some intuitive explications, like the addition of two numbers, where there were implemented logical gates which deal with those operations.

Specifically, which hardware resources are involved in creating the binary form of a number?

$\endgroup$
  • $\begingroup$ Okay, thanks for clarifying. I still think the question is ill-posed, but explaining why is probably the answer you need. $\endgroup$ – Raphael Oct 9 '16 at 21:01
  • $\begingroup$ This answer may help you a bit. $\endgroup$ – Raphael Oct 9 '16 at 21:02
  • 1
    $\begingroup$ Thank you very much! It's a great article. I will look at it as a map for learning things about how computers work. $\endgroup$ – Iulian Barbu Oct 11 '16 at 6:36
1
$\begingroup$

Numbers are translated according to the encoding supported by the architecture, usually 2's complement for signed integers, and regular unsigned binary encoding for unsigned integers in modern computers. There exist deterministic algorithms for translating a decimal number into binary form and back.

The compiler will perform these algorithms on the character constants it reads from the source code (most likely in a text encoding), and then use some sort of procedure or function to translate them into binary form. Compilers will also usually reduce expressions to constants if possible as a part of their optimizations (e.g. 10 >> 2 becomes 2).

One possible function for, say, compilers written in C might be the atoi() function from the C Standard Library, which translates a string of ASCII characters into an int. This int will have an implementation-specific encoding, but is usually 2's complement these days.

Since the translation of text to binary encoding is implementation-specific, the terms of how it actually does it (whether in software or hardware) is not guaranteed. However, usually the translation will just take place as a procedure written in software to run on top of the more basic hardware instructions (e.g. a direct ASCII --> binary integer representation may not exist on something as simple as 32-bit MIPS, but there will be an atoi() function for a C Standard Library on MIPS).

$\endgroup$
  • $\begingroup$ So the hardware resources involved in translating the "10" string into binary form are let say esoteric or maybe the logic behind isn't so standardized for a software engineer. $\endgroup$ – Iulian Barbu Oct 9 '16 at 20:25
  • $\begingroup$ @IulianBarbu Yes. But there are popular algorithms and implementations and hardware architectures that are standard (e.g. GNU libc implementation of atoi() that is architecture-independent). $\endgroup$ – VermillionAzure Oct 9 '16 at 20:31
  • $\begingroup$ Yes. Actually, after consulting atoi.c implementation I've seen the line that actually explained me something. It is used character encoding, as you said. I don't know for sure but I presume that there are registers with character corespondence to binary code. And from this you could generate all kinds of numbers which could be processed by CPU. $\endgroup$ – Iulian Barbu Oct 9 '16 at 20:40
1
$\begingroup$

The translation of constants in their decimal form is done during the parsing stage of the compilation process.

In the parsing stage, the compiler executes code that transforms the text file into a compiler-internal representation of the program. The parser has a set of rules that tell it when to expect a number. Numbers are translated into binary form by a compiler functions that a programmer once wrote.

An assembler has internally a list of opcodes and the parameter types of the architecture. It will try to match the line with one of the type classes. So it will, for example, accept the line "add eax, 3" if and only if there is an opcode for adding a constant (of some length) to a register. The string "3" can only be a number, so it will be parsed as such.

About the additions: There are two cases to be considered. Modern compilers can often perform arithmetic operations between constants at compile time. So if a C program contains the phrase "10+20", then the machine code will contain a "30" instead. In all other cases, the generated program will contain an addition operation in the binary opcode format of the processor that the program is supposed to run on. So the addition is just forwarded to the processors built-in addition command.

How an addition is implemented in a process is a classical topic in computer architecture. The respective Wikipedia page contains an overview.

$\endgroup$
  • $\begingroup$ Thank you for your answer. Actually, you've helped me to clearify better my question, so I've updated it. $\endgroup$ – Iulian Barbu Oct 9 '16 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.