2

This example was written in NASM:

section .bss
    var28: resb  28


section .text
    _main:

        ; Initialize
            finit
            fldpi
        ; Read Tag Word
            fstenv [var28]
            mov    ax, [var28 + 8] ; move the Tag Word to ax

At this point ax = 0011 1111 1111 1111, which means ST7 = 00 (valid), and the rest is 11 (empty).

The rest of the code:

        ; FFREE ST(i)

            ffree ST7 ; Sets tag for ST(i) to empty.
            ; Read Tag Word
                fstenv [var28]
                mov    ax, [var28 + 8] ; move the Tag Word to ax

At this point ax = 0011 1111 1111 1111 too.
My question is, shouldn't be ax = 1111 1111 1111 1111?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Bite Bytes
  • 1,455
  • 8
  • 24
  • `FFREE` is only useful when combined with either `FDECSTP` or `FINCSTP`. The most common combination is `FFREE`+`FINCSTP`, which is equivalent to (and less efficient than) `FSTP`. I have yet to find a good reason to use `FFREE`. – Cody Gray - on strike Jun 18 '17 at 11:17
  • @CodyGray I saw that post, it has nothing to do with my question. My question is more detailed and it's unfair to mark it as a duplicate for that post. – Bite Bytes Jun 18 '17 at 15:01
  • @CodyGray because people will go to the other question, considering it to be "original", though, it doesn't have as much details as this one. – Bite Bytes Jun 19 '17 at 16:04
  • @CodyGray: The old question didn't have a great answer (link + not much detail, and the notation used in the question didn't look very clear to me), so it worked better to mark it as a duplicate of this. Both the question and answer here are well-written. – Peter Cordes Jun 28 '17 at 05:30
  • Agreed, @Peter. This is a better Q&A pair, so it makes sense to reverse the direction. Thanks for being so on top of the duplicate situation in this tag. One of these days, maybe I'll earn a gold tag badge in [assembly], too. :-) – Cody Gray - on strike Jun 28 '17 at 10:55
  • 2
    @CodyGray: I'm not as active as I used to be. I got burned out after seeing the same beginner questions multiple times after a couple years. – Peter Cordes Jun 28 '17 at 21:00

1 Answers1

4

At this point ax = 0011 1111 1111 1111, which means ST7 = 00 (valid), and the rest is 11 (empty).

No. The Tag Word refers to the registers (R7..R0), while ST(i) refers to the "top of the stack" (TOS) which can change.

The first fldpi sets the TOS (=ST(0)) to register R7 and loads PI into that register. A second fld would change the TOS to register R6 and fill that register. ST(0) would point then to the register of the second fld. ffree st0 would free R6 (the second tag in the Tag Word) and set ST0 to R7. The Status Word contains a three-bit number with the register to which the TOS currently points.

In your example program, fldpi loads PI into ST(0) which points to R7. To empty R7 you have to use ffree st0.

Please take a look to Chapter 8 of the Intel Manual Vol. 1 where it is discussed in detail.

rkhb
  • 14,159
  • 7
  • 32
  • 60