Please disable adblock to view this page.

← Go home

Mean, Variance, Standard Deviation using 8087 (Assembly x86)

April 23, 2016
Published By : Pratik Kataria
Categorised in: ,

%macro disp 4
mov eax,%1
mov ebx,%2
mov ecx,%3
mov edx,%4
int 80h

section .data
arr dd 10.00 , 20.00 , 30.00 , 40.00 , 50.00
num dd 5.00
mult dd 10000.00
dot db “.”
newl db 10

section .bss

mean rest 1
result resb 15
temp resb 1
meanst resd 1
store1 resd 1
store2 resd 1
section .text

global _start

mov esi,arr
mov edx,5

finit    ; initialization of co processor
fldz    ;load zero on top the stack


above:    fadd dword[esi]    ; top of the stack= top of the stack+[esi]
add esi,4
dec edx
jnz above


fdiv dword[num]    ;top of the stack = top of the stack/[num]

fst dword[meanst];this will store top of the stack real in [meanst] variable

fmul dword[mult];this will store =top of the stack multiply by 10000.00  this is done to print decimal

fbstp tword[mean]; this will convert top of the stack to BCD and pop in mean register

;mean get displayed

mov esi,mean
call print

;variance (variance =((firstno. -mean)2+(secondno. -mean)2….)/totalnumber
disp 0,0,0,0
mov esi,0
mov esi,arr ;first esi will point to the array
mov edx,5    ;as there are total 5 number
mov dword[store2],0h    ;initialisinf store2 to 0 which will contain final variance answer

above4:    fldz            ;top of stack is initialize to 0
fadd dword[esi]        ;top of stack is initialise by [esi]
fsub dword[meanst]    ;mean is subtracted
fst dword[store1]    ; no. -mean is stored in [store1]
fmul dword[store1]    ;top of the stack is multiply by same number
fadd dword[store2]    ;added to top of stack
fst dword[store2]    ;finally store store2 number which final value will have (firstno. -mean)2+(secondno. -mean)2….
add esi,4        ;as array is dd
dec edx            ;counter is decremented
jnz above4

fdiv dword[num]        ;top of stack will be divided by total number thus having variance value
fst dword[store1]    ;store top of stack in store1
fmul dword[mult]    ;multiply top of stack with 10000
fbstp tword[mean]    ;and store in mean variable
mov esi,mean
call print        ;display of variance

disp 0,0,0,0
fld dword[store1]
fsqrt        ;this will find square root of variance and store in top of stack
fmul dword[mult]
fbstp tword[mean]
mov esi,mean
call print

disp 1,0,0,0    ;exit is called

print:    disp 0,0,0,0    ;all ax,bx,cx,dx register is initialize to 0

mov edi,result    ;edi pointing through display buffer
add esi,9    ;esi now pinted to last element
mov edx,10    ;edx act as a counter

;this loop is for checking when the byte of esi is not  0 if not 0 then move to below else repeat process
above1:    mov al,byte[esi]
cmp al,00
jne below
dec esi
dec edx
jmp above1

below:    mov eax,0    ;eax is initialise to 0

above3:    mov al,byte[esi]    ;al will contain 2 packed number
mov cx,2        ;cx is used as counter

above2:    rol al,4
mov [temp],al        ;first ah bit will convert into ascii then al register converted into ascii
and al,0FH
add al,30h
mov [edi],al
inc edi
mov al,[temp]
dec cx
jnz above2

dec esi
cmp edx,3
jne below1
mov al,byte[dot]    ;if edx become 3 then . should be printed
mov [edi],al
inc edi

below1:    dec edx
jnz above3

disp 4,1,result,15
disp 4,1,newl,1

View Article Page