HW5_S - 0306-250 Assembly Language Programming Fall 2009...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 0306-250 Assembly Language Programming Fall 2009 Homework Five Solution 1. Write an assembly language macro (called Total) according to these specifications: a. Accepts a pointer to an array of words in an index register; b. Accepts the number of array elements to total as a word value in another index register; c. Returns the sum of the array elements as a word in the accumulator register. d. Except for the obvious need to modify the register used to return the result, leaves all other registers unchanged after the execution of the expanded macro is complete. Total: MACRO ; Totals elements of word array starting ; in memory at address in index register \1. ; The number of elements in the array is ; specified in \2. ; Inputs: \1: Pointer to array ; \2: Number of elements in array ; Output: D ; Modify: CCR,D psh\1 psh\2 cp\2 #0 beq TotalEnd\@ ldd #0 TotalLoop\@: addd 2,\1+ dbne \2,TotalLoop\@ TotalEnd\@: pul\2 pul\1 ENDM Page 1 of 3 0306-250 20091 Homework Five Solution Page 2 of 3 2. Write assembly language code that uses a stack frame to implement the following C function and its call. Do not optimize the function, (i.e., write all intermediate results to variables on the stack frame as indicated in the C code). word MemWord; word IncMemW (word *Pointer) { word MemW; } MemW = *Pointer; MemW = MemW + 1; return MemW; MemWord = IncMemW (&MemWord); ;MemWord = IncMemW (&MemWord) ;Put current frame pointer and parameters on stack movw #MemWord,2,-sp ;Push pointer to array leas -2,sp ;Allocate space for return value ;Call total jsr IncMemW ;Process return movw 2,sp+,MemWord ;Get return value leas 2,sp ;Deallocate parameter space Done: bra * IncMemW: ;**************************************************************** ; Implements the following C code using a stack frame. ; word IncMemW (word *Pointer) { ; word MemW; ; ; MemW = *Pointer; ; MemW = MemW + 2; ; return MemW; ; } ; Create stack frame for IncMemW pshx ;Save old frame pointer tfr sp,x ;Establish Total's frame pointer leas -2,sp ;Allocate stack space local variable ; Preserve registers to be used pshd ; Function code ldd [6,x] ; *Pointer std -2,x ;MemW = addd #1 ; MemW + 1 std -2,x ;MemW = movw -2,x,4,x ;return MemW ; Restore registers used puld ; Deallocate stack frame for IncMemW tfr x,sp pulx rts ;**************************************************************** ; Variables MyData: SECTION 0306-250 20091 Homework Five Solution MemWord: DS.W END Page 3 of 3 1 Grading Criteria · · · 20 points: Overall presentation—includes macro and subroutine code specified with appropriate comments. 40 points: Macro (Total) - 15 points: Correct parameter usage - 15 points: Correct sum - 10 points: No side effects 40 points: Stack frame subroutine (IncMemW) - 15 points: Correct subroutine call setup and return completion/cleanup - 15 points: Correct subroutine implementation - 10 points: No side effects ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online