x86 Assembly/Floating Point - Wikibooks, open books for an open world (2024)

x86 Assembly
quick links: registersmovejumpcalculatelogicrearrangemisc.FPU

The ALU is only capable of dealing with integer values.While integers are sufficient for some applications, it is often necessary to use decimals.A highly specialized coprocessor, all part of the FPU– the floating-point unit–, will allow you to manipulate numbers with fractional parts.

Contents

  • 1 x87 Coprocessor
  • 2 FPU Register Stack
  • 3 Examples
  • 4 Floating-Point Instruction Set
    • 4.1 Original 8087 instructions
      • 4.1.1 Data Transfer Instructions
      • 4.1.2 Arithmetic Instructions
      • 4.1.3 FPU Internal and Other Instructions
    • 4.2 Added in specific processors
      • 4.2.1 Added with 80287
      • 4.2.2 Added with 80387
      • 4.2.3 Added with Pentium Pro
      • 4.2.4 Added with SSE
      • 4.2.5 Added with SSE3
    • 4.3 Undocumented instructions
  • 5 Further Reading

x87 Coprocessor

[edit | edit source]

The original x86 family members had a separate math coprocessor that handled floating point arithmetic.The original coprocessor was the 8087, and all FPUs since have been dubbed “x87”chips.Later variants integrated the FPU into the microprocessor itself.Having the capability to manage floating point numbers means a few things:

  1. The microprocessor must have space to store floating point numbers.
  2. The microprocessor must have instructions to manipulate floating point numbers.

The FPU, even when it is integrated into an x86 chip, is still called the “x87”section.For instance, literature on the subject will frequently call the FPU Register Stack the “x87 Stack”, and the FPU operations will frequently be called the “x87 instruction set”.

The presence of an integrated x87 FPU can be checked using the cpuid instruction.

; after you have verified; that the cpuid instruction is indeed available:mov eax, 1 ; argument request feature reportcpuidxor rax, rax ; wipe clean accumulator registerbt edx, rax ; CF ≔ edx[rax] retrieve bit 0setc al ; al ≔ CF

FPU Register Stack

[edit | edit source]

The FPU has an array of eight registers that can be accessed as a stack.There is one top index indicating the current top of the stack.Pushing or popping items to or from the stack will only change the top index and store or wipe data respectively.

st(0) or simply st refers to the register that is currently at the top of the stack.If eight values were stored on the stack, st(7) refers to last element on the stack (i. e. the bottom).

Numbers are pushed onto the stack from memory, and are popped off the stack back to memory.There is no instruction allowing to transfer values directly to or from ALU registers.The x87 stack can only be accessed by FPU instructions‒ you cannot write mov eax, st(0)‒ it is necessary to store values to memory if you want to print them, for example.

FPU instructions generally will pop the first two items off the stack, act on them, and push the answer back on to the top of the stack.

Floating point numbers may generally be either 32bits long, the float data type in the programming languageC, or 64bits long, double inC.However, in order to reduce round-off errors, the FPU stack registers are all 80bits wide.

Most calling conventions return floating point values in the st(0) register.

Examples

[edit | edit source]

The following program (using NASM syntax) calculates the square root of 123.45.

[org 0x7c00][bits 16]global _startsection .dataval: dq 123.45 ; define quadword (double precision)section .bssres: resq 1 ; reserve 1 quadword for resultsection .text_start: ;initilizes the FPU, avoids inconsistent behavior fninit; load value into st(0)fld qword [val] ; treat val as an address to a qword; compute square root of st(0) and store the result in st(0)fsqrt; store st(0) at res, and pop it off the x87 stackfstp qword [res]; the FPU stack is now empty again; end of program

Essentially, programs that use the FPU load values onto the stack with fld and its variants, perform operations on these values, then store them into memory with one of the forms of fst, most commonly fstp when you are done with x87, to clean up the x87 stack as required by most calling conventions.

Here is a more complex example that evaluates the Law of Cosines:

;; c^2 = a^2 + b^2 - cos(C)*2*a*b;; C is stored in angglobal _startsection .data a: dq 4.56 ;length of side a b: dq 7.89 ;length of side b ang: dq 1.5 ;opposite angle to side c (around 85.94 degrees)section .bss c: resq 1 ;the result ‒ length of side csection .text _start: fld qword [a] ;load a into st0 fmul st0, st0 ;st0 = a * a = a^2 fld qword [b] ;load b into st0 (pushing the a^2 result up to st1) fmul st0, st0 ;st0 = b * b = b^2, st1 = a^2 faddp ;add and pop, leaving st0 = old_st0 + old_st1 = a^2 + b^2. (st1 is freed / empty now) fld qword [ang] ;load angle into st0. (st1 = a^2 + b^2 which we'll leave alone until later) fcos ;st0 = cos(ang) fmul qword [a] ;st0 = cos(ang) * a fmul qword [b] ;st0 = cos(ang) * a * b fadd st0, st0 ;st0 = cos(ang) * a * b + cos(ang) * a * b = 2(cos(ang) * a * b) fsubp st1, st0 ;st1 = st1 - st0 = (a^2 + b^2) - (2 * a * b * cos(ang)) ;and pop st0 fsqrt ;take square root of st0 = c fstp qword [c] ;store st0 in c and pop, leaving the x87 stack empty again ‒ and we're done! ; don't forget to make an exit system call for your OS, ; or execution will fall off the end and decode whatever garbage bytes are next. mov eax, 1 ; __NR_exit xor ebx, ebx int 0x80 ; i386 Linux sys_exit(0) ;end program

Floating-Point Instruction Set

[edit | edit source]

You may notice that some of the instructions below differ from another in name by just one letter: a P appended to the end. This suffix signifies that in addition to performing the normal operation, they also Pop the x87 stack after execution is complete.

Original 8087 instructions

[edit | edit source]

FDISI, FENI, FLDENVW, FLDPI, FNCLEX, FNDISI, FNENI, FNINIT, FNSAVEW, FNSTENVW, FRSTORW, FSAVEW, FSTENVW

Data Transfer Instructions

[edit | edit source]

  • fld: load floating-point value
  • fild: load integer
  • fbld
  • fbstp
  • load a constant on top of the stack
    • fld1: x86 Assembly/Floating Point - Wikibooks, open books for an open world (1)
    • fldld2e: x86 Assembly/Floating Point - Wikibooks, open books for an open world (2)
    • fldl2t: x86 Assembly/Floating Point - Wikibooks, open books for an open world (3)
    • fldlg2: x86 Assembly/Floating Point - Wikibooks, open books for an open world (4)
    • flln2: x86 Assembly/Floating Point - Wikibooks, open books for an open world (5)
    • fldz: “positive” x86 Assembly/Floating Point - Wikibooks, open books for an open world (6)
  • fst, fstp
  • fist, fistp: store integer
  • fxch: exchange
  • fisttp: store a truncated integer

Arithmetic Instructions

[edit | edit source]

  • fabs: absolute value
  • fchs: change sign
  • fxtract: split exponent and significant
  • fadd, faddp, fiadd: addition
  • fsub, fsubp, fisub: subtraction
  • fsubr, fsubrp, fisubr: reverse subtraction
  • fmul, fmulp, fimul
  • fsqrt: square root
  • fdiv, fdivp, fidiv: division (see also fdiv bug on Wikipedia)
  • fdivr, fdivrp, fidivr
  • fprem: partial remainder
  • fptan
  • fpatan
  • frndint: round to integer
  • fscale: multiply/divide by integral powers of 2
  • f2xm1: x86 Assembly/Floating Point - Wikibooks, open books for an open world (7)
  • fyl2x: x86 Assembly/Floating Point - Wikibooks, open books for an open world (8)
  • fyl2xp1: x86 Assembly/Floating Point - Wikibooks, open books for an open world (9)

FPU Internal and Other Instructions

[edit | edit source]

  • finit: initialize FPU
  • fldcw
  • flenv
  • frstor
  • fsave, fnsave
  • fstcw, fnstcw
  • fstenv, fnstenv
  • fstsw, fnstsw
  • finccstp and fdecstp: increment or decrement top
  • ffree: tag a register as free
  • ftst: test
  • fcom, fcomp, fcompp: compare floating-point values
  • ficom, ficomp: compare with an integer
  • fxam: examine a register
  • fclex: clear exceptions
  • fnop
  • fwait does the same as wait.

Added in specific processors

[edit | edit source]

Added with 80287

[edit | edit source]

FSETPM

Added with 80387

[edit | edit source]

FCOS, FLDENVD, FNSAVED, FNSTENVD, FPREM1, FRSTORD, FSAVED, FSIN, FSINCOS, FSTENVD, FUCOM, FUCOMP, FUCOMPP

Added with Pentium Pro

[edit | edit source]

FCMOVB, FCMOVBE, FCMOVE, FCMOVNB, FCMOVNBE, FCMOVNE, FCMOVNU, FCMOVU, FCOMI, FCOMIP, FUCOMI, FUCOMIP, FXRSTOR, FXSAVE

Added with SSE

[edit | edit source]

FXRSTOR, FXSAVE

These are also supported on later Pentium IIs which do not contain SSE support

Added with SSE3

[edit | edit source]

FISTTP (x87 to integer conversion with truncation regardless of status word)

Undocumented instructions

[edit | edit source]

  • ffreep: performs ffree st(i) and pops the stack

Further Reading

[edit | edit source]

  • X86 Disassembly/Floating Point Numbers
  • Floating Point
x86 Assembly/Floating Point - Wikibooks, open books for an open world (2024)
Top Articles
How Long Does a Background Check Take in Florida?
Ohio Medical Marijuana Card | Cannabis Doctors | Dr. Green Relief
Kansas City Kansas Public Schools Educational Audiology Externship in Kansas City, KS for KCK public Schools
Hawkeye 2021 123Movies
Craigslist Kennewick Pasco Richland
Apply A Mudpack Crossword
House Share: What we learned living with strangers
WK Kellogg Co (KLG) Dividends
Fire Rescue 1 Login
2016 Hyundai Sonata Price, Value, Depreciation & Reviews | Kelley Blue Book
Games Like Mythic Manor
Jesus Calling Oct 27
Nba Rotogrinders Starting Lineups
Find Such That The Following Matrix Is Singular.
Aldine Isd Pay Scale 23-24
Craigslist In Visalia California
360 Tabc Answers
Craigslist List Albuquerque: Your Ultimate Guide to Buying, Selling, and Finding Everything - First Republic Craigslist
Puss In Boots: The Last Wish Showtimes Near Cinépolis Vista
Johnnie Walker Double Black Costco
Unionjobsclearinghouse
The Weather Channel Local Weather Forecast
Dtlr Duke St
Baja Boats For Sale On Craigslist
Munis Self Service Brockton
Airline Reception Meaning
Https E22 Ultipro Com Login Aspx
Panolian Batesville Ms Obituaries 2022
Accuradio Unblocked
800-695-2780
Maths Open Ref
Elijah Streams Videos
Vip Lounge Odu
Promatch Parts
Mkvcinemas Movies Free Download
Sports Clips Flowood Ms
Puerto Rico Pictures and Facts
Bay Focus
KITCHENAID Tilt-Head Stand Mixer Set 4.8L (Blue) + Balmuda The Pot (White) 5KSM175PSEIC | 31.33% Off | Central Online
Bbc Gahuzamiryango Live
Convenient Care Palmer Ma
Tyler Perry Marriage Counselor Play 123Movies
Lyndie Irons And Pat Tenore
FedEx Authorized ShipCenter - Edouard Pack And Ship at Cape Coral, FL - 2301 Del Prado Blvd Ste 690 33990
Killer Intelligence Center Download
Minterns German Shepherds
Yosemite Sam Hood Ornament
Shiftselect Carolinas
Westport gun shops close after confusion over governor's 'essential' business list
Bluebird Valuation Appraiser Login
What Is The Gcf Of 44J5K4 And 121J2K6
Suzanne Olsen Swift River
Latest Posts
Article information

Author: Fr. Dewey Fisher

Last Updated:

Views: 5773

Rating: 4.1 / 5 (42 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Fr. Dewey Fisher

Birthday: 1993-03-26

Address: 917 Hyun Views, Rogahnmouth, KY 91013-8827

Phone: +5938540192553

Job: Administration Developer

Hobby: Embroidery, Horseback riding, Juggling, Urban exploration, Skiing, Cycling, Handball

Introduction: My name is Fr. Dewey Fisher, I am a powerful, open, faithful, combative, spotless, faithful, fair person who loves writing and wants to share my knowledge and understanding with you.