Hello again everybody:
I was working hard about this theme and finally i've a method to work with integers & floating point numbers joined. In other words: you can to add, substract, multiply or divide 2 numers that you need in the order that you like. f.i an integer + float, 2 floats, 2 integers or a float with an intege, whatever.
this is the code of nasmath.asm
; --------------------------------------------------------------
; nasmath.asm
; Script to operate with 2 numbers (15.15 and 5)
; Addition, Substraction, Multiplication and Division
; with floating point and integer numbers
;
; Compile: nasm -f elf64 nasmath.asm
; Link: gcc -no-pie nasmath.o -o nasmath
; Run: ./nasmath
; --------------------------------------------------------------
bits 64
extern printf
segment .rodata
CLS: db `\033[2J\033[H`, 0 ; Clean Screen format
LF: db ` `, 10, 0 ; Line Feed format
segment .data
Htitle: db "Operations with 15.15 and 5", 10, 0 ;header
Hsepar: db "------------------------------", 10, 0 ;separator
Maddit: db "Addition : %g", 10, 0 ;message for addition
Msubst: db "Substraction: %g", 10, 0 ;message for substraction
Mmulti: db "Multiply : %g", 10, 0 ;message for multiplication
Mdivis: db "Division : %g", 10, 0 ;message for division
Number1: dq 15.15 ; a floating point number
Number2: dq 5 ; an integer number
segment .bss
Result: resq 1
segment .text
global main
main:
push rbp
; ----------------------- Printing headers -------------------------
lea rdi, [CLS]
xor rax, rax
call printf
lea rdi, [Hsepar]
xor rax, rax
call printf
lea rdi, [Htitle]
xor rax, rax
call printf
lea rdi, [Hsepar]
xor rax, rax
call printf
lea rdi, [LF]
xor rax, rax
call printf
; ----------------------- Addition ----------------------
fld qword [Number1] ; (1) If Number1 is an integer
fst qword [Result] ; change 'fld' by 'fild'
fild qword [Number2] ; (2) If Number2 is a FP
fadd qword [Result] ; change 'fild' by 'fld'
fstp qword [Result]
lea rdi, [Maddit]
movq xmm0, qword [Result]
xor rax, 1
call printf
; ----------------------- Substraction ------------------
fld qword [Number1] ; (1)
fst qword [Result]
fild qword [Number2] ; (2)
fsubr qword [Result]
fstp qword [Result]
lea rdi, [Msubst]
movq xmm0, qword [Result]
xor rax, 1
call printf
; ----------------------- Multiplication ----------------
fld qword [Number1] ; (1)
fst qword [Result]
fild qword [Number2] ; (2)
fmul qword [Result]
fstp qword [Result]
lea rdi, [Mmulti]
movq xmm0, qword [Result]
xor rax, 1
call printf
; ----------------------- Division -----------------------
fild qword [Number2] ; (1) *We need reverse the order
fst qword [Result] ; of dividend/divisor
fld qword [Number1] ; (2)
fdiv qword [Result]
fstp qword [Result]
lea rdi, [Mdivis]
movq xmm0, qword [Result]
xor rax, 1
call printf
; ----------------------- Exit ----------------------
pop rbp
mov rax, 0
ret
Enjoy it.