Пример 1. Вычисление факториала
Листинг П4.1. Входной файл
prog
if ((InpVar > 31) or InpVar<0) CompileTest:= 0
else
if (InpVar=0) CompileTest:= 1
else
begin
i:= InpVar;
Fact:= 1;
while (i<>-1) do
begin
j:= I-1;
Sum:= Fact;
while (not (j=0)) do
begin
Sum:= Sum + Fact;
j:= j-(-1);
end;
Fact:= Sum;
i:= i – 1;
end;
CompileTest:= Fact;
end;
end.
Листинг П4.2. Результирующий код
program MyCurs;
var
Fact,i,j,Sum: integer;
function CompileTest(InpVar: integer): integer; stdcall;
begin
asm
pushad {запоминаем регистры}
mov eax,InpVar
cmp eax,31 { 1: > (InpVar, 31) }
setg al
and eax,1
mov ebx,eax
mov eax,InpVar
cmp eax,0 { 2: < (InpVar, 0) }
setl al
and eax,1
or eax,ebx { 3: or (^1, ^2) }
jnz @F3 { 4: if (^3, ^7) }
jmp @M7
@F3:
xor eax,eax
mov Result,eax { 5::= (CompileTest, 0) }
jmp @M31 { 6: jmp (1, ^31) }
@M7:
mov eax,InpVar
cmp eax,0 { 7: = (InpVar, 0) }
sete al
and eax,1
jnz @F7 { 8: if (^7, ^11) }
jmp @M11
@F7:
xor eax,eax
inc eax
mov Result,eax { 9::= (CompileTest, 1) }
jmp @M31 { 10: jmp (1, ^31) }
@M11:
mov eax,InpVar
mov i,eax { 11::= (i, InpVar) }
xor eax,eax
inc eax
mov Fact,eax { 12::= (Fact, 1) }
@M13:
mov eax,i
cmp eax,1 { 13: <> (i, 1) }
setne al
and eax,1
jnz @F13 { 14: if (^13, ^30) }
jmp @M30
@F13:
mov eax,i
dec eax { 15: – (i, 1) }
mov j,eax { 16::= (j, ^15) }
mov eax,Fact
mov Sum,eax { 17::= (Sum, Fact) }
@M18:
mov eax,j
cmp eax,0 { 18: = (j, 0) }
sete al
and eax,1
not eax { 19: not (^18, 0) }
and eax,1
jnz @F19 { 20: if (^19, ^26) }
jmp @M26
@F19:
mov eax,Sum
add eax,Fact { 21: + (Sum, Fact) }
mov Sum,eax { 22::= (Sum, ^21) }
mov eax,j
dec eax { 23: – (j, 1) }
mov j,eax { 24::= (j, ^23) }
jmp @M18 { 25: jmp (1, ^18) }
@M26:
mov eax,Sum
mov Fact,eax { 26::= (Fact, Sum) }
mov eax,i
dec eax { 27: – (i, 1) }
mov i,eax { 28::= (i, ^27) }
jmp @M13 { 29: jmp (1, ^13) }
@M30:
mov eax,Fact
mov Result,eax { 30::= (CompileTest, Fact) }
@M31:
nop { 31: nop (0, 0) }
popad {восстанавливаем регистры}
end;
end;
var InpVar: integer;
begin
readln(InpVar);
writeln(CompileTest(InpVar));
readln;
end.
Пример 2. Иллюстрация работы функций оптимизации
Листинг П4.3. Входной файл
prog
D:= 0;
B:= 1;
C:= 1;
A:= C + InpVar;
D:= C+B+234;
C:= A + B + C;
D:= ((C) +(A+B)) – (InpVar + 1) + (A+B);
E:= (D – 22) – (A + B);
CompileTest:= 0;
if (a
and (b
a:=0 else a:=1;
if (InpVar > 0 or -1 <> InpVar)
while (a<1) do a:=a+(b+1)
else CompileTest:= InpVar+1;
end.
Листинг П4.4. Результирующий код без учета оптимизации
program MyCurs;
var
A,B,C,D,E: integer;
function CompileTest(InpVar: integer): integer; stdcall;
var
_Tmp0,_Tmp1: integer;
begin
asm
pushad {запоминаем регистры}
mov eax,0
mov D,eax { 1::= (D, 0) }
mov eax,1
mov B,eax { 2::= (B, 1) }
mov C,eax { 3::= (C, 1) }
add eax,InpVar { 4: + (C, InpVar) }
mov A,eax { 5::= (A, ^4) }
mov eax,C
add eax,B { 6: + (C, B) }
add eax,234 { 7: + (^6, 234) }
mov D,eax { 8::= (D, ^7) }
mov eax,A
add eax,B { 9: + (A, B) }
add eax,C { 10: + (^9, C) }
mov C,eax { 11::= (C, ^10) }
mov eax,A
add eax,B { 12: + (A, B) }
add eax,C { 13: + (C, ^12) }
mov ebx,eax
mov eax,InpVar
add eax,1 { 14: + (InpVar, 1) }
sub eax,ebx { 15: – (^13, ^14) }
neg eax
mov ebx,eax
mov eax,A
add eax,B { 16: + (A, B) }
add eax,ebx { 17: + (^15, ^16) }
mov D,eax { 18::= (D, ^17) }
sub eax,22 { 19: – (D, 22) }
mov ebx,eax
mov eax,A
add eax,B { 20: + (A, B) }
sub eax,ebx { 21: – (^19, ^20) }
neg eax
mov E,eax { 22::= (E, ^21) }
mov eax,0
mov Result,eax { 23::= (CompileTest, 0) }
mov eax,A
cmp eax,B { 24: < (A, B) }
setl al
and eax,1
mov ebx,eax
mov eax,A
cmp eax,C { 25: < (A, C) }
setl al
and eax,1
or eax,ebx { 26: or (^24, ^25) }
mov ebx,eax
mov eax,E
cmp eax,0 { 27: = (E, 0) }
sete al
and eax,1
mov ecx,eax
mov eax,D
cmp eax,0 { 28: = (D, 0) }
sete al
and eax,1
mov edx,eax
mov eax,B
cmp eax,C { 29: < (B, C) }
setl al
and eax,1
mov esi,eax
mov eax,A
cmp eax,C { 30: < (A, C) }
setl al
and eax,1
mov edi,eax
mov eax,C
cmp eax,E { 31: <> (C, E) }
setne al
and eax,1
mov _Tmp0,eax
mov eax,B
cmp eax,0 { 32: = (B, 0) }
sete al
and eax,1
not eax { 33: not (^32, 0) }
and eax,1
or eax,_Tmp0 { 34: or (^31, ^33) }
and eax,edi { 35: and (^30, ^34) }
or eax,esi { 36: or (^29, ^35) }
and eax,edx { 37: and (^28, ^36) }
xor eax,ecx { 38: xor (^27, ^37) }
xor eax,ebx { 39: xor (^26, ^38) }
jnz @F39 { 40: if (^39, ^43) }
jmp @M43
@F39:
mov eax,0
mov A,eax { 41::= (A, 0) }
jmp @M44 { 42: jmp (1, ^44) }
@M43:
mov eax,1
mov A,eax { 43::= (A, 1) }
@M44:
mov eax,InpVar
cmp eax,0 { 44: > (InpVar, 0) }
setg al
and eax,1
mov ebx,eax
mov eax,1
neg eax { 45: – (0, 1) }
cmp eax,InpVar { 46: <> (^45, InpVar) }
setne al
and eax,1
or eax,ebx { 47: or (^44, ^46) }
jnz @F47 { 48: if (^47, ^56) }
jmp @M56
@F47:
@M49:
mov eax,A
cmp eax,1 { 49: < (A, 1) }
setl al
and eax,1
jnz @F49 { 50: if (^49, ^55) }
jmp @M55
@F49:
mov eax,B
add eax,1 { 51: + (B, 1) }
add eax,A { 52: + (A, ^51) }
mov A,eax { 53::= (A, ^52) }
jmp @M49 { 54: jmp (1, ^49) }
@M55:
jmp @M58 { 55: jmp (1, ^58) }
@M56:
mov eax,InpVar
add eax,1 { 56: + (InpVar, 1) }
mov Result,eax { 57::= (CompileTest, ^56) }
@M58:
nop { 58: nop (0, 0) }
popad {восстанавливаем регистры}
end;
end;
var InpVar: integer;
begin
readln(InpVar);
writeln(CompileTest(InpVar));
readln;
end.
Листинг П4.5. Результирующий код с учетом оптимизации
program MyCurs;
var
A,B,C,D,E: integer;
function CompileTest(InpVar: integer): integer; stdcall;
begin
asm
pushad {запоминаем регистры}
xor eax,eax
mov D,eax { 1::= (D, 0) }
inc eax
mov B,eax { 2::= (B, 1) }
mov C,eax { 3::= (C, 1) }
add eax,InpVar { 4: + (C, InpVar) }
mov A,eax { 5::= (A, ^4) }
mov eax,236
mov D,eax { 6::= (D, 236) }
mov eax,A
add eax,B { 7: + (A, B) }
mov ebx,eax
add eax,C { 8: + (^7, C) }
mov C,eax { 9::= (C, ^8) }
add eax,ebx { 10: + (C, ^7) }
mov ecx,eax
mov eax,InpVar
inc eax { 11: + (InpVar, 1) }
sub eax,ecx { 12: – (^10, ^11) }
neg eax
add eax,ebx { 13: + (^12, ^7) }
mov D,eax { 14::= (D, ^13) }
mov eax,214
sub eax,ebx { 15: – (214, ^7) }
mov E,eax { 16::= (E, ^15) }
xor eax,eax
mov Result,eax { 17::= (CompileTest, 0) }
mov eax,A
cmp eax,B { 18: < (A, B) }
setl al
and eax,1
mov ebx,eax
mov eax,A
cmp eax,C { 19: < (A, C) }
setl al
and eax,1
mov edx,eax
or eax,ebx { 20: or (^18, ^19) }
mov ebx,eax
mov eax,E
cmp eax,0 { 21: = (E, 0) }
sete al
and eax,1
mov ecx,eax
mov eax,C
cmp eax,E { 22: <> (C, E) }
setne al
and eax,1
xor eax,eax
inc eax { 23: or (^22, 1) }
and eax,edx { 24: and (^19, ^23) }
{ 25: or (0, ^24) }
xor eax,eax { 26: and (0, ^25) }
xor eax,ecx { 27: xor (^21, ^26) }
xor eax,ebx { 28: xor (^20, ^27) }
jnz @F28 { 29: if (^28, ^32) }
jmp @M32
@F28:
xor eax,eax
mov A,eax { 30::= (A, 0) }
jmp @M33 { 31: jmp (1, ^33) }
@M32:
xor eax,eax
inc eax
mov A,eax { 32::= (A, 1) }
@M33:
mov eax,InpVar
cmp eax,0 { 33: > (InpVar, 0) }
setg al
and eax,1
mov ebx,eax
xor eax,eax
dec eax
cmp eax,InpVar { 34: <> (-1, InpVar) }
setne al
and eax,1
or eax,ebx { 35: or (^33, ^34) }
jnz @F35 { 36: if (^35, ^44) }
jmp @M44
@F35:
@M37:
mov eax,A
cmp eax,1 { 37: < (A, 1) }
setl al
and eax,1
jnz @F37 { 38: if (^37, ^43) }
jmp @M43
@F37:
mov eax,B
inc eax { 39: + (B, 1) }
add eax,A { 40: + (A, ^39) }
mov A,eax { 41::= (A, ^40) }
jmp @M37 { 42: jmp (1, ^37) }
@M43:
jmp @M46 { 43: jmp (1, ^46) }
@M44:
mov eax,InpVar
inc eax { 44: + (InpVar, 1) }
mov Result,eax { 45::= (CompileTest, ^44) }
@M46:
nop { 46: nop (0, 0) }
popad {восстанавливаем регистры}
end;
end;
var InpVar: integer;
begin
readln(InpVar);
writeln(CompileTest(InpVar));
readln;
end.