# Sample program to demonstrate linkage between bootstrap and # generated assembler code for cse minijava projects. HP 2/10 # Modified for x86-64, 11/11 # Modified to include get call, an call via table, MR 2/13 # # This code simulates a MiniJava "main" program and a sample # factorial function, but does not implement object creation or # ordinary methods, as would be done in the regular project. # # It also demonstrates the syntax for indirect calls via a table # of method labels, but note that this is not nearly a complete # implementation of object-oriented dynamic dispatch. It is just # meant to demostrate some of the syntax. # # This version works on linux. To make it work on Windows or # OS X, leading underscores need to be added to the external # names (_asm_main, _put) .text .globl asm_main # label for "main" program # main function - print 5, then print fact(5) asm_main: pushq %rbp # prologue - save frame ptr movq %rsp,%rbp subq $16,%rsp # space to store input value # rounded up to 16 byte multiple call get # Fetch and store input movq %rax,-8(%rbp) movq -8(%rbp),%rdi # System.out.println(input) call put movq -8(%rbp),%rdi # System.out.println(fact(input)) lea Fact$$,%rax # Real OO code would fetch table from # first word of receiver object call *8(%rax) # Call label at 8 byte offset from rax movq %rax,%rdi call put movq %rbp,%rsp # epilogue - return popq %rbp # (could use leave instead of movq/popq) ret # long fact(long n) - return fact(n) for n >= 1 fact: pushq %rbp # prologue movq %rsp,%rbp subq $16,%rsp # allocate stack frame for local copy of arg movq %rdi,-8(%rbp) # save n cmpq $1,%rdi # compare n to 1 jg fact_else # jump if n > 1 movq $1,%rax # return 1 jmp fact_exit fact_else: subq $1,%rdi # new argument is n-1 call fact # call fact(n-1) (result in rax) movq -8(%rbp),%rdx # reload n into rdx imulq %rdx,%rax # compute product in rax fact_exit: # return (result in rax here) movq %rbp,%rsp # epilogue popq %rbp ret .data Fact$$: .quad 0 # 64 bit 0 (e.g., null superclass) .quad fact # Pointer to fact method