Results 1 to 4 of 4

Thread: Arithmetic on the FPU: (vv1+vv2) / (vv3+vv4)

  1. #1

    Arithmetic on the FPU: (vv1+vv2) / (vv3+vv4)


    The FPU is a stack machine with a depth of 8 registers. So when you use it think Reverse Polish Notation (RPN). Variables are always loaded from memory onto st(0), pushing the other registers down, so the previous st(0) becomes st(1) and so forth. Arithmetic is executed on the st(0) register using st(0) and a memory operand or an st(0..7) register. Operands are popped from the FPU stack once they have served their purpose, leaving the FPU in its original state.

    [code=thinbasic]

    ' Using Machine Code with ThinBasic
    '---------------------------------------------------------------------------
    '---Reference:
    ' Intel x86 Architecture Ref Manual Vol 2
    '---http://developer.intel.com/design/pentiumii/manuals/243191.htm
    '---------------------------------------------------------------------------
    ' Syntax rules:
    ' #Variable patches in long address of Variable (4 bytes)
    ' NLn patches in long decimal value n (4 bytes)
    ' comments are indicated by a quote mark '
    ' all other words are read as hexadecimal bytes.
    ' An error during MC_Eval$ will produce a string containing &hc3 (ret) only.

    '----------------------------------------------
    ' arithmentic on the Floating Point Processor (FPU)
    '
    ' (vv1+vv2) / (vv3+vv4)
    '
    ' 23 bytes
    '----------------------------------------------
    dim vv( as double
    dim sMC as string = MC_Eval$ "
    b8 #vv ' vv base address
    dd 40 00 ' fld qword ptr [eax+00] ' ( vv1
    dc 40 08 ' fadd qword ptr [eax+08] ' + vv2
    dd 40 10 ' fld qword ptr [eax+16] ' ) ( vv3
    dc 40 18 ' fadd qword ptr [eax+24] ' +vv4
    'de c9 ' fmulp st(1) '
    de f9 ' fdivp st(1) ' ) /
    dd 58 38 ' fstp qword ptr [eax+56] ' = vv8
    c3 ' ret '
    "
    '-------------------------------'

    vv(1)=1.5
    vv(2)=2
    vv(3)=2.5
    vv(4)=3
    ' (vv1+vv2) / (vv3+vv4)
    ' = 0.636363...

    MC_Exec sMC

    msgbox 0, vv( ' result

    [/code]

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Arithmetic on the FPU: (vv1+vv2) / (vv3+vv4)

    Thanks Charles,

    RPN is the best
    Here I add explanative picture of "what happens on the stack", hope not many mistakes


    Petr
    Attached Images Attached Images
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3

    Re: Arithmetic on the FPU: (vv1+vv2) / (vv3+vv4)

    Thank you Petr.
    How did you produce the image?

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Arithmetic on the FPU: (vv1+vv2) / (vv3+vv4)

    Hi Charles,

    I did it in Excel :P, could be done using TBGL or other thinBASIC modules too.


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Similar Threads

  1. Arithmetic
    By Charles Pegge in forum O2h Compiler
    Replies: 0
    Last Post: 17-03-2009, 15:27

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •