Author Topic: Basic FPU Float64 and Vector2D NASM MACROS  (Read 14523 times)

Offline encryptor256

  • Full Member
  • **
  • Posts: 250
  • Country: lv
  • Win64 .
    • On Youtube: encryptor256
Basic FPU Float64 and Vector2D NASM MACROS
« on: December 30, 2013, 12:44:04 PM »
Hello!

This is Basic FPU Float64 and Vector2D NASM MACROS!

Using NASM, GOLINK, GCC-MINGW64 (see below on "How to compile" section).

Entry:

Some time ago i created Float32 macros, but now, im on x64 machine, so, Float64 and Vector2D macros are needed.

If you know C \ C++, then:
  • Float32 is 32 bit (or 4x byte) type named: float
  • Float64 is 64 bit (or 8x byte) type named: double

These are previous x32 examples:

Float64 macros (fpu_float64_nasm_macros.asm):
  • float64.push
  • float64.pushv
  • float64.set
  • float64.pop
  • float64.add
  • float64.addv
  • float64.sub
  • float64.subv
  • float64.mul
  • float64.mulv
  • float64.div
  • float64.abs - absolute value
  • float64.absv
  • float64.sqrt - square root
  • float64.sqrtv
  • float64.chs - change sign
  • float64.chsv
  • float64.xch - swap
  • float64.cmpv - experimental
  • float64.cmp - experimental

Float64 comparison note (float64.cmpv, float64.cmp):
Code: [Select]

; Quote from book "The Art of Assembly Language"
; 14.4.7 Comparison Instructions:
;  "..."
;  "Since the  sahf instruction does not copy"
;  "any 80x87 processor status bits into the sign or"
;  "overflow flags, you cannot use the  jg ,  jl ,  jge , or  jle instructions."
;  "Instead, use the  ja ,  jae ,  jb ,  jbe , je , and  jz instructions"
;  "when testing the results of a floating point comparison."

Float64 Vector2D Structure (fpu_float64_2d_vector_nasm_macros.asm):
Code: [Select]
; 2D VECTOR OFFSET STRUCTURE
VECTOR2D.X equ 8*0
VECTOR2D.Y equ 8*1
VECTOR2D_SIZE equ 8*2

Float64 Vector2D macros (fpu_float64_2d_vector_nasm_macros.asm):
  • vector2d.push
  • vector2d.pushv
  • vector2d.set
  • vector2d.pop
  • vector2d.add
  • vector2d.sub
  • vector2d.mul
  • vector2d.div
  • vector2d.magnitude
  • vector2d.normalize
  • vector2d.normal
  • vector2d.dot

Example code, test data output (produced by main.asm):
Code: [Select]

[code begin]



-> USAGE: FPU FLOAT 64 BIT NASM MACROS

1.1. float: 20.101000
1.2. float: 15.102000
2.1. float: 37.205000
2.2. float: 39.207000
2.3. float: 41.209000
2.4. float: 43.211000
3.1. float: 15.800000
3.2. float: 14.700000
3.3. float: 13.600000
3.4. float: 12.500000
4.1. float: 106.590000
4.2. float: 129.580000
4.3. float: 152.570000
4.4. float: 175.560000
5.1. float: 4.098039
5.2. float: 3.370968
5.3. float: 2.863014
5.4. float: 2.488095
5.5. float: 2.200000
6.1. float: -1024.768000
6.2. float: 1024.768000
6.3. float: 1024.768000
7.1. float: 5.062114
7.2. float: 4.028027
8.1. float: -25.625000
8.2. float: -25.625000

-> FPU FLOAT 64 BIT 2D VECTOR NASM MACROS

1.1. vector2d:(10.000000,15.000000)
1.2. vector2d:(11.000000,12.000000)
2.1. vector2d:(22.400000,24.600000)
2.2. vector2d:(24.600000,26.800000)
2.3. vector2d:(26.800000,29.000000)
3.1. vector2d:(70.400000,65.500000)
3.2. vector2d:(69.300000,64.400000)
3.3. vector2d:(68.200000,63.300000)
4.1. vector2d:(3025.050000,3544.640000)
4.2. vector2d:(3135.600000,3655.410000)
4.3. vector2d:(3246.150000,3766.180000)
5.1. vector2d:(3.338870,2.860795)
5.2. vector2d:(3.221154,2.774105)
5.3. vector2d:(3.111455,2.692513)
6.1. float: 17.804494
6.2. float: 17.804494
7.1. vector2d:(0.617822,0.786318)
7.2. vector2d:(0.617822,0.786318)
8.1. vector2d:(-14.000000,11.000000)
8.2. vector2d:(-14.000000,11.000000)
9.1. float: 814.340000

[code end]

Process completed, Exit Code 13.
Execution time: 00:00.140

Example code, test data is divided into three sections (main.asm):
  • USAGE: FPU FLOAT 64 BIT NASM MACROS
  • USAGE: FPU FLOAT 64 BIT 2D VECTOR NASM MACROS
  • USAGE: FPU FLOAT 64 BIT COMPARISON NASM MACROS - NEW
Code: [Select]
%ifndef _main_
%define _main_
bits 64
section .data use64

      txt_code_begin: db 10,"[code begin]",10,10,0
      txt_code_end: db 10,10,"[code end]",10,0

      buffer: times 1024 db 0

      txt_format0: db 10,"%d.%d. float: %f",0
      txt_format1: db 10,"%d.%d. vector2d:(%f,%f)",0

      flt0:       dq 0.0
      flt1:       dq 0.0
      fltr:       dq 0.0

      v2d0:       dq 0.0,0.0
      v2d1:       dq 0.0,0.0
      v2dr:       dq 0.0,0.0

      txt_task0:  db 10,10,"-> USAGE: FPU FLOAT 64 BIT NASM MACROS",10,0
      txt_task1:  db 10,10,"-> FPU FLOAT 64 BIT 2D VECTOR NASM MACROS",10,0

section .text use64

global main
extern printf

; Include macro files
%include "fpu_float64_nasm_macros.asm"
%include "fpu_float64_2d_vector_nasm_macros.asm"

; Printer macros for float's and vector's

; float64.print
; --------------------------------------
%macro float64.print 3
      mov r9,qword [%3]
      mov r8,qword %2
      mov rdx,qword %1
      mov rcx,qword txt_format0
      call printf
%endmacro

; vector2d.print
; --------------------------------------
%macro vector2d.print 3
      mov rax,qword [%3+8]
      mov qword [rsp+8*4],rax
      mov r9,qword [%3]
      mov r8,qword %2
      mov rdx,qword %1
      mov rcx,qword txt_format1
      call printf
%endmacro

; Entry point
; --------------------------------------
align 16
main:
      ; Create stack
      push rbp
      mov rbp,rsp
      push rbx
      lea rsp,[rsp-(8*15)]

      ; Print info
      mov rcx,qword txt_code_begin
      call printf

      ;  - - - - - - - - - - - - - - - - - - - - - -
      ; - - - - - - - - - - - - - - - - - - - - - - -
      ;  - - - - - - - - - - - - - - - - - - - - - -
      ;  USAGE: FPU FLOAT 64 BIT NASM MACROS
      ;  - - - - - - - - - - - - - - - - - - - - - -
      ; - - - - - - - - - - - - - - - - - - - - - - -
      ;  - - - - - - - - - - - - - - - - - - - - - -

      ; Print info: txt_task0
      mov rcx,qword txt_task0
      call printf

      ; 1. --------------------------------------
      ; 1.1-2. set, push, pop
      float64.set flt0,20.101
      float64.set flt1,15.102
      float64.push flt0
      float64.push flt1
      float64.pop flt1
      float64.pop flt0
      float64.print 1,1,flt0
      float64.print 1,2,flt1

      ; 2. --------------------------------------
      ; 2.1. add
      float64.set flt0,21.102
      float64.set flt1,16.103
      float64.add flt0,flt1
      float64.pop fltr
      float64.print 2,1,fltr
      ; 2.2. pushv
      float64.pushv 22.103
      float64.pushv 17.104
      float64.add
      float64.pop fltr
      float64.print 2,2,fltr
      ; 2.3. other
      float64.pushv 23.104
      float64.set flt1,18.105
      float64.add flt1
      float64.pop fltr
      float64.print 2,3,fltr
      ; 2.4. addv
      float64.addv 24.105,19.106
      float64.pop fltr
      float64.print 2,4,fltr

      ; 3. --------------------------------------
      ; 3.1. sub
      float64.set flt0,20.9
      float64.set flt1,05.1
      float64.sub flt0,flt1
      float64.pop fltr
      float64.print 3,1,fltr
      ; 3.2. pushv
      float64.pushv 20.9
      float64.pushv 06.2
      float64.sub
      float64.pop fltr
      float64.print 3,2,fltr
      ; 3.4. other
      float64.pushv 20.9
      float64.set flt1,07.3
      float64.sub flt1
      float64.pop fltr
      float64.print 3,3,fltr
      ; 3.5. subv
      float64.subv 20.9,08.4
      float64.pop fltr
      float64.print 3,4,fltr

      ; 4. --------------------------------------
      ; 4.1. mul
      float64.set flt0,20.9
      float64.set flt1,05.1
      float64.mul flt0,flt1
      float64.pop fltr
      float64.print 4,1,fltr ; (Calcualtor say's: 106.59)
      ; 4.2. pushv
      float64.pushv 20.9
      float64.pushv 06.2
      float64.mul
      float64.pop fltr
      float64.print 4,2,fltr ; (Calcualtor say's: 129.58)
      ; 4.3. other
      float64.pushv 20.9
      float64.set flt1,07.3
      float64.mul flt1
      float64.pop fltr
      float64.print 4,3,fltr ; (Calcualtor say's: 152.57)
      ; 4.4. mulv
      float64.mulv 20.9,08.4
      float64.pop fltr
      float64.print 4,4,fltr ; (Calcualtor say's: 175.56)

      ; 5. --------------------------------------
      ; 5.1. div
      float64.set flt0,20.9
      float64.set flt1,05.1
      float64.div flt0,flt1
      float64.pop fltr
      float64.print 5,1,fltr ; (Calcualtor say's: 4.0980392156862745098039215686275)
      ; 5.2. pushv
      float64.pushv 20.9
      float64.pushv 06.2
      float64.div
      float64.pop fltr
      float64.print 5,2,fltr ; (Calcualtor say's: 3.3709677419354838709677419354839)
      ; 5.3. other
      float64.pushv 20.9
      float64.set flt1,07.3
      float64.div flt1
      float64.pop fltr
      float64.print 5,3,fltr ; (Calcualtor say's: 2.8630136986301369863013698630137)
      ; 5.4. divv
      float64.divv 20.9,08.4
      float64.pop fltr
      float64.print 5,4,fltr ; (Calcualtor say's: 2.4880952380952380952380952380952)
      ; 5.5. other
      float64.pushv 20.9
      float64.divv 09.5
      float64.pop fltr
      float64.print 5,5,fltr ; (Calcualtor say's: 2.2)

      ; 6. --------------------------------------
      ; 6.1. other
      float64.pushv -1024.768
      float64.pop fltr
      float64.print 6,1,fltr
      ; 6.2. abs
      float64.pushv -1024.768
      float64.abs
      float64.pop fltr
      float64.print 6,2,fltr
      ; 6.3. absv
      float64.absv -1024.768
      float64.pop fltr
      float64.print 6,3,fltr

      ; 7. --------------------------------------
      ; 7.1. sqrt
      float64.pushv 25.625
      float64.sqrt
      float64.pop fltr
      float64.print 7,1,fltr ; (Calcualtor say's: 5.0621141828291467333117255724753)
      ; 7.2. sqrtv
      float64.sqrtv 16.225
      float64.pop fltr
      float64.print 7,2,fltr ; (Calcualtor say's: 4.0280268122245661139295278926851)

      ; 8. --------------------------------------
      ; 8.1. chs - change sign
      float64.pushv 25.625
      float64.chs
      float64.pop fltr
      float64.print 8,1,fltr
      ; 8.2. chsv
      float64.chsv 25.625
      float64.pop fltr
      float64.print 8,2,fltr

      ;  - - - - - - - - - - - - - - - - - - - - - -
      ; - - - - - - - - - - - - - - - - - - - - - - -
      ;  - - - - - - - - - - - - - - - - - - - - - -
      ;  USAGE: FPU FLOAT 64 BIT 2D VECTOR NASM MACROS
      ;  - - - - - - - - - - - - - - - - - - - - - -
      ; - - - - - - - - - - - - - - - - - - - - - - -
      ;  - - - - - - - - - - - - - - - - - - - - - -

      ; Print info: txt_task1
      mov rcx,qword txt_task1
      call printf

      ; 1. --------------------------------------
      ; 1.1-2. set, push, pop
      vector2d.set v2d0,10.0,15.0
      vector2d.set v2d1,11.0,12.0
      vector2d.push v2d0
      vector2d.push v2d1
      vector2d.pop v2d1
      vector2d.pop v2d0
      vector2d.print 1,1,v2d0
      vector2d.print 1,2,v2d1

      ; 2. --------------------------------------
      ; 2.1. add
      vector2d.set v2d0,10.1,11.2
      vector2d.set v2d1,12.3,13.4
      vector2d.add v2d0,v2d1
      vector2d.pop v2dr
      vector2d.print 2,1,v2dr
      ; 2.2. pushv
      vector2d.pushv 11.2,12.3
      vector2d.pushv 13.4,14.5
      vector2d.add
      vector2d.pop v2dr
      vector2d.print 2,2,v2dr
      ; 2.3. other
      vector2d.pushv 12.3,13.4
      vector2d.set v2d1,14.5,15.6
      vector2d.add v2d1
      vector2d.pop v2dr
      vector2d.print 2,3,v2dr

      ; 3. --------------------------------------
      ; 3.1. sub
      vector2d.set v2d0,100.5,100.7
      vector2d.set v2d1,30.1,35.2
      vector2d.sub v2d0,v2d1
      vector2d.pop v2dr
      vector2d.print 3,1,v2dr
      ; 3.2. pushv
      vector2d.pushv 100.5,100.7
      vector2d.pushv 31.2,36.3
      vector2d.sub
      vector2d.pop v2dr
      vector2d.print 3,2,v2dr
      ; 3.4. other
      vector2d.pushv 100.5,100.7
      vector2d.set v2d1,32.3,37.4
      vector2d.sub v2d1
      vector2d.pop v2dr
      vector2d.print 3,3,v2dr

      ; 4. --------------------------------------
      ; 4.1. mul
      vector2d.set v2d0,100.5,100.7
      vector2d.set v2d1,30.1,35.2
      vector2d.mul v2d0,v2d1
      vector2d.pop v2dr
      vector2d.print 4,1,v2dr ; (Calcualtor say's: 3025.05, 3544.64)
      ; 4.2. pushv
      vector2d.pushv 100.5,100.7
      vector2d.pushv 31.2,36.3
      vector2d.mul
      vector2d.pop v2dr
      vector2d.print 4,2,v2dr ; (Calcualtor say's: 3135.6, 3655.41)
      ; 4.3. other
      vector2d.pushv 100.5,100.7
      vector2d.set v2d1,32.3,37.4
      vector2d.mul v2d1
      vector2d.pop v2dr
      vector2d.print 4,3,v2dr ; (Calcualtor say's: 3246.15, 3766.18)

      ; 5. --------------------------------------
      ; 5.1. div
      vector2d.set v2d0,100.5,100.7
      vector2d.set v2d1,30.1,35.2
      vector2d.div v2d0,v2d1
      vector2d.pop v2dr
      vector2d.print 5,1,v2dr ; (Calcualtor say's: 3.338870431893687707641196013289, 2.8607954545454545454545454545455)
      ; 5.2. pushv
      vector2d.pushv 100.5,100.7
      vector2d.pushv 31.2,36.3
      vector2d.div
      vector2d.pop v2dr
      vector2d.print 5,2,v2dr ; (Calcualtor say's: 3.2211538461538461538461538461538, 2.7741046831955922865013774104683)
      ; 5.3. other
      vector2d.pushv 100.5,100.7
      vector2d.set v2d1,32.3,37.4
      vector2d.div v2d1
      vector2d.pop v2dr
      vector2d.print 5,3,v2dr ; (Calcualtor say's: 3.1114551083591331269349845201238, 2.6925133689839572192513368983957)

      ; 6. --------------------------------------
      ; 6.1. magnitude
      vector2d.set v2d0,11.0,14.0
      vector2d.magnitude v2d0
      float64.pop fltr
      float64.print 6,1,fltr ; (Calcualtor say's: 17.804493814764855594546558781474 )
      ; 6.2. other
      vector2d.pushv 11.0,14.0
      vector2d.magnitude
      float64.pop fltr
      float64.print 6,2,fltr

      ; 7. --------------------------------------
      ; 7.1. normalize
      vector2d.set v2d0,11.0,14.0
      vector2d.normalize v2d0
      vector2d.pop v2dr
      vector2d.print 7,1,v2dr; (Calcualtor say's: 0.61782158020450231298358229015564, 0.7863183748057302165245592783799 )
      ; 7.2. other
      vector2d.pushv 11.0,14.0
      vector2d.normalize
      vector2d.pop v2dr
      vector2d.print 7,2,v2dr

      ; 8. --------------------------------------
      ; 8.1. normal
      vector2d.set v2d0,11.0,14.0
      vector2d.normal v2d0
      vector2d.pop v2dr
      vector2d.print 8,1,v2dr
      ; 8.2. other
      vector2d.pushv 11.0,14.0
      vector2d.normal
      vector2d.pop v2dr
      vector2d.print 8,2,v2dr

      ; 9. --------------------------------------
      ; 9.1. dot
      vector2d.set v2d0,11.4,14.6
      vector2d.set v2d1,25.2,36.1
      vector2d.dot v2d0,v2d1
      float64.pop fltr
      float64.print 9,1,fltr ; (Calcualtor say's: 814.34)


      ;  - - - - - - - - - - - - - - - - - - - - - -
      ; - - - - - - - - - - - - - - - - - - - - - - -
      ;  - - - - - - - - - - - - - - - - - - - - - -
      ;  USAGE: FPU FLOAT 64 BIT COMPARISON NASM MACROS
      ;  - - - - - - - - - - - - - - - - - - - - - -
      ; - - - - - - - - - - - - - - - - - - - - - - -
      ;  - - - - - - - - - - - - - - - - - - - - - -

; 1. JA (This will hang as expected)
;       float64.set flt0,20.9
;       float64.set flt1,05.1
;       float64.cmp flt0,flt1
;       ja 123
; 2. JA (This will hang as expected)
;       float64.cmpv 20.9,05.1
;       ja 123
; 3. JA (This will not hang as expected)
;       float64.cmpv 10.2,20.9
;       ja 123
; 4. JA (This will not hang as expected)
;       float64.cmpv 10.2,10.2
;       ja 123
; 5. JE (This will hang as expected)
;       float64.cmpv 10.2,10.2
;       je 123
; 6. JAE (This will hang as expected)
;       float64.cmpv 10.2,10.2
;       jae 123
; 7. JAE (This will hang as expected)
;       float64.cmpv 11.2,10.2
;       jae 12
; 8. JAE (This will not hang as expected)
;       float64.cmpv 10.1,10.2
;       jae 123

      ; Print info
      mov rcx,qword txt_code_end
      call printf
.quit:
      ; Clear stack n quit
      lea rsp,[rsp+(8*15)]
      pop rbx
      pop rbp
      ret
%endif

You can find, in example code and match index of output data, to find what print's what.
Example code:
Code: [Select]
      ; 4. --------------------------------------
      ; 4.1. mul
      float64.set flt0,20.9
      float64.set flt1,05.1
      float64.mul flt0,flt1
      float64.pop fltr
      float64.print 4,1,fltr ; (Calcualtor say's: 106.59)
Outputs:
Code: [Select]
4.1. float: 106.590000

Attachments:
  • main.asm
  • fpu_float64_nasm_macros.asm
  • fpu_float64_2d_vector_nasm_macros.asm
  • princess barbie adventues book.pdf

How to compile:

1. NASM, The Netwide Assembler, (http://nasm.us/)

Command line: "nasm.exe -f win64 main.asm -o main.obj"

2.1. GoLink, Jeremy Gordon's Go Tools for Win32 and Win64, (http://www.godevtool.com/)

Command line: "golink.exe /entry main /console main.obj MSVCRT.dll kernel32.dll"

2.2. GCC-MINGW64, Mingw-w64 delivers runtime, headers and libs for developing both 64 bit (x64) and 32 bit (x86) windows applications using GCC and other free software compilers, (http://mingw-w64.sourceforge.net/)

Command line: "gcc -m64 main.obj"



I think that's it!
Encryptor256.
Encryptor256's Investigation \ Research Department.