CSci281 - Assembly program #1

Write a function to evaluate a longword polynomial given longword value X, a coefficents table at address T, of length N, e.g., it should return the value T[0]*X^0 + T[1]*X^1 + T[2]*X^2 ... T[n-1]*X^(n-1).

The stack frame referenced by register A3 should be established by the caller. The value X should be passed in A3[-8], T should be passed in A3[-12], N should be passed in A3[-14], and the result returned in A3[-4].

The function should preserve all register values, and leave memory undisturbed (except for stack space used to preserve registers).

The following code fragment demonstrates how this function would be used:


;USES LINK
org 1000
X         DC.L     19
T         DC.L     3,5,2
N         DC.W     3
RESULT    DS.L     1

org 400
MAIN
        ; call function to calculate polynomial in X with N coefficients from table T
          LINK     A3,#-14     ;sets up SP
          MOVE.L   X,-8(A3)
          MOVE.L   #T,-12(A3)   ; Thanks to Isaac for catching an error here!
          MOVE.W   N,-14(A3)
          JSR      POLY       ;call subroutine POLY
          MOVE.L   -4(A3), RESULT
          UNLK     A3
          DC.W 0 ; this causes processor to halt

POLY      ; T[0] + T[1]X + T[2]X^2 + T[3]X^3...
          ; T[0] + X(T[1] + X(T[2] + X(T[3] + X(...))))
          ; write code to save registers
          ; write code to compute result
          ; write code to restore registers
          RTS
    

Note: The above example carefully uses a small value for X and a modestly sized table of coefficients so as not to push the limitations of the multiply instruction (which generates a 32-bit result from two 16-bit operands). We'll discuss multi-precision operations later in class.


CSci281/asgn0302.html was last edited by Randolph Bentson, on 2007/02/25T12:51:42-08:00

Valid HTML 4.01 Strict Valid CSS!