Chapter 16 by gegouzhen12

VIEWS: 0 PAGES: 19

									          Assembly Language for Intel-Based
               Computers, 5th Edition
                                              Kip R. Irvine


                   Floating-Point Processing for
                          Assignment One



By Sai-Keung Wong, NCTU



(c) Pearson Education, 2006-2007. All rights reserved. You may modify and copy this slide show for your personal use,
or for use in the classroom, as long as this copyright statement, the author's name, and the title are not changed.
                                         FPU Register Stack

      • Eight individually addressable 80-bit data registers named R0
        through R7
      • Three-bit field named TOP in the FPU status word identifies
        the register number that is currently the top of stack.




Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.   Web site   Examples   2
                         Load A Floating Point Number
.data
 myFloat01 REAL8 3.141592654
                                                                                              ST(7)
                                                                                              ST(6)
.code                                                                                         ST(5)
                                                                                              ST(4)
finit                                                                                         ST(3)
                                                                                              ST(2)
                                                                                              ST(1)
                                                                                              ST(0)
;where is this number stored?
                                                                                                     TOP

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.   Web site   Examples      3
                         Load A Floating Point Number
.data
 myFloat01 REAL8 3.141592654
                                                                                              ST(7)
                                                                                              ST(6)
.code                                                                                         ST(5)
                                                                                              ST(4)
finit                                                                                         ST(3)
fld myFloat01                                                                                 ST(2)
                                                                                              ST(1)
                                                                             3.141592654      ST(0)


;where is this number stored?                                                                        TOP

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.   Web site   Examples      4
                                         Load Two Numbers
.data
myFloat01 REAL8 3.141592654
                                                                                              ST(7)
myFloat02 REAL8 2.781281828                                                                   ST(6)
                                                                                              ST(5)
.code                                                                                         ST(4)
                                                                                              ST(3)
finit                                                                                         ST(2)
fld myFloat01                                                               3.141592654       ST(1)
fld myFloat02                                                               2.781281828       ST(0)


; Where are these two numbers stored?                                                                TOP

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.   Web site   Examples      5
                                      Load Three Numbers
.data
myFloat01 REAL8 3.141592654
myFloat02 REAL8 2.781281828                                                                   ST(7)
                                                                                              ST(6)
m_one     REAL8 -1.0
                                                                                              ST(5)
.code
                                                                                              ST(4)
                                                                                              ST(3)
finit                                                                        3.141592654      ST(2)
fld myFloat01                                                                2.781281828      ST(1)
fld myFloat02                                                               -1.0              ST(0)
fld m_one

                                                                                                     TOP
; Where are these three numbers stored?
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.   Web site   Examples      6
                               Display a number in ST(0)
      .data

      v REAL8                         -1.0

      .code

      finit
      fld v
      call WriteFloat




Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.   Web site   Examples   7
                                 Store ST(0) into memory
      .data
      v                               REAL8                             ?
      fArray                          REAL8                             99 DUP(?)

      .code
      fst   v                                                           ;direct addressing

      mov esi, offset fArray                                            ;indirect addressing
      fst REAL8 PTR [esi]
      fst REAL8 PTR [esi + 8]


Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.         Web site   Examples   8
                                          Add Two Numbers
.data
v0 REAL8                      -1.0
v1 REAL8                      -2.0                                                            ST(7)
                                                                                              ST(6)
.code
                                                                                              ST(5)
                                                                                              ST(4)
finit                                                                                         ST(3)
fld v0                                                                                        ST(2)
fld v1                                                                       -1.0             ST(1)
fadd ST(0), ST(1)                                                            -3.0             ST(0)

; where is the result stored?
                                                                                                     TOP

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.   Web site   Examples      9
                                 Product of Two Numbers
.data
v0 REAL8                      -1.0
v1 REAL8                      -2.0                                                            ST(7)
                                                                                              ST(6)
.code
                                                                                              ST(5)
                                                                                              ST(4)
finit                                                                                         ST(3)
fld v0                                                                                        ST(2)
fld v1                                                                       -1.0             ST(1)
fmul ST(0), ST(1)                                                            2.0              ST(0)

; where is the result stored?
                                                                                                     TOP

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.   Web site   Examples     10
                                                                   1/n
.data
one REAL8                                      1.0
n      REAL8                                   128.123
result REAL8                                   ?

.code
mov esi, offset n
mov edi, result
fld one
fdiv REAL8 PTR [esi]                                           ; ST(0) = ST(0) / REAL8 PTR [esi]
fst REAL8 PTR [edi]                                            ; store ST(0) to REAL PTR [edi]

Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.    Web site   Examples   11
Generation the numbers between 0…N.

Convert them into floating numbers and
        store them in an array.

                 e.g.

                 N = 10
      0.0, 1.0, 2.0, 3.0, …, 10.0.


                         Web site   Examples
.data
fArray         REAL8        1000 DUP(?)
one            REAL8        1.0       Generation the
zero           REAL8        0.0    numbers between
.code
                                               0…N
;assume ecx = N
       inc ecx                                       ST(7)
                                                     ST(6)
       mov esi, offset fArray
                                                     ST(5)
       finit
                                                     ST(4)
       fld one                                       ST(3)
       fld zero                                      ST(2)
L0:    fst REAL8 PTR [esi]                 1.0       ST(1)
       fadd ST(0), ST(1)                   0.0       ST(0)
       add esi, 8
       loop L0                     Web site Examples  13
                                     Factorial of A Number


      0! = 1
      1! = 1
      2! = 1 x 2
      3! = 1x2x3
      4! = 1x2x3x4
      …
      N! = ?




Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.   Web site   Examples   14
                                     Factorial of A Number


      0! = 1
      1! = 1
      2! = 1 x 2
      3! = 1x2x3
                                                                        N     0         1   2      3   4    …
      4! = 1x2x3x4
      …
                                                                        N!    1         1   ?      ?   ?    …
      N! = (N-1)! x N




Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.        Web site   Examples           15
.data
fArray                    REAL8 1000 DUP(?)
ftArray                   REAL8 1, 1, 1000 DUP(?)
.code                                             Compute the factorials
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;assume ecx = N
                                            for the numbers between 0…N
             inc ecx
             mov esi, offset fArray
             mov edi, offset ftArray                             esi
             add edi, 8


                                                                 …
             add esi, 8*2
      L1: finit                               fArray 0 1 2 3 4
             fld REAL8 PTR [esi]
             fld REAL8 PTR [edi]
             fmul ST(0), ST(1)               ftArray 1 1 ? ? ?   …
             fst REAL8 PTR [edi+8]
             add esi, 8
             add edi, 8                                     edi
             loop L1                                Web site Examples 16
.data
fArray         REAL8 1000 DUP(?)
                                                               Error?
ftArray        REAL8    1.0, 1.0, 1000 DUP(?)
.code
          ……
                                       Compute the factorials
          inc ecx????
                                  for the numbers between 0…N
       mov esi, offset fArray                              esi
       mov edi, offset ftArray


                                                                          …
       add edi, 8
       add esi, 8*2                 fArray 0         1     2     3   4
   L1: finit
       fld REAL8 PTR [esi]
       fld REAL8 PTR [edi]         ftArray 1         1     ?     ?   ?    …
       fmul ST(0), ST(1)
       fst REAL8 PTR [edi+8]
       add esi, 8                                    edi
       add edi, 8
                                          Web site    Examples           17
       loop L1
.data
fArray                    REAL8 1000 DUP(?)
                                                                       Compute 1/N!
ftArray                   REAL8 1, 1, 1000 DUP(?)
reciprocal                REAL8 1000 DUP(?)
one                       REAL8 1
.code
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;assume that ecx is set properly
             mov esi, offset ftArray
                                                                 esi
             mov edi, offset reciprocal
 L2:         finit
             fld one                                       ftArray 1 1 2 6 24                     …
                                                                                                  …
             fdiv REAL8 PTR [esi]
             fst REAL8 PTR [edi]
                                                       reciprocal ? ? ? ? ?
             add esi, 8
             add edi, 8
             loop L2
                                                                   edi
   Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.   Web site   Examples   18
      • Read                          CMP, JE and JNE, etc.




Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.   Web site   Examples   19

								
To top