Tuesday, 14 April 2015

Floating point operations in Assembly Language

*************PROGRAM***************
FLOATING POINT OPERATIONS
******************************************

extern printf,scanf

section .data
m0 db 10,"FLOATING POINT OPERATIONS",0
m1 db "%s",0
m2 db 10,"Enter number:",0
m3 db "Number is:",0
m4 db "%lf",0
m5 db 10,"The Mean is:",0
m6 db 10,"The Variance is:",0
m7 db 10,"The Standard Deviation is:",0

section .bss
v1 resb 8
v2 resb 8
v3 resb 8
f1 resb 8
f2 resb 8
f3 resb 8
c resb 2
mean resb 8
var resb 8
dev resb 8

%macro print 2
mov rax,0
mov rdi,%1
mov rsi,%2
call printf
%endmacro

%macro printfloat 2
push rbp
mov rax,1
mov rdi,%1
movsd xmm0,%2
call printf
pop rbp
%endmacro

%macro scan 2
mov rax,0
mov rdi,%1
mov rsi,%2
call scanf
%endmacro

section .text
global main
main:
print m1,m0
print m1,m2
scan m4,v1
print m1,m3
printfloat m4,[v1]

print m1,m2
scan m4,v2
print m1,m3
printfloat m4,[v2]

print m1,m2
scan m4,v3
print m1,m3
printfloat m4,[v3]

finit
fld qword[v1]
fadd qword[v2]
fadd qword[v3]
mov byte[c],03

fidiv word[c]
fstp qword[mean]
print m1,m5
printfloat m4,[mean]

finit
fld qword[v1]
fld qword[v2]
fld qword[v3]
fsub qword[mean]
fmul st0
fstp qword[f1]
fsub qword[mean]
fmul st0
fstp qword[f2]
fsub qword[mean]
fmul st0
fstp qword[f3]
fld qword[f1]
fadd qword[f2]
fadd qword[f3]
fidiv word[c]
fstp qword[var]
print m1,m6
printfloat m4,[var]

finit
fld qword[var]
fsqrt
fstp qword[dev]
print m1,m7
printfloat m4,[dev]

**************OUTPUT****************
sourabh@Ubuntu:~ nasm -f elf64 float.asm
sourabh@Ubuntu:~ gcc float.o
sourabh@Ubuntu:~ ./a.out

FLOATING POINT OPERATIONS
Enter number:10
Number is:10.000000
Enter number:20
Number is:20.000000
Enter number:30
Number is:30.000000
The Mean is:20.000000
The Variance is:66.666667
The Standard Deviation is:8.164966 :D

No comments:

Post a Comment