-1

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?

Raphael
  • 73,212
  • 30
  • 182
  • 400
Iulian Barbu
  • 23
  • 1
  • 4

2 Answers2

1

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.

DCTLib
  • 2,817
  • 17
  • 13
1

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).

CinchBlue
  • 614
  • 4
  • 16