Saturday, April 19, 2014

Math co-processor 8087 program to obtain Mean, Variance and Standard Deviation in NASM[x86/64]


Problem Statement: Write 8087 ALP to obtain:
i) Mean ii) Variance iii) Standard Deviation
For a given set of data elements defined in data segment. Also display result.




section .data

numbers db "The numbers are:102.59,198.21,100.67,230.78,67.93",10
len equ $-numbers
meanmsg db 10,"CALCULATED MEAN IS:-"
meanmsg_len equ $-meanmsg
sdmsg db 10,"CALCULATED STANDARD DEVIATION IS:-"
sdmsg_len equ $-sdmsg
varmsg db 10,"CALCULATED VARIANCE IS:-"
varmsg_len equ $-varmsg
array dd 102.56,198.21,100.67,230.78,67.93
arraycnt dw 05
dpoint db '.'
hdec dq 100

section .bss
dispbuff resb 1
resbuff rest 1

mean resd 1
variance resd 1

%macro linuxsyscall 4
mov rax,%1
mov rdi,%2
mov rsi,%3
mov rdx,%4
syscall
%endmacro   


section .text
global _start
_start:
linuxsyscall 01,01,numbers,len
finit
fldz
mov rbx,array
mov rsi,00
xor rcx,rcx
mov cx,[arraycnt]

up:
fadd dword[RBX+RSI*4]
inc rsi
loop up

fidiv word[arraycnt]
fst dword[mean]
linuxsyscall 01,01,meanmsg,meanmsg_len
call dispres

mov rcx,00
mov cx,[arraycnt]
mov rbx,array
mov rsi,00
FLDZ

up1:
FLDZ
FLD dword[RBX+RSI*4]
FSUB dword[mean]
FST ST1
FMUL
FADD
inc rsi
loop up1

FIDIV word[arraycnt]
FST dword[variance]
FSQRT
linuxsyscall 01,01,sdmsg,sdmsg_len
CALL dispres

FLD dword[variance]
linuxsyscall 01,01,varmsg,varmsg_len
CALL dispres

exit: 
mov rax,60
mov rdi,0
syscall


disp8_proc:
mov rdi,dispbuff
mov rcx,02

back:
rol bl,04
mov dl,bl
and dl,0FH
cmp dl,09
jbe next1
add dl,07H

next1:  
add dl,30H
mov [rdi],dl
inc rdi
loop back
ret

dispres:
fimul dword[hdec]
fbstp tword[resbuff]
xor rcx,rcx
mov rcx,09H
mov rsi,resbuff+9

up2:
push rcx
push rsi
mov bl,[rsi]
call disp8_proc

linuxsyscall 01,01,dispbuff,2
pop rsi
dec rsi
pop rcx
loop up2

linuxsyscall 01,01,dpoint,1
mov bl,[resbuff]
call disp8_proc
linuxsyscall 01,01,dispbuff,2
ret

Output

amodi@ubuntu:~/MIL/Assign7$ nasm -f elf64 -l asgn7.lst asgn7.asm
amodi@ubuntu:~/MIL/Assign7$ ld -o asgn7 asgn7.o
amodi@ubuntu:~/MIL/Assign7$ ./asgn7
The numbers are: 102.59,198.21,100.67,230.78,67.93

CALCULATED MEAN IS:-000000000000000140.30
CALCULATED STANDARD DEVIATION IS:-000000000000000062.32
CALCULATED VARIANCE IS:-000000000000003954.34
 

3 comments:

  1. What is difference between dd,dw dq..??

    ReplyDelete
    Replies
    1. DW DQ and DD are three assembly language instructions for storing the maximum positive values.
      WordInteger DW 32767
      ShortInteger DD 2147483647
      LongInteger DQ 9223372836854775807

      DW means "define word", DD "define double word" and DQ"define quad word"

      Delete
  2. Why do we have to use FLDZ beffore we Enter the loop for variance.. i.e before up1.

    The FLDZ instruction inside the loop should be enough right?

    ReplyDelete

Perform a suitable assignment using Xen Hypervisor or equivalent open source to configure it. Give necessary GUI.

 To install kvm on Fedora:  yum install kvm  yum install virt-manager libvirt libvirt-python python-virtinst  su -c "yum install @v...