Translation of high-level language constructs to Assembly/Machine Language
Most software today is written by programmers in a high-level language, such as C++, Java, or Python. Yet, whatever program is executed on a CPU, needs to be specified in its machine-language. How is it possible, then, to run a program written in a high-level language?
​
A high-level language can be physically executed on a CPU, which can only "understand" its machine-language, with two main (possibly combined) techniques: interpretation and compilation. In the case of interpretation, each high-level instruction is executed by an "interpreter", a program that acts as a sort of virtual CPU, having as its Instruction Set the instructions available in the high-level language. Yet this interpreter, in turn, must be executed as a program in machine-language on the real CPU. In the case of compilation, the programs in a high-level language are first translated (compiled) to machine language, before being executed on the CPU.
​
Yet, programs written in high-level languages use programming constructs such as IF-THEN-ELSE, FOR, WHILE-DO, and so on. How can these constructs be translated to machine language, where such high-level instructions are not available? This is possible mainly exploiting the control-flow instructions jump on condition and unconditional jump, as shown in this section with the help of a few concrete examples.
Basic constructs
More complex constructs









W, X, AND Y are initialized (a numerical value is assigned to them).
The sum of W and X is then assigned to the variable Y.
Initially W=0, X=0, Y=0 (labels of the last three memory cells).
LOD #2: 2 is loaded into the accumulator.
STO W: the result is stored into (the memory cell of address) W
LOD #8: 8 is loaded into the accumulator.
STO X: the result is stored into (the memory cell of address) X
LOD W: the content of (memory cell of address) W is loaded into the accumulator.
ADD X: (the content of the cell of address) X is added to the value in the accumulator.
STO Y: the result is stored into (the memory cell of address) Y.
HLT: Stops all further operations.
Results: W=2, Y=8, Y=10.
Here the content of the RAM memory, as well as the memory addresses, are visualized in binary code. These are the values as they are physically stored and handled - but it is obviously more convenient, for a human, to use the equivalent mnemonics, working with the assembly language and a more familiar numbering base.
Initially Y=10, W=2, and X=0;
3 is multiplied by the value of Y. The result is then assigned to a temporary variable (TMP), and is subsequently added to the quotient of 2/W;
the result shall be: Y=10, W=2, TMP=30 and X=31.
Assertion: Y=10, W=2, TMP=0, X=0 (labels of the last four memory cells).
LOD #3: 3 is loaded into the accumulator.
MUL Y: (the content of the cell of address) Y is multiplied by the value in the accumulator.
STO TMP: the result is stored into (the memory cell of address) TMP
LOD #2: 2 is loaded into the accumulator.
DIV W: (the content of the cell of address) W is divided to the value in the accumulator.
ADD TMP: (the content of the cell of address) TMP is added to the value in the accumulator.
STO X: the result is stored into (the memory cell of address) X.
HLT: Stops all further operations.
Results: Y=10, W=2, TMP=30, X=31
Initially COUNT=0;
Infinite loop in which COUNT is incremented by one at every iteration.
Initially A=0 (label of the last memory cell).
LOD A: the content of (memory cell of address) A is loaded into the accumulator.
ADD #1: 1 is added to the value in the accumulator.
STO A: the result is stored into (the memory cell of address) A.
JMP FOR: Jumps unconditionally to the address corresponding to the label "FOR”.
Results: A takes the values 0, 1, 2, 3, ...
EXAMPLE: translation of the initialization and sum of two variables
EXAMPLE: translation of an arithmetic formula
EXAMPLE: translation of an infinite loop









Initially X=8, Y=0, and Z=1;
The high-level code specifies that if X equals 3 then Y is set to Y+5, otherwise Z is set to Z+2.
In this case X is different from 3, so the ELSE clause is executed. Hence the result will be: Y=2 and Z=3.
Initially X=8, Y=2, Z=1 (labels of the last three memory cells).
LOD X: the content of (memory cell of address) X is loaded into the accumulator.
CMP #3: X is compared to 3. T,hat is, the operation 8-3 is performed, which sets the NZ Flag (without affecting the accumulator).
JNZ ELSE: as NZ is set, jumps to the (address corresponding to the) label “ELSE”.
LOD #5: 5 is loaded into the accumulator.
ADD Y: (the content of the cell of address) Y is added to the value in the accumulator.
STO Y: the result is stored into (the memory cell of address) Z.
LOD #2: 2 is loaded into the accumulator.
ADD Z: (the content of the cell of address) Z is added to the value in the accumulator.
STO Z: the result is stored into (the memory cell of address) Z.
HLT: Stops all further operations.
Results: X=8, Y=2, Z=3.
Here the content of the RAM memory, as well as the memory addresses, are visualized in binary code. These are the values as they are physically stored and handled - but it is obviously more convenient, for a human, to use the equivalent mnemonics, working with the assembly language and a more familiar numbering base.
Initially MAX=5, COUNT=0, SUM=0
​
The high-level code specifies that while COUNT differs from MAX, the instructions COUNT=COUNT+1 and SUM=SUM+COUNT
are to be executed.
​
The result of the execution of this loop will be: MAX=5, COUNT=5, SUM=15.
Initially MAX=5, COUNT=0, SUM=0 (labels of the last three memory cells).
LOD COUNT: COUNTis loaded into the Accumulator.
CMP MAX: MAX is compared to the accumulator, setting the corresponding flags.The operation (0-5) is executed in the first cycle, the operation (5-5) in the last cycle.
JZ ENDWHILE: flag Z is not set in the first cycle, hence the jump is not executed. The flag Z is only set in the last cycle, when the control jumps to the address corresponding to the label “END WHILE”, terminating the iterations.
ADD #1: 1 is added to the content of the accumulator.
STO COUNT: the result is stored in (the memory cell) COUNT.
ADD SUM: SUM is added to the value of the accumulator.
STO SUM: the result is stored in (the cell of address) SUM.
JMP WHILE: Jumps unconditionally to the address corresponding to the label “WHILE”.
HLT: Stops all further operations.
Initially COUNT=0.
Starting from I=3, cycle backwards through the loop until I reaches 0.
Every loop increments the counter COUNT by one.
The result will be: COUNT=3, I=0.
Initially I=3, COUNT=0 (labels of the last two memory cells).
Lod I: the content of the memory cell of address I is loaded into the accumulator.
CMP #1: 1 si compared to the content of the accumulator. In case the accumulator contains 1, the operation (1-1) sets the Z flag.
JZ ENDFOR: If the Z flag is set, jump to the address corresponding to the label “ENDFOR”.
LOD COUNT: (the content of the memory cell of address) COUNT is loaded into the accumulator.
ADD #1: 1 is added to the value of the accumulator.
STO COUNT: the result is stored into the memory cell of address COUNT.
LOD I: the content of (the memory cell of address) I is loaded into the accumulator.
SUB #1: 1 is subtracted from the value of the accumulator.
STO I: the result is stored into the memory cell of address I.
JMP FOR: Jumps to the address corresponding to the label “For”.
HLT: Stops all further operations.
EXAMPLE: Translation of a While construct
EXAMPLE: translation of an IF-THEN-ELSE construct
EXAMPLE: Translation of a For construct
Now that you learnt the basics, you might challenge yourself with more autonomous tasks:
CC BY-NC-SA 4.0 Credits