# Generated by c.py # Atul Varma (Spring 2004) .text # BEGIN FUNCTION: fib() ------------------------------------------------ # # Function type: function(int)->int _fib: pushl %ebp # Save old frame pointer movl %esp, %ebp # Set new frame pointer subl $4, %esp # Allocate space for local+temp vars leal _fib_count, %edx # Get address of fib_count addl $1, (%edx) # Perform assignment '+=' movl (%edx), %ecx # Copy assignment result to register # IF statment - begin movl 8(%ebp), %ecx # Get value of i cmpl $1, %ecx # Compare %ecx to $1 sete %cl # Perform '==' movzbl %cl, %ecx # Zero-extend the boolean result testl %ecx, %ecx # Test the result jz .L2_else # If result is zero, jump to else clause # IF statment - THEN clause - begin movl $1, %eax # Set return value jmp .L0_function_end # Exit function # IF statment - THEN clause - end jmp .L1_done # IF statment - ELSE clause - begin .L2_else: # IF statment - begin movl 8(%ebp), %ecx # Get value of i cmpl $0, %ecx # Compare %ecx to $0 sete %cl # Perform '==' movzbl %cl, %ecx # Zero-extend the boolean result testl %ecx, %ecx # Test the result jz .L4_else # If result is zero, jump to else clause # IF statment - THEN clause - begin movl $0, %eax # Set return value jmp .L0_function_end # Exit function # IF statment - THEN clause - end jmp .L3_done # IF statment - ELSE clause - begin .L4_else: # FUNCTION CALL to fib() - begin movl 8(%ebp), %ecx # Get value of i subl $1, %ecx # Perform '-' pushl %ecx # Push arg 1 call _fib # Call fib() addl $4, %esp # Deallocate argument stack # FUNCTION CALL to fib() - end # FUNCTION CALL to fib() - begin movl %eax, -4(%ebp) # Save caller-save register to temp movl 8(%ebp), %eax # Get value of i subl $2, %eax # Perform '-' pushl %eax # Push arg 1 call _fib # Call fib() addl $4, %esp # Deallocate argument stack # FUNCTION CALL to fib() - end movl -4(%ebp), %ecx # Stack machine: copy temp to register addl %eax, %ecx # Perform '+' movl %ecx, %eax # Set return value jmp .L0_function_end # Exit function # IF statment - ELSE clause - end .L3_done: # IF statment - end # IF statment - ELSE clause - end .L1_done: # IF statment - end .L0_function_end: movl %ebp, %esp # Deallocate stack frame popl %ebp # Restore old stack frame ret # END FUNCTION: fib() -------------------------------------------------- # BEGIN FUNCTION: show_fib() ------------------------------------------- # # Function type: function(int)->int _show_fib: pushl %ebp # Save old frame pointer movl %esp, %ebp # Set new frame pointer # FUNCTION CALL to printf() - begin # FUNCTION CALL to fib() - begin movl 8(%ebp), %edx # Get value of i pushl %edx # Push arg 1 call _fib # Call fib() addl $4, %esp # Deallocate argument stack # FUNCTION CALL to fib() - end pushl %eax # Push arg 3 movl 8(%ebp), %eax # Get value of i pushl %eax # Push arg 2 movl $LC0, %eax # Get addr of string literal 'fib(%d)...' pushl %eax # Push arg 1 call _printf # Call printf() addl $12, %esp # Deallocate argument stack # FUNCTION CALL to printf() - end movl $0, %eax # Set return value jmp .L5_function_end # Exit function .L5_function_end: movl %ebp, %esp # Deallocate stack frame popl %ebp # Restore old stack frame ret # END FUNCTION: show_fib() --------------------------------------------- # BEGIN FUNCTION: set_a() ---------------------------------------------- # # Function type: function(pointer(char))->int _set_a: pushl %ebp # Save old frame pointer movl %esp, %ebp # Set new frame pointer movl 8(%ebp), %edx # Get value of c # (Getting pointer target addr via '*') movb $97, (%edx) # Perform assignment '=' movzbl (%edx), %ecx # Copy assignment result to register movl $0, %eax # Set return value jmp .L6_function_end # Exit function .L6_function_end: movl %ebp, %esp # Deallocate stack frame popl %ebp # Restore old stack frame ret # END FUNCTION: set_a() ------------------------------------------------ # BEGIN FUNCTION: get_literal() ---------------------------------------- # # Function type: function()->pointer(char) _get_literal: pushl %ebp # Save old frame pointer movl %esp, %ebp # Set new frame pointer movl $LC1, %edx # Get addr of string literal 'blah\n' movl %edx, %eax # Set return value jmp .L7_function_end # Exit function .L7_function_end: movl %ebp, %esp # Deallocate stack frame popl %ebp # Restore old stack frame ret # END FUNCTION: get_literal() ------------------------------------------ # BEGIN FUNCTION: main() ----------------------------------------------- # # Function type: function(int,pointer(pointer(char)))->int .global _main _main: pushl %ebp # Save old frame pointer movl %esp, %ebp # Set new frame pointer subl $24, %esp # Allocate space for local+temp vars leal -5(%ebp), %edx # Get address of c movb $104, (%edx) # Perform assignment '=' movzbl (%edx), %ecx # Copy assignment result to register leal _fib_count, %ecx # Get address of fib_count leal _stuff_count, %edx # Get address of stuff_count movl $0, (%edx) # Perform assignment '=' movl (%edx), %eax # Copy assignment result to register movl %eax, (%ecx) # Perform assignment '=' movl (%ecx), %edx # Copy assignment result to register # FUNCTION CALL to printf() - begin movl 12(%ebp), %edx # Get value of argv movl (%edx), %ecx # Pointer dereference pushl %ecx # Push arg 2 movl $LC2, %ecx # Get addr of string literal 'My exec...' pushl %ecx # Push arg 1 call _printf # Call printf() addl $8, %esp # Deallocate argument stack # FUNCTION CALL to printf() - end # FOR loop - begin leal -4(%ebp), %eax # Get address of i movl $0, (%eax) # Perform assignment '=' movl (%eax), %ecx # Copy assignment result to register .L9_test: movl -4(%ebp), %ecx # Get value of i movl 8(%ebp), %eax # Get value of argc cmpl %eax, %ecx # Compare %ecx to %eax setl %cl # Perform '<' movzbl %cl, %ecx # Zero-extend the boolean result testl %ecx, %ecx # Test the result jz .L10_done # If result is zero, leave for loop # FUNCTION CALL to printf() - begin movl 12(%ebp), %ecx # Get value of argv movl -4(%ebp), %eax # Get value of i movl (%ecx,%eax,4), %edx # Pointer array index dereference movl $0, %ecx # Load numeric constant 0 movzbl (%edx,%ecx,1), %eax # Pointer array index dereference pushl %eax # Push arg 5 movl -4(%ebp), %eax # Get value of i pushl %eax # Push arg 4 movl 12(%ebp), %eax # Get value of argv movl -4(%ebp), %edx # Get value of i movl (%eax,%edx,4), %ecx # Pointer array index dereference pushl %ecx # Push arg 3 movl -4(%ebp), %ecx # Get value of i pushl %ecx # Push arg 2 movl $LC3, %ecx # Get addr of string literal ' argv[...' pushl %ecx # Push arg 1 call _printf # Call printf() addl $20, %esp # Deallocate argument stack # FUNCTION CALL to printf() - end # FUNCTION CALL to increment_stuff_count() - begin call _increment_stuff_count # Call increment_stuff_count() # FUNCTION CALL to increment_stuff_count() - end leal -4(%ebp), %eax # Get address of i addl $1, (%eax) # Perform assignment '+=' movl (%eax), %ecx # Copy assignment result to register jmp .L9_test # Jump to start of for loop .L10_done: # FOR loop - end leal -4(%ebp), %ecx # Get address of i movl $0, (%ecx) # Perform assignment '=' movl (%ecx), %eax # Copy assignment result to register # WHILE loop - begin .L11_test: movl $1, %eax # Load numeric constant 1 testl %eax, %eax # Test the result jz .L12_done # If result is zero, leave while loop # FUNCTION CALL to show_fib() - begin movl -4(%ebp), %eax # Get value of i pushl %eax # Push arg 1 call _show_fib # Call show_fib() addl $4, %esp # Deallocate argument stack # FUNCTION CALL to show_fib() - end leal -4(%ebp), %eax # Get address of i addl $1, (%eax) # Perform assignment '+=' movl (%eax), %ecx # Copy assignment result to register # IF statment - begin movl -4(%ebp), %ecx # Get value of i cmpl $5, %ecx # Compare %ecx to $5 setg %cl # Perform '>' movzbl %cl, %ecx # Zero-extend the boolean result testl %ecx, %ecx # Test the result jz .L14_else # If result is zero, jump to else clause # IF statment - THEN clause - begin jmp .L12_done # Loop: break statement # IF statment - THEN clause - end jmp .L13_done # IF statment - ELSE clause - begin .L14_else: jmp .L11_test # Loop: continue statement # IF statment - ELSE clause - end .L13_done: # IF statment - end jmp .L11_test # Jump to start of while loop .L12_done: # WHILE loop - end leal _stuff_count, %ecx # Get address of stuff_count movl _stuff_count, %eax # Get value of stuff_count imull $2, %eax # Perform '*' movl %eax, (%ecx) # Perform assignment '=' movl (%ecx), %edx # Copy assignment result to register # FUNCTION CALL to printf() - begin movl _fib_count, %edx # Get value of fib_count pushl %edx # Push arg 2 movl $LC4, %edx # Get addr of string literal 'fib_cou...' pushl %edx # Push arg 1 call _printf # Call printf() addl $8, %esp # Deallocate argument stack # FUNCTION CALL to printf() - end # FUNCTION CALL to printf() - begin movl _stuff_count, %eax # Get value of stuff_count pushl %eax # Push arg 2 movl $LC5, %eax # Get addr of string literal 'stuff_c...' pushl %eax # Push arg 1 call _printf # Call printf() addl $8, %esp # Deallocate argument stack # FUNCTION CALL to printf() - end # FUNCTION CALL to printf() - begin movzbl -5(%ebp), %eax # Get value of c pushl %eax # Push arg 2 movl $LC6, %eax # Get addr of string literal 'before ...' pushl %eax # Push arg 1 call _printf # Call printf() addl $8, %esp # Deallocate argument stack # FUNCTION CALL to printf() - end # FUNCTION CALL to set_a() - begin leal -5(%ebp), %eax # Get address of c # (Address-of operator '&' used here) pushl %eax # Push arg 1 call _set_a # Call set_a() addl $4, %esp # Deallocate argument stack # FUNCTION CALL to set_a() - end leal -12(%ebp), %eax # Get address of a movl $-1, (%eax) # Perform assignment '=' movl (%eax), %edx # Copy assignment result to register leal -17(%ebp), %edx # Get address of b movl -12(%ebp), %eax # Get value of a movzbl %al, %eax # Implicit cast: int -> char movb %al, (%edx) # Perform assignment '=' movzbl (%edx), %ecx # Copy assignment result to register leal -16(%ebp), %ecx # Get address of c movzbl -17(%ebp), %edx # Get value of b movl %edx, (%ecx) # Perform assignment '=' movl (%ecx), %eax # Copy assignment result to register # FUNCTION CALL to printf() - begin movl -12(%ebp), %eax # Get value of a pushl %eax # Push arg 2 movl $LC7, %eax # Get addr of string literal ' a = %...' pushl %eax # Push arg 1 call _printf # Call printf() addl $8, %esp # Deallocate argument stack # FUNCTION CALL to printf() - end # FUNCTION CALL to printf() - begin movzbl -17(%ebp), %eax # Get value of b pushl %eax # Push arg 2 movl $LC8, %eax # Get addr of string literal ' b = %...' pushl %eax # Push arg 1 call _printf # Call printf() addl $8, %esp # Deallocate argument stack # FUNCTION CALL to printf() - end # FUNCTION CALL to printf() - begin movl -16(%ebp), %eax # Get value of c pushl %eax # Push arg 2 movl $LC9, %eax # Get addr of string literal ' c = %...' pushl %eax # Push arg 1 call _printf # Call printf() addl $8, %esp # Deallocate argument stack # FUNCTION CALL to printf() - end # FUNCTION CALL to printf() - begin movzbl -5(%ebp), %eax # Get value of c pushl %eax # Push arg 2 movl $LC10, %eax # Get addr of string literal 'after s...' pushl %eax # Push arg 1 call _printf # Call printf() addl $8, %esp # Deallocate argument stack # FUNCTION CALL to printf() - end # FUNCTION CALL to printf() - begin # FUNCTION CALL to get_literal() - begin call _get_literal # Call get_literal() # FUNCTION CALL to get_literal() - end pushl %eax # Push arg 2 movl $LC11, %eax # Get addr of string literal 'get_lit...' pushl %eax # Push arg 1 call _printf # Call printf() addl $8, %esp # Deallocate argument stack # FUNCTION CALL to printf() - end # FUNCTION CALL to printf() - begin # FUNCTION CALL to get_literal() - begin call _get_literal # Call get_literal() # FUNCTION CALL to get_literal() - end movl $3, %ecx # Load numeric constant 3 movzbl (%eax,%ecx,1), %edx # Pointer array index dereference pushl %edx # Push arg 2 movl $LC12, %edx # Get addr of string literal 'get_lit...' pushl %edx # Push arg 1 call _printf # Call printf() addl $8, %esp # Deallocate argument stack # FUNCTION CALL to printf() - end leal -12(%ebp), %eax # Get address of c # FUNCTION CALL to malloc() - begin movl %eax, -24(%ebp) # Save caller-save register to temp pushl $30 # Push arg 1 call _malloc # Call malloc() addl $4, %esp # Deallocate argument stack # FUNCTION CALL to malloc() - end movl -24(%ebp), %edx # Stack machine: copy temp to register movl %eax, (%edx) # Perform assignment '=' movl (%edx), %ecx # Copy assignment result to register movl -12(%ebp), %ecx # Get value of c movl $0, %edx # Load numeric constant 0 leal (%ecx,%edx,1), %eax # Load addr of pointer array index movb $104, (%eax) # Perform assignment '=' movzbl (%eax), %ecx # Copy assignment result to register movl -12(%ebp), %ecx # Get value of c movl $1, %eax # Load numeric constant 1 leal (%ecx,%eax,1), %edx # Load addr of pointer array index movb $105, (%edx) # Perform assignment '=' movzbl (%edx), %ecx # Copy assignment result to register movl -12(%ebp), %ecx # Get value of c movl $2, %edx # Load numeric constant 2 leal (%ecx,%edx,1), %eax # Load addr of pointer array index movb $0, (%eax) # Perform assignment '=' movzbl (%eax), %ecx # Copy assignment result to register # FUNCTION CALL to printf() - begin movl -12(%ebp), %ecx # Get value of c pushl %ecx # Push arg 2 movl $LC13, %ecx # Get addr of string literal 'array-b...' pushl %ecx # Push arg 1 call _printf # Call printf() addl $8, %esp # Deallocate argument stack # FUNCTION CALL to printf() - end # FUNCTION CALL to free() - begin movl -12(%ebp), %eax # Get value of c pushl %eax # Push arg 1 call _free # Call free() addl $4, %esp # Deallocate argument stack # FUNCTION CALL to free() - end movl $0, %eax # Set return value jmp .L8_function_end # Exit function .L8_function_end: movl %ebp, %esp # Deallocate stack frame popl %ebp # Restore old stack frame ret # END FUNCTION: main() ------------------------------------------------- .global_vars: .comm _fib_count,16 LC0: .ascii "fib(%d) is %d.\12\0" LC1: .ascii "blah\12\0" LC2: .ascii "My executable name is %s.\12\0" LC3: .ascii " argv[%d] is: %s argv[%d][0] is: %c\12\0" LC4: .ascii "fib_count is %d.\12\0" LC5: .ascii "stuff_count is %d.\12\0" LC6: .ascii "before set_a(&c), c == '%c'\12\0" LC7: .ascii " a = %d\12\0" LC8: .ascii " b = %d\12\0" LC9: .ascii " c = %d\12\0" LC10: .ascii "after set_a(&c), c == '%c'\12\0" LC11: .ascii "get_literal() = %s\12\0" LC12: .ascii "get_literal()[3] = %c\12\0" LC13: .ascii "array-built string is: %s\12\0"