Use the posted C++ program: Do not modify or change the C++ program. Write in as
ID: 670811 • Letter: U
Question
Use the posted C++ program:
Do not modify or change the C++ program.
Write in assembly language, created in a in a separate file named labxx.asm, where xx is the lab number (01, 02, 03…) the external assembler function AverageFunc.
The first parameter is the number of elements in the array and the address of the array is the second parameter.
For this assignment:
The stack must managed directly by the assembler code
No masm directives can be used to manage the stack
The masm function is called by the C++ program using the decl C style stack model
The masm function must do all its responsibilities for a decl C style stack mdel
The stack parameters must be accessed from the stack using [BP + stack offset] addressing style.
The function will determine the average of the values in the array and return the average rounded to the nearest whole number.
So, if the fractional part of the result is >= 0.5, the result is rounded to the next integer number.
If not, then the integer part of the number is used as the result.
.586 ;Target processor, use Pentium class machines
.MODEL FLAT, C ;Use the flat memory model. Use C calling and return conventions
.STACK ;Define a stack segment of 1KB (Not required for this example)
.DATA ;Create a near data segment. Local variables are declared here (Not required for this example)
.CODE ;Indicates the start of a code segment.
AverageFunc PROC
; Stack parameter offests
NoOfElemenstsDW EQU 08
ArrayAddrStartPtr EQU 12
ElementSizeBytes EQU 04
push ebp ;save curent stack frame pointer
mov ebp, esp ;set current stack frame pointer
push esi ;perserve registers for calling program
push ecx
;get parameters from stack using stack frame
;for NoOfElemenstsDW into ecx and ArrayAddrStartPtr into esi
;process the array sum
;clear eax summer
arrayLoop:
; sum up into eax summer register
; update esi to ext element
loop arrayLoop
push edx ;perserve registers for calling program
push ebx
;take average and calcuate rounding
;clear upper dividend edx
;set divisor into ebx
;divide edx:eax / ebx
;multiply remainder edx by 2
;if 2 x edx remainder is greater than divisor ebx
jbe NoRound
; round quotient eax up to next integer
NoRound: ;endif
pop ebx ; restore used registers
pop edx
pop ecx
pop esi
pop ebp ;restore calling bp frame
ret
AverageFunc ENDP
END
Explanation / Answer
INCLUDE Irvine32.inc
.DATA
number1 DQ 1.365 ;loading numbers
number2 DQ 1.258
hundred DD 100 ;upto two decimal points
.CODE
_main PROC
fld number1
fimul hundred
frndint
fidiv hundred
call ShowFPUStack ;after rounding off, storing into stack
fld number2
fimul hundred
frndint
fidiv hundred
call ShowFPUStack ;after rounding off, storing into stack
invoke ExitProcess, 0
_main ENDP
END _main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.