We look at output from a production compiler to see how compiling is done in one real example. The source Pascal code was entered on esus and compiled with the fpc compiler using the -al and -AAS flags. For example:
fpc -al -AAS myprog.pas
The source code was in myprog.pas and the translated assembly code was in myprog.s
program prog1(input, output);var a, b: Integer;begin {prog1} write('Please enter integer values for a and b: '); read(a, b); writeln; if a < b then a := b + 1 else b := a - 1 end.
.text .balign 16 # [prog2.pas] # [6] begin {prog1} .globl main .type main,@function main: .globl PASCALMAIN .type PASCALMAIN,@function PASCALMAIN: .globl program_init .type program_init,@function program_init: pushl %ebp movl %esp,%ebp subl $4,%esp call FPC_INITIALIZEUNITS .L3: # [7] write('Please enter integer values for a and b: '); leal U_SYSLINUX_OUTPUT,%edi movl %edi,-4(%ebp) pushl $.L6 pushl -4(%ebp) pushl $0 call FPC_WRITE_TEXT_SHORTSTR pushl -4(%ebp) call FPC_WRITE_END pushl $.L3 call FPC_IOCHECK.L7: # [8] read(a, b); leal U_SYSLINUX_INPUT,%edi movl %edi,-4(%ebp) pushl -4(%ebp) call FPC_READ_TEXT_SINT movw %ax,_A pushl -4(%ebp) call FPC_READ_TEXT_SINT movw %ax,_B pushl -4(%ebp) call FPC_READ_END pushl $.L7 call FPC_IOCHECK .L8: # [9] writeln; leal U_SYSLINUX_OUTPUT,%edi movl %edi,-4(%ebp) pushl -4(%ebp) call FPC_WRITELN_END pushl $.L8 call FPC_IOCHECK # [10] if a < b movswl _A,%eax movswl _B,%edx cmpl %eax,%edx jg .L9 jmp .L10 .L9: # [12] a := b + 1 movswl _B,%eax incl %eax movw %ax,_A jmp .L13 .L10: # [14] b := a -1 movswl _A,%eax decl %eax movw %ax,_B .L13: # [15] end. call FPC_DO_EXIT leave ret.Le0: .size main, .Le0 - main .balign 16.data # [16] .ascii "FPC 1.0.6 [2002/05/23] for i386 - LINUX"
for a := 1 to 10 do
b := b + a
# [10] for a := 1 to 10 do movw $1,_A .balign 4,144 .L11: # [11] b := b + a movswl _B,%eax movswl _A,%edx addl %eax,%edx movw %dx,_B cmpw $10,_A jge .L10 incw _A jmp .L11 .L10:
for i := 1 to n do
a := a + 1
# [10] for i := 1 to n do movw _N,%di movw %di,-4(%ebp) movw $1,_I movw _I,%ax cmpw -4(%ebp),%ax jg .L10 .balign 4,144 .L11: # [11] a := a + 1 movswl _A,%eax incl %eax movw %ax,_A movw _I,%ax cmpw -4(%ebp),%ax jge .L10 incw _I jmp .L11 .L10:
case choice of
1:
a := 3;
2:
a := 5;
3:
a := 10;
4, 7:
a := 100;
6, 10:
a := 1000
end;
# [10] case choice of movw _CHOICE,%ax cmpw $1,%ax jl .L22 decw %ax jz .L3 decw %ax jz .L5 decw %ax jz .L7 decw %ax jz .L9 subw $2,%ax jz .L12 decw %ax jz .L9 subw $3,%ax jz .L12 jmp .L22 .L12: # [20] a := 1000 movw $1000,_A jmp .L21 .L9: # [18] a := 100; movw $100,_A jmp .L21 .L7: # [16] a := 10; movw $10,_A jmp .L21 .L5: # [14] a := 5; movw $5,_A jmp .L21 .L3: # [12] a := 3; movw $3,_A jmp .L21 .L22: .L21: .L33: