//----------------------------------------------------------------- // product.s // // Here is an implementation using assembly language for the // recursive function described in Question IV (on Exam II). // // assemble using: $ as product.s -o product.o // // programmer: ALLAN CRUSE // written on: 11 APR 2007 //----------------------------------------------------------------- # stackframe argument-offsets from register EBP .equ y, 12 .equ x, 8 .section .text #------------------------------------------------------------------ prod: # prototype: unsigned int prod( unsigned int x, unsigned int y ); .global prod # make function visible push %ebp # preserve caller's EBP mov %esp, %ebp # setup argument access mov y(%ebp), %eax # setup y-value in EAX # if ( y == 0 ), return y; cmp $0, %eax # base-case: y == zero? jne recur # no, then do recursion jmp ready # else return zero-value recur: # else return x + prod( x, y-1 ); dec %eax # compute y-1 in EAX push %eax # push argument #2 mov x(%ebp), %eax # setup x-value in EAX push %eax # push argument #1 call prod # compute prod( x, y-1 ) add $8, %esp # discard the arguments add x(%ebp), %eax # add x to prod(x, y-1 ) ready: pop %ebp # restore former EBP ret # and return to caller #------------------------------------------------------------------ .end