//----------------------------------------------------------------- // product.s // // Here is an implementation using assembly language for the // function that returns the result of multiplying a pair of // signed integers without using the x86 'imul' instruction. // // assemble using: $ as product.s -o product.o // // programmer: ALLAN CRUSE // written on: 06 MAR 2008 //----------------------------------------------------------------- .global product # make entry-point public .section .text product: # # prototype: int product( int x, int y ); # .equ y, 12 # symbolic offset for y .equ x, 8 # symbolic offset for x push %ebp # preserve caller's frame mov %esp, %ebp # setup local stack frame push %ecx # save working registers push %edx cmpl $0, x(%ebp) # check: x is negative? jge noneg # no, retain x,y values negl x(%ebp) # else negate x negl y(%ebp) # also negate y noneg: xor %eax, %eax # initialize our total mov x(%ebp), %ecx # setup loop-counter jecxz noadd # skip if count is zero nxadd: add y(%ebp), %eax # add y value to total loop nxadd # again until exhausted noadd: pop %edx # recover saved registers pop %ecx pop %ebp # recover frame pointer ret # return control to caller .end # nothing else to assemble