[ Pobierz całość w formacie PDF ]
cmp eax, 10
jna ?3_false
mov ebx, 0
jmp ?2_endif
?3_false:
cmp eax, 5
jne ?4_false
mov ebx, 2
jmp ?2_endif
?4_false:
mov ebx, 3
?2_endif:
This code generation algorithm generalizes to any number of ELSEIF clauses. If you need to see an
example of an IF statement with more than two ELSEIF clauses, feel free to run a short example through the
HLA compiler to see the result.
In addition to processing boolean expressions, the HLA IF statement supports a hybrid syntax that lets
you combine the structured nature of the IF statement with the unstructured nature of typical assembly lan-
guage control flow. The hybrid form gives you almost complete control over the code generation process
without completely sacrificing the readability of an IF statement. The following is a typical example of this
form of the IF statement:
Beta Draft - Do not distribute © 2001, By Randall Hyde Page 1557
Appendix L
if
(#{
cmp( eax, 10 );
jna false;
}#) then
mov( 0, eax );
endif;
; The above generates the following assembly code:
cmp eax, 10
jna ?2_false
?2_true:
mov eax, 0
?2_false:
Of course, the hybrid IF statement fully supports ELSE and ELSEIF clauses (in fact, the IF and ELSEIF
clauses can have a potpourri of hybrid or traditional boolean expression forms). The hybrid forms, since
they let you specify the sequence of instructions to compile, put the issue of efficiency squarely in your lap.
About the only contribution that HLA makes to the inefficiency of the program is the insertion of a JMP
instruction to skip over ELSEIF and ELSE clauses.
Although the hybrid form of the IF statement lets you write very efficient code that is more readable
than the traditional compare and jump sequence, you should keep in mind that the hybrid form is defi-
nitely more difficult to read and comprehend than the IF statement with boolean expressions. Therefore, if
the HLA compiler generates reasonable code with a boolean expression then by all means use the boolean
expression form; it will probably be easier to read.
L.7 The While Statement
The only difference between an IF statement and a WHILE loop is a single JMP instruction. Of course,
with an IF and a JMP you can simulate most control structures, the WHILE loop is probably the most typical
example of this. The typical translation from WHILE to IF/JMP takes the following form:
while( expr ) do
endwhile;
// The above translates to:
label:
if( expr ) then
jmp label;
endif;
Page 1558 © 2001, By Randall Hyde Beta Draft - Do not distribute
HLA Code Generation for HLL Statements
Experienced assembly language programmers know that there is a slightly more efficient implementa-
tion if it is likely that the boolean expression is true the first time the program encounters the loop. That
translation takes the following form:
jmp testlabel;
label:
testlabel:
JT( expr ) label; // Note: JT means jump if expression is true.
This form contains exactly the same number of instructions as the previous translation. The difference
is that a JMP instruction was moved out of the loop so that it executes only once (rather than on each itera-
tion of the loop). So this is slightly more efficient than the previous translation. HLA uses this conversion
algorithm for WHILE loops with standard boolean expressions.
L.8 repeat..until
L.9 for..endfor
L.10 forever..endfor
L.11 break, breakif
L.12 continue, continueif
L.13 begin..end, exit, exitif
L.14 foreach..endfor
L.15 try..unprotect..exception..anyexception..endtry, raise
Editorial Note: This document is a work in progress. At some future date I will finish the sections above.
Until then, use the HLA -s compiler option to emit MASM code and study the MASM output as described
in this appendix.
Beta Draft - Do not distribute © 2001, By Randall Hyde Page 1559
Appendix L
Page 1560 © 2001, By Randall Hyde Beta Draft - Do not distribute [ Pobierz caÅ‚ość w formacie PDF ]
zanotowane.pl doc.pisz.pl pdf.pisz.pl chiara76.opx.pl
cmp eax, 10
jna ?3_false
mov ebx, 0
jmp ?2_endif
?3_false:
cmp eax, 5
jne ?4_false
mov ebx, 2
jmp ?2_endif
?4_false:
mov ebx, 3
?2_endif:
This code generation algorithm generalizes to any number of ELSEIF clauses. If you need to see an
example of an IF statement with more than two ELSEIF clauses, feel free to run a short example through the
HLA compiler to see the result.
In addition to processing boolean expressions, the HLA IF statement supports a hybrid syntax that lets
you combine the structured nature of the IF statement with the unstructured nature of typical assembly lan-
guage control flow. The hybrid form gives you almost complete control over the code generation process
without completely sacrificing the readability of an IF statement. The following is a typical example of this
form of the IF statement:
Beta Draft - Do not distribute © 2001, By Randall Hyde Page 1557
Appendix L
if
(#{
cmp( eax, 10 );
jna false;
}#) then
mov( 0, eax );
endif;
; The above generates the following assembly code:
cmp eax, 10
jna ?2_false
?2_true:
mov eax, 0
?2_false:
Of course, the hybrid IF statement fully supports ELSE and ELSEIF clauses (in fact, the IF and ELSEIF
clauses can have a potpourri of hybrid or traditional boolean expression forms). The hybrid forms, since
they let you specify the sequence of instructions to compile, put the issue of efficiency squarely in your lap.
About the only contribution that HLA makes to the inefficiency of the program is the insertion of a JMP
instruction to skip over ELSEIF and ELSE clauses.
Although the hybrid form of the IF statement lets you write very efficient code that is more readable
than the traditional compare and jump sequence, you should keep in mind that the hybrid form is defi-
nitely more difficult to read and comprehend than the IF statement with boolean expressions. Therefore, if
the HLA compiler generates reasonable code with a boolean expression then by all means use the boolean
expression form; it will probably be easier to read.
L.7 The While Statement
The only difference between an IF statement and a WHILE loop is a single JMP instruction. Of course,
with an IF and a JMP you can simulate most control structures, the WHILE loop is probably the most typical
example of this. The typical translation from WHILE to IF/JMP takes the following form:
while( expr ) do
endwhile;
// The above translates to:
label:
if( expr ) then
jmp label;
endif;
Page 1558 © 2001, By Randall Hyde Beta Draft - Do not distribute
HLA Code Generation for HLL Statements
Experienced assembly language programmers know that there is a slightly more efficient implementa-
tion if it is likely that the boolean expression is true the first time the program encounters the loop. That
translation takes the following form:
jmp testlabel;
label:
testlabel:
JT( expr ) label; // Note: JT means jump if expression is true.
This form contains exactly the same number of instructions as the previous translation. The difference
is that a JMP instruction was moved out of the loop so that it executes only once (rather than on each itera-
tion of the loop). So this is slightly more efficient than the previous translation. HLA uses this conversion
algorithm for WHILE loops with standard boolean expressions.
L.8 repeat..until
L.9 for..endfor
L.10 forever..endfor
L.11 break, breakif
L.12 continue, continueif
L.13 begin..end, exit, exitif
L.14 foreach..endfor
L.15 try..unprotect..exception..anyexception..endtry, raise
Editorial Note: This document is a work in progress. At some future date I will finish the sections above.
Until then, use the HLA -s compiler option to emit MASM code and study the MASM output as described
in this appendix.
Beta Draft - Do not distribute © 2001, By Randall Hyde Page 1559
Appendix L
Page 1560 © 2001, By Randall Hyde Beta Draft - Do not distribute [ Pobierz caÅ‚ość w formacie PDF ]