section .data
arr dd 140.60,222.40,313.40,422.30,521.02
mean_msg db "The Mean is: "
len_mean equ $-mean_msg
varmsg db "The Variance is: "
lenvarmsg equ $-varmsg
sdmsg db "The Standard Deviation is: "
lensdmsg equ $-sdmsg
NL db 10
divv dw 5
num dw 10000
zero dd 0.0
section .bss
mean resd 1
buff rest 1
disbuff resb 21
var resw 1
%macro disp 2
mov rax,01
mov rdi,01
mov rsi,%1
mov rdx,%2
syscall
%endmacro
section .text
global _start
_start:
;initializing FPU
finit
;****************MEAN**********************;
;Loading zero in stack top
fldz
mov ch,05
xor rsi,rsi
mov rdx,arr
up:
fadd dword [rdx+rsi]
add rsi,04
dec ch
jnz up
fidiv word [divv] ;fdiv given
fst dword [mean] ;why dword taken??It could be ten bytes long
disp mean_msg,len_mean
call display
disp NL,1
;***********VARIANCE************************;
fldz
;fst st1
mov ch,05
xor rsi,rsi
mov rdx,arr
up1:
fld dword [rdx+rsi]
fsub dword [mean]
fmul st0,st0
fadd st0,st1 ;fadd st1,st0------->THIS WAS SO TRUE
fst st1
add rsi,04
dec ch
jnz up1
fld st1
fidiv word [divv]
fst dword [var]
disp varmsg,lenvarmsg
call display
disp NL,1
;*************SD****************************;
fld dword [var]
fsqrt
disp sdmsg,lensdmsg
call display
disp NL,1
;*********Termination***********************;
mov rax,3Ch
mov rdi,00
syscall
display:
fimul word [num] ;caused the main issue
fbstp tword [buff]
mov rax,[buff+2]
mov rsi,disbuff
mov ch,16
mov cl,04
agn:
rol rax,cl
mov bl,al
and bl,0Fh
add bl,30h
mov [rsi],bl
inc rsi
dec ch
jnz agn
mov byte [rsi],'.'
inc rsi
mov ax,[buff]
mov ch,04
agn1:
rol ax,cl
mov bl,al
and bl,0FH
add bl,30H
mov [rsi],bl
inc rsi
dec ch
jnz agn1
mov rax,01
mov rdi,01
mov rsi,disbuff
mov rdx,21
syscall
ret
output:
[gaurav@localhost ~]$ nasm -f elf64 8087.asm -o 8087.o
[gaurav@localhost ~]$ ld -o 8087 8087.o
[gaurav@localhost ~]$ ./8087
The Mean is: 0000000000000323.9440
The Variance is: 0000000000018510.0478
The Standard Deviation is: 0000000000000136.0516
[gaurav@localhost ~]$
arr dd 140.60,222.40,313.40,422.30,521.02
mean_msg db "The Mean is: "
len_mean equ $-mean_msg
varmsg db "The Variance is: "
lenvarmsg equ $-varmsg
sdmsg db "The Standard Deviation is: "
lensdmsg equ $-sdmsg
NL db 10
divv dw 5
num dw 10000
zero dd 0.0
section .bss
mean resd 1
buff rest 1
disbuff resb 21
var resw 1
%macro disp 2
mov rax,01
mov rdi,01
mov rsi,%1
mov rdx,%2
syscall
%endmacro
section .text
global _start
_start:
;initializing FPU
finit
;****************MEAN**********************;
;Loading zero in stack top
fldz
mov ch,05
xor rsi,rsi
mov rdx,arr
up:
fadd dword [rdx+rsi]
add rsi,04
dec ch
jnz up
fidiv word [divv] ;fdiv given
fst dword [mean] ;why dword taken??It could be ten bytes long
disp mean_msg,len_mean
call display
disp NL,1
;***********VARIANCE************************;
fldz
;fst st1
mov ch,05
xor rsi,rsi
mov rdx,arr
up1:
fld dword [rdx+rsi]
fsub dword [mean]
fmul st0,st0
fadd st0,st1 ;fadd st1,st0------->THIS WAS SO TRUE
fst st1
add rsi,04
dec ch
jnz up1
fld st1
fidiv word [divv]
fst dword [var]
disp varmsg,lenvarmsg
call display
disp NL,1
;*************SD****************************;
fld dword [var]
fsqrt
disp sdmsg,lensdmsg
call display
disp NL,1
;*********Termination***********************;
mov rax,3Ch
mov rdi,00
syscall
display:
fimul word [num] ;caused the main issue
fbstp tword [buff]
mov rax,[buff+2]
mov rsi,disbuff
mov ch,16
mov cl,04
agn:
rol rax,cl
mov bl,al
and bl,0Fh
add bl,30h
mov [rsi],bl
inc rsi
dec ch
jnz agn
mov byte [rsi],'.'
inc rsi
mov ax,[buff]
mov ch,04
agn1:
rol ax,cl
mov bl,al
and bl,0FH
add bl,30H
mov [rsi],bl
inc rsi
dec ch
jnz agn1
mov rax,01
mov rdi,01
mov rsi,disbuff
mov rdx,21
syscall
ret
output:
[gaurav@localhost ~]$ nasm -f elf64 8087.asm -o 8087.o
[gaurav@localhost ~]$ ld -o 8087 8087.o
[gaurav@localhost ~]$ ./8087
The Mean is: 0000000000000323.9440
The Variance is: 0000000000018510.0478
The Standard Deviation is: 0000000000000136.0516
[gaurav@localhost ~]$