#=========================================================================
                                    # C-Minus Compilation to IA-32 Code
                                    # File: Test.s
#=========================================================================
                                    # -> allocating global var: x
          .comm     x, 4            # bss space for global
                                    # <- vardecl
#=========================================================================
                                    # processing function: main
          .text                     # 
.globl main
main:
          pushl	    %ebp            # Function prologue
  	  movl      %esp, %ebp
                                    # -> compound statement
          subl      $0, %esp        # allocate local var space
                                    # -> op
                                    # -> constant
          movl      $5, %eax        # load const
                                    # <- constant
          pushl     %eax            # op: push right
                                    # -> id
                                    # looking up id: x
          movl      $x, %eax        # load id address
                                    # <- id
          popl      %ebx            # op: pop right
          movl      %ebx, (%eax)    # assign: store value
                                    # <- op
                                    # -> function call: output
                                    # -> id
                                    # looking up id: x
          movl      $x, %eax        # load id address
          movl      (%eax), %eax    # load id value
                                    # <- id
          pushl     %eax            # store arg val
          call      output          # jump to function loc
                                    # <- call
          addl      $0, %esp        # deallocate local vars
                                    # <- compound statement
          leave                     # reset stack & frame ptrs
          ret                       # return to caller
                                    # <- fundecl
#=========================================================================
                                    # Conversion strings
          .section    .rodata       # 
.inStr:   .string   "%d"            
.outStr:  .string   "%d\n"          
#=========================================================================
                                    # Code for input routine
          .text                     # 
.globl input
input:
          pushl     %ebp            # Function prologue
          movl      %esp, %ebp      #
          subl      $4, %esp        # create slot for result
          pushl     %esp            # push slot's address
          pushl     $.inStr         # push conversion spec
          call      scanf           # read an integer
          movl      8(%esp), %eax   # move int to %eax
          leave                     # reset stack & frame ptrs
          ret                       # return to caller
#=========================================================================
                                    # Code for output routine
          .text                     # 
.globl output
output:
          pushl     %ebp            # Function prologue
          movl      %esp, %ebp      #
          pushl     8(%ebp)         # get int to print
          pushl     $.outStr        # push conversion spec
          call      printf          # invoke printf
          leave                     # reset stack & frame ptrs
          ret                       # return to caller
#=========================================================================
                                    # That's all Folks...
