Author Topic: Basic FPU Float32 NASM Macros: Add, Sub, Mul, Div (Example code)  (Read 14256 times)

Offline encryptor256

  • Full Member
  • **
  • Posts: 250
  • Country: lv
  • Win64 .
    • On Youtube: encryptor256
Hello!

Basic FPU Float32 NASM Macros!

Today i created some macros,
can share with you,
basic float32 management, in my way.

Im planning to extend these basic macros,
up to vector management, so, these are just a basic.


; ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
; float32 (Basic)
; ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

; 1. float32.push 1: Push float value from memory address onto stack
; 2. float32.pushv 1: Push float value onto stack
; 3. float32.set 2: Assign float value to a memory address
; 4. float32.pop 1: Pop float value off the stack, into memory address
; 5. float32.pops 0: Pop float value off the stack, into stack

Code: [Select]
; [*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*]
; [*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*]
; [*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*]
; ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
; float32 (Basic)
; ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

; 1. float32.push:
; ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

%macro float32.push 1
  fld dword [%1]
%endmacro

; 2. float32.pushv:
; ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

%macro float32.pushv 1
  sub esp,dword 4
  mov dword [esp],__float32__(%1)
  float32.push esp
  add esp,dword 4
%endmacro

; 3. float32.set:
; ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

%macro float32.set 2
  mov dword [%1],__float32__(%2)
%endmacro

; 4. float32.pop:
; ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

%macro float32.pop 1
  fstp dword [%1]
%endmacro

; 5. float32.pops:
; ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

%macro float32.pops 0
  sub esp,dword 4
  fstp dword [esp]
%endmacro

; ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
; float32 (Math)
; ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

; 1. float32.add 0-2
; 2. float32.addv 1-2
; 3. float32.sub 0-2
; 4. float32.subv 1-2
; 5. float32.mul 0-2
; 6. float32.mul 1-2
; 7. float32.div 0-2
; 8. float32.divv 1-2

Created add and addv, then just copy paste for other operations.

Code: [Select]
; [*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*]
; [*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*]
; [*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*],[*]
; ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
; float32 (Math)
; ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

; 1. float32.add:
; ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

%macro float32.add 0-2
  %if %0 > 0
    %rep %0
      float32.push %1
      %rotate 1
    %endrep
  %endif
  faddp st1,st0
%endmacro

; 2. float32.addv:
; ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

%macro float32.addv 1-2
  %rep %0
    float32.pushv %1
    %rotate 1
  %endrep
  faddp st1,st0
%endmacro

; 3. float32.sub:
; ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

%macro float32.sub 0-2
  %if %0 > 0
    %rep %0
      float32.push %1
      %rotate 1
    %endrep
  %endif
  fsubp st1,st0
%endmacro

; 4. float32.subv:
; ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

%macro float32.subv 1-2
  %rep %0
    float32.pushv %1
    %rotate 1
  %endrep
  fsubp st1,st0
%endmacro

; 5. float32.mul:
; ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

%macro float32.mul 0-2
  %if %0 > 0
    %rep %0
      float32.push %1
      %rotate 1
    %endrep
  %endif
  fmulp st1,st0
%endmacro

; 6. float32.mulv:
; ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

%macro float32.mulv 1-2
  %rep %0
    float32.pushv %1
    %rotate 1
  %endrep
  fmulp st1,st0
%endmacro

; 7. float32.div:
; ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

%macro float32.div 0-2
  %if %0 > 0
    %rep %0
      float32.push %1
      %rotate 1
    %endrep
  %endif
  fdivp st1,st0
%endmacro

; 8. float32.divv:
; ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

%macro float32.divv 1-2
  %rep %0
    float32.pushv %1
    %rotate 1
  %endrep
  fdivp st1,st0
%endmacro

Example code description:

Demonstration of some possible variations and usages,
each demo starts with commentary index, and index is also printed to the output.

Procedure printfFloat32 puts output on the screen:
; ---------------------------------------------------------
; Procedure printfFloat32
; This procedure prints float32
; Param1: Index
; Param2: float32 Value
; ---------------------------------------------------------

Here begins example code:


Code: [Select]
; 1. ------------------------------- (add)
float32.pushv 4.51
float32.pushv 0.5
float32.add
float32.pops
push dword 1
call printfFloat32

; 2. ------------------------------- (add)
float32.set float1,4.52
float32.push float1
float32.pushv 1.5
float32.add
float32.pops
push dword 2
call printfFloat32

; 3. ------------------------------- (addv)
float32.addv 4.5,2.53
float32.pops
push dword 3
call printfFloat32

; 4. ------------------------------- (add)
float32.set float1,4.54
float32.set float2,3.5
float32.push float1
float32.push float2
float32.add
float32.pop result
push dword [result]
push dword 4
call printfFloat32

; 5. ------------------------------- (add)
float32.set float1,4.55
float32.set float2,4.5
float32.add float1,float2
float32.pop result
push dword [result]
push dword 5
call printfFloat32

; 6. ------------------------------- (div)
float32.divv 100.0,25.0
float32.pops
push dword 6
call printfFloat32

; 7. ------------------------------- (sub)
float32.set float1,100.0
float32.set float2,25.0
float32.sub float1,float2
float32.pop result
push dword [result]
push dword 7
call printfFloat32


; 8. ------------------------------- (mul)
float32.set float1,100.0
float32.set float2,25.0
float32.mul float1,float2
float32.pops
push dword 8
call printfFloat32

This is output of that code:

Code: [Select]
1. float32: 5.010000
2. float32: 6.020000
3. float32: 7.030000
4. float32: 8.040000
5. float32: 9.050000
6. float32: 4.000000
7. float32: 75.000000
8. float32: 2500.000000

Attachment:

Added attachment: fpu_float32_macros.zip
There are two files,
First is main compile file: fpu_float32_main.asm
Second is include file: fpu_float32_macros.asm
Include file is included automaticaly when compiling.

Compile:

NASM: "nasm.exe fpu_float32_main.asm -f win32 -o fpu_float32_main.o "

GCC: "gcc fpu_float32_main.o -m32 -o fpu_float32_main.exe"

That's it!

Encryptor256!

« Last Edit: October 08, 2013, 09:58:07 AM by encryptor256 »
Encryptor256's Investigation \ Research Department.

Offline nullptr

  • Jr. Member
  • *
  • Posts: 27
Re: Basic FPU Float32 NASM Macros: Add, Sub, Mul, Div (Example code)
« Reply #1 on: October 10, 2013, 12:25:20 PM »
thanks Encryptor256 for providing code :)