Guest User

80x86 Assembly div instruction with WeightedAvg

a guest
Feb 27th, 2014
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. COMMENT @
  2. Objective: Prompt and allocate DWORD(32 bits) size labels for Grade1-Grade4 and Weight1-Weight4.
  3. Solve for WeightedSum = G1*W1 + G2*W2 + G3*W3 + G4*W4
  4. Solve for SumOfWeights = W1 + W2 + W3 + W4
  5. Solve for WeightedAverage = (WeightedSum/SumOfWeights)
  6. Display WeightedAverage & WeightedSum
  7. @
  8.  
  9. ; Preprocessor directives
  10. .586 ; use the 80586 set of instructions
  11. .MODEL FLAT ; use the flat memory model (only 32 bit addresses, no segment:offset)
  12.  
  13. ; External source files
  14. INCLUDE io.h ; header file for input/output
  15.  
  16. ; Stack configuration
  17. .STACK 4096 ; allocate 4096 bytes for the stack
  18.  
  19. ; Named memory allocation and initialization
  20. .DATA
  21. Grade1 DWORD ?
  22. Grade2 DWORD ?
  23. Grade3 DWORD ?
  24. Grade4 DWORD ?
  25.  
  26. Weight1 DWORD ?
  27. Weight2 DWORD ?
  28. Weight3 DWORD ?
  29. Weight4 DWORD ?
  30.  
  31. WeightedSum DWORD ?
  32. WeightedAvg DWORD ?
  33. SumOfWeight DWORD ?
  34. WeightAvg BYTE 32 DUP (?)
  35. WeightSum BYTE 32 DUP (?)
  36.  
  37. ;Prompt the user for input reserving a single byte for each prompt
  38. prompt1 BYTE "Enter value for Grade1: ", 0
  39. prompt1b BYTE "Enter value for Weight1: ", 0
  40.  
  41. prompt2 BYTE "Enter value for Grade2: ", 0
  42. prompt2b BYTE "Enter value for Weight2: ", 0
  43.  
  44. prompt3 BYTE "Enter value for Grade3: ", 0
  45. prompt3b BYTE "Enter value for Weight3: ", 0
  46.  
  47. prompt4 BYTE "Enter value for Grade4: ", 0
  48. prompt4b BYTE "Enter value for Weight4: ", 0
  49.  
  50. ;Create a label of byte size and duplicate it x40. This will hold our Grades and Weights
  51. value BYTE 40 DUP (?)
  52.  
  53. ;Final prompt for output
  54. sumPrompt BYTE "Weighted Sum: ", 0
  55. avgPrompt BYTE "Weighted Average: ", 0
  56.  
  57.  
  58.  
  59. ; procedure definitions
  60. .CODE
  61. _MainProc PROC
  62.  
  63. ;Ask for Grade1 and Weight1 *REPEAT FOR GRADE1-4 & WEIGHT1-4*
  64. input prompt1, value, 40 ;Ask for Grade1 input
  65. atod value
  66. mov Grade1, eax
  67.  
  68. input prompt1b, value, 40 ;Ask for Weight1 input
  69. atod value
  70. mov Weight1, eax
  71.  
  72. input prompt2, value, 40 ;Ask for Grade2 input
  73. atod value
  74. mov Grade2, eax
  75. input prompt2b, value, 40 ;Ask for Weight2 input
  76. atod value
  77. mov Weight2, eax
  78.  
  79. input prompt3, value, 40 ;Ask for Grade3 Input
  80. atod value
  81. mov Grade3, eax
  82.  
  83. input prompt3b, value, 40 ;Ask for Weight3 input
  84. atod value
  85. mov Weight3, eax
  86.  
  87. input prompt4, value, 40 ;Ask for Grade4 Input
  88. atod value
  89. mov Grade4, eax
  90.  
  91. input prompt4b, value, 40 ;Ask for Weight4 input
  92. atod value
  93. mov Weight4, eax
  94.  
  95.  
  96. mov eax, 0
  97. mov ecx, Weight1
  98. ;WeightedSumPart1
  99. G1:
  100. add eax, Grade1
  101. dec ecx;
  102. jnz G1
  103.  
  104. ;WeightedSumPart2
  105. mov ebx, 0
  106. mov ecx, Weight2
  107. G2:
  108. add ebx, Grade2
  109. dec ecx
  110. jnz G2
  111. add eax, ebx
  112.  
  113. ;WeightedSumPart3
  114. mov ebx, 0
  115. mov ecx, Weight3
  116. G3:
  117. add ebx, Grade3
  118. dec ecx
  119.  
  120. jnz G3
  121.  
  122. add eax, ebx
  123. ;WeightedSumpart4
  124. mov ebx, 0
  125. mov ecx, Weight4
  126.  
  127. G4:
  128. add ebx, Grade4
  129. dec ecx
  130. jnz G4
  131. add eax, ebx
  132. mov WeightedSum, eax
  133. SumOfWeights:
  134. ;Remember: SumOfWeights(EBX) = Weight1 + Weight2 + Weight3 + Weight4
  135. mov ebx, 0
  136. add ebx, Weight1
  137. add ebx, Weight2
  138. add ebx, Weight3
  139. add ebx, Weight4
  140.  
  141.  
  142. mov SumOfWeight, ebx
  143.  
  144. dtoa WeightSum, WeightedSum
  145. WeightedAverage:
  146. mov eax, SumOfWeight
  147. div WeightSum
  148. dtoa WeightAvg, eax
  149. output sumPrompt, WeightSum
  150. output avgPrompt, WeightAvg
  151. mov eax, 0 ; exit with return code 0
  152.  
  153. ret
  154. _MainProc ENDP
  155.  
  156. END ; end of source code
  157.  
  158. COMMENT @
  159. Grades: Weights:
  160. 88 1
  161. 77 2
  162. 94 1
  163. 85 3
  164. SumOfWeight = 7
  165. WeightedSum = 591
  166. WeightedAvg = 81 ;Displays different
  167.  
  168. @
Add Comment
Please, Sign In to add comment