![]() |
I called bricks the short pieces of code, that "COMPAS" generate as a part of operator. Compiler builds result machine code from the bricks. As it is clearly seen from logic of the structure, FOR construction consists from two blocks - before cycle body and after it. Part before cycle bodyStep 1 (extract value to begin cycle) Step 2 (extract value to end cycle) Step 3 (compare parameter's values and exit if incorrect) Step 4 (store initial value into cycle variable) Step 5 (save parameter address and last value) Part after cycle bodyStep 1 (restore parameter address and last value) Step 2 (compare cycle parameter with end value, exit if equal) Step 3 (make step) Step 4 (go to repeat cycle) And in the end example of full operator FOR Before cycle: step 1 (extract value to begin cycle) For this aim "COMPAS" uses standard bricks of assign operator, which extract value to AX. Then "COMPAS" always moves this initial value into BX by simple command
Back to: Before cycle: step 2 (extract value to end cycle) For this aim "COMPAS" also uses standard bricks of assign operator, which extract value to AX. Now the first value is stored in BX and the last one - in AX. Back to: Before cycle: step 3 (compare parameter's values and exit if incorrect) To compare the initial value and the last one "COMPAS" generates the following instructions:
As you can see from above, in both cases
the cycle will be immediately finished if result > 0.
Hence cycle with TO will be skipped if the initial
value is greater than last one (BX > AX) and in the opposite case
for DOWNTO cycle.
So incorrect cycles Back to: Before cycle: step 4 (store initial value into cycle variable) This action is realized by the following commands:
I want to accent that now the last parameter value is still keeping in AX and SI stores the memory address of cycle variable. On the next step they will be saved in the stack. Back to: Before cycle: step 5 (save parameter address and last value) This is the last step before cycle body. Because we know exactly that the cycle contents will change AX and SI values, we must save them somewhere. "COMPAS" uses stack for this purpose and always generates here the following code:
So the end parameter value (AX) and variable address (SI) is saved. After execution of the cycle body, these values will be restored by the first step. Back to: [here must be the cycle body]After cycle: step 1 (restore parameter address and last value) This is the first step after cycle body execution. It restores the parameter address value in SI register and the end parameter value in AX. Note that the order of reading from stack is always opposite to writing one.
Back to: After cycle: step 2 (compare cycle parameter with end value, exit if equal) To analyze the exit of the cycle, "COMPAS" generate constant code
Back to: After cycle: step 3 (make step) The task of this step is to calculate next value for TO cycle and previous for DOWNTO
You may be surprised, that this step contains 3 instructions (only one is needed for "E97"). But Intel processor has not inc [si] and dec [si] instructions... Back to: After cycle: step 4 (go to repeat cycle) This is just an unconditional brunch to repeat the cycle.
It's important to note, that this is jump to step 5 before cycle, but not to beginning of the cycle body. Back to: Example of full operator FOR Here you can find full results of cycle FOR compilation. FOR i:=9 DOWNTO 0 DO WRITELN(i);
Back to: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||