Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- COMMENT @
- Objective: Prompt and allocate DWORD(32 bits) size labels for Grade1-Grade4 and Weight1-Weight4.
- Solve for WeightedSum = G1*W1 + G2*W2 + G3*W3 + G4*W4
- Solve for SumOfWeights = W1 + W2 + W3 + W4
- Solve for WeightedAverage = (WeightedSum/SumOfWeights)
- Display WeightedAverage & WeightedSum
- @
- ; Preprocessor directives
- .586 ; use the 80586 set of instructions
- .MODEL FLAT ; use the flat memory model (only 32 bit addresses, no segment:offset)
- ; External source files
- INCLUDE io.h ; header file for input/output
- ; Stack configuration
- .STACK 4096 ; allocate 4096 bytes for the stack
- ; Named memory allocation and initialization
- .DATA
- Grade1 DWORD ?
- Grade2 DWORD ?
- Grade3 DWORD ?
- Grade4 DWORD ?
- Weight1 DWORD ?
- Weight2 DWORD ?
- Weight3 DWORD ?
- Weight4 DWORD ?
- WeightedSum DWORD ?
- WeightedAvg DWORD ?
- SumOfWeight DWORD ?
- WeightAvg BYTE 32 DUP (?)
- WeightSum BYTE 32 DUP (?)
- ;Prompt the user for input reserving a single byte for each prompt
- prompt1 BYTE "Enter value for Grade1: ", 0
- prompt1b BYTE "Enter value for Weight1: ", 0
- prompt2 BYTE "Enter value for Grade2: ", 0
- prompt2b BYTE "Enter value for Weight2: ", 0
- prompt3 BYTE "Enter value for Grade3: ", 0
- prompt3b BYTE "Enter value for Weight3: ", 0
- prompt4 BYTE "Enter value for Grade4: ", 0
- prompt4b BYTE "Enter value for Weight4: ", 0
- ;Create a label of byte size and duplicate it x40. This will hold our Grades and Weights
- value BYTE 40 DUP (?)
- ;Final prompt for output
- sumPrompt BYTE "Weighted Sum: ", 0
- avgPrompt BYTE "Weighted Average: ", 0
- ; procedure definitions
- .CODE
- _MainProc PROC
- ;Ask for Grade1 and Weight1 *REPEAT FOR GRADE1-4 & WEIGHT1-4*
- input prompt1, value, 40 ;Ask for Grade1 input
- atod value
- mov Grade1, eax
- input prompt1b, value, 40 ;Ask for Weight1 input
- atod value
- mov Weight1, eax
- input prompt2, value, 40 ;Ask for Grade2 input
- atod value
- mov Grade2, eax
- input prompt2b, value, 40 ;Ask for Weight2 input
- atod value
- mov Weight2, eax
- input prompt3, value, 40 ;Ask for Grade3 Input
- atod value
- mov Grade3, eax
- input prompt3b, value, 40 ;Ask for Weight3 input
- atod value
- mov Weight3, eax
- input prompt4, value, 40 ;Ask for Grade4 Input
- atod value
- mov Grade4, eax
- input prompt4b, value, 40 ;Ask for Weight4 input
- atod value
- mov Weight4, eax
- mov eax, 0
- mov ecx, Weight1
- ;WeightedSumPart1
- G1:
- add eax, Grade1
- dec ecx;
- jnz G1
- ;WeightedSumPart2
- mov ebx, 0
- mov ecx, Weight2
- G2:
- add ebx, Grade2
- dec ecx
- jnz G2
- add eax, ebx
- ;WeightedSumPart3
- mov ebx, 0
- mov ecx, Weight3
- G3:
- add ebx, Grade3
- dec ecx
- jnz G3
- add eax, ebx
- ;WeightedSumpart4
- mov ebx, 0
- mov ecx, Weight4
- G4:
- add ebx, Grade4
- dec ecx
- jnz G4
- add eax, ebx
- mov WeightedSum, eax
- SumOfWeights:
- ;Remember: SumOfWeights(EBX) = Weight1 + Weight2 + Weight3 + Weight4
- mov ebx, 0
- add ebx, Weight1
- add ebx, Weight2
- add ebx, Weight3
- add ebx, Weight4
- mov SumOfWeight, ebx
- dtoa WeightSum, WeightedSum
- WeightedAverage:
- mov eax, SumOfWeight
- div WeightSum
- dtoa WeightAvg, eax
- output sumPrompt, WeightSum
- output avgPrompt, WeightAvg
- mov eax, 0 ; exit with return code 0
- ret
- _MainProc ENDP
- END ; end of source code
- COMMENT @
- Grades: Weights:
- 88 1
- 77 2
- 94 1
- 85 3
- SumOfWeight = 7
- WeightedSum = 591
- WeightedAvg = 81 ;Displays different
- @
Add Comment
Please, Sign In to add comment