fork download
  1.  
  2. section .data
  3. nline db 10,10
  4. nline_len: equ $-nline
  5.  
  6. msg db 10,"MIL assignment 13 : 8087 program for mean, SD, & Variance"
  7. db 10,"---------------------------------------------",10
  8. msg_len: equ $-msg
  9.  
  10. mmsg db 10,"CALCULATED MEAN : "
  11. mmsg_len equ $-mmsg
  12.  
  13. sdmsg db 10,"CALCULATED STANDARD DEVIATION : "
  14. sdmsg_len equ $-sdmsg
  15.  
  16. vmsg db 10,"CALCULATED VARIANCE : "
  17. vmsg_len equ $-vmsg
  18.  
  19.  
  20. array dd 102.56, 198.21, 100.67, 230.78, 67.93
  21. count dw 05
  22.  
  23. dpoint db '.'
  24. hdec dq 100
  25. ;---------------------------------------------------------------
  26. section .bss
  27. char_ans resB 2
  28. resbuff resT 1
  29.  
  30. mean resD 1
  31. variance resD 1
  32. ;---------------------------------------------------------------
  33. ;macros as per 64-bit convensions
  34.  
  35. %macro print 2
  36. mov rax,1 ; Function 1 - write
  37. mov rdi,1 ; To stdout
  38. mov rsi,%1 ; String address
  39. mov rdx,%2 ; String size
  40. syscall ; invoke operating system to WRITE
  41. %endmacro
  42.  
  43. %macro read 2
  44. mov rax,0 ; Function 0 - Read
  45. mov rdi,0 ; from stdin
  46. mov rsi,%1 ; buffer address
  47. mov rdx,%2 ; buffer size
  48. syscall ; invoke operating system to READ
  49. %endmacro
  50.  
  51. %macro exit 0
  52. print nline, nline_len
  53. mov rax, 60 ; system call 60 is exit
  54. xor rdi, rdi ; we want return code 0
  55. syscall ; invoke operating system to exit
  56. %endmacro
  57. ;---------------------------------------------------------------
  58.  
  59. section .text
  60. global _start
  61. _start:
  62.  
  63. print msg, msg_len
  64.  
  65. finit ; initialize coprocessor
  66. fldz ; loads zero on top of stack st(0)=0
  67.  
  68. mov rbx,array
  69. mov rsi,00 ; index of array initalized to 0
  70. xor rcx,rcx
  71. mov cx,[count] ; load count in cx reg
  72.  
  73. back: fadd dword[RBX+RSI*4] ; st(0)+[array+(index*4)]=st(0)
  74. ; each element in array is of type Double word = 4 bytes
  75. inc rsi ; increment array index
  76. loop back ; repeat addition untill all elements are added
  77.  
  78. fidiv word[count] ; st(0)=sum of array elements / count = mean
  79. fst dword[mean] ; store the st(0) in mean
  80.  
  81. print mmsg,mmsg_len
  82. call display_result
  83.  
  84. mov rbx,array
  85. mov rsi,00 ; index of array initalized to 0
  86. xor rcx,rcx
  87. mov cx,[count] ; load count in cx reg
  88.  
  89. fldz ; loads zero on top of stack st(0)=0
  90. back1:fldz
  91. FLD DWORD[RBX+RSI*4] ;st(0)=array[rsi]
  92. FSUB DWORD[mean] ;st(0)=st(0)-mean
  93. FST ST1
  94. FMUL ;st(0)=st(0)*st(1)
  95. FADD ;add squared value to st(1) i.e. st(0)=st(0)+st(1)
  96. INC RSI
  97. LOOP back1
  98.  
  99. FIDIV word[count] ;divide result by count to get variance
  100. FST dWORD[variance]
  101. FSQRT ;st(0)=sqrt(st(0))= value of standard deviation
  102.  
  103. print sdmsg,sdmsg_len
  104. CALL display_result
  105.  
  106. FLD dWORD[variance]
  107. print vmsg,vmsg_len
  108. CALL display_result
  109.  
  110. ;---------------------------------------------------------------
  111. display_8:
  112. mov rsi,char_ans+1
  113. mov rcx,2 ; number of digits
  114.  
  115. cnt: mov rdx,0
  116. mov rbx,16
  117. div rbx
  118. cmp dl, 09h ; check for remainder in RDX
  119. jbe add30
  120. add dl, 07h
  121. add30:
  122. add dl,30h ; calculate ASCII code
  123. mov [rsi],dl ; store it in buffer
  124. dec rsi ; point to one byte back
  125.  
  126. dec rcx ; decrement count
  127. jnz cnt
  128.  
  129. print char_ans,2 ; display result on screen
  130. ret
  131. ;---------------------------------------------------------------
  132. display_result:
  133.  
  134. fimul dword[hdec]
  135. fbstp tword[resbuff]
  136. xor rcx,rcx
  137. mov rcx,09H
  138. mov rsi,resbuff+9
  139.  
  140. nextdigit:
  141. push rcx
  142. push rsi
  143.  
  144. xor rax,rax
  145. mov al,[rsi]
  146. call display_8
  147.  
  148. pop rsi
  149. dec rsi
  150. pop rcx
  151. loop nextdigit
  152.  
  153. print dpoint,1
  154.  
  155. xor rax,rax
  156. mov al,[resbuff]
  157. call display_8
  158.  
  159. ret
  160. ;-------------------
  161.  
Success #stdin #stdout 0s 156KB
stdin
Standard input is empty
stdout
MIL assignment 13 : 8087 program for mean, SD, & Variance
---------------------------------------------

CALCULATED MEAN 		: 000000000000000140.03
CALCULATED STANDARD DEVIATION	: 000000000000000062.88
CALCULATED VARIANCE 		: 000000000000003954.42