CSC448: Code Generation: Simplified Example [11/14] Previous pageContentsNext page

GCC's output can be simplified:

file:simplified-loop-windows.s [source]
00001: .global _main
00002: 
00003: .text
00004:         .align 4
00005: 
00006: _main:
00007:         pushl   %ebp
00008:         movl    %esp, %ebp
00009:         subl    $8, %esp
00010: 
00011:         movl    $3, -4(%ebp)
00012:         jmp     lab2
00013: lab1:
00014:         pushl   -4(%ebp)
00015:         pushl   $somenameforthestring
00016:         call    _printf
00017:         addl    $8, %esp
00018:         leal    -4(%ebp), %eax
00019:         decl    (%eax)
00020: lab2:
00021:         cmpl    $0, -4(%ebp)
00022:         jns     lab1
00023: 
00024:         movl    $0, %eax
00025:         leave
00026:         ret
00027: 
00028: 
00029: .section .rodata
00030: 
00031: somenameforthestring:
00032:         .string "%d\n"
00033: 
$ gcc -o loop simplified-loop.s
$ ./loop 
3
2
1
0
        

For Linux (note underscore changes):

file:simplified-loop-linux.s [source]
00001: .global main
00002: 
00003: .text
00004:         .align 4
00005: 
00006: main:
00007:         pushl   %ebp
00008:         movl    %esp, %ebp
00009:         subl    $8, %esp
00010: 
00011:         movl    $3, -4(%ebp)
00012:         jmp     lab2
00013: lab1:
00014:         pushl   -4(%ebp)
00015:         pushl   $somenameforthestring
00016:         call    printf
00017:         addl    $8, %esp
00018:         leal    -4(%ebp), %eax
00019:         decl    (%eax)
00020: lab2:
00021:         cmpl    $0, -4(%ebp)
00022:         jns     lab1
00023: 
00024:         movl    $0, %eax
00025:         leave
00026:         ret
00027: 
00028: 
00029: .section .rodata
00030: 
00031: somenameforthestring:
00032:         .string "%d\n"
00033: 

Previous pageContentsNext page