Asembly Language
Asembly Language
Assembly Languages
Floating-Point Processing
Pu-Jen Cheng
Adapted from the slides prepared by Kip Irvine for the book,
Assembly Language for Intel-Based Computers, 5th Ed.
And prepared by Behrooz Parhami for the book, Computer
Architecture, 2005
Outline
rtnei(x) rtni(x)
4 4
3 3
2 2
1 1
x x
–4 –3 –2 –1 1 2 3 4 –4 –3 –2 –1 1 2 3 4
–1 –1
–2 –2
–3 –3
–4 –4
3 3
2 2
1 1
x x
–4 –3 –2 –1 1 2 3 4 –4 –3 –2 –1 1 2 3 4
–1 –1
–2 –2
–3 –3
–4 –4
(a) Round inward to nearest integer (b) Round upward to nearest integer
Floating-Point Exceptions
Six types of exception conditions
¾ Invalid operation
¾ Divide by zero
¾ Denormalized operand
¾ Numeric overflow (or underflow)
¾ Inexact precision
Each has a corresponding mask bit
¾ if set when an exception occurs, the exception is handled
automatically by FPU
¾ if clear when an exception occurs, a software exception handler is
invoked
FPU Instruction Set
Instruction mnemonics begin with letter F
Second letter identifies data type of memory
operand
¾ B = bcd
¾ I = integer
i t
¾ no letter: floating point
Examples
¾ FBLD load binary coded decimal
¾ FISTP store integer and pop stack
¾ FMUL multiply floating-point operands
FPU Instruction Set
Operands
¾ zero, one, or two
¾ no immediate operands
¾ no general-purpose registers (EAX, EBX, ...)
¾ integers must be loaded from memory onto the stack
and converted to floating-point before being used in
calculations
¾ if an instruction has two operands, one must be a FPU
register
FP Instruction Set
Data Types
Load Floating-Point Value
FLD
¾ Copies floating point operand from memory into the top
Example
FSTP
¾ pops the stack after copying
Arithmetic Instructions
Same operand types as FLD and FST
Floating-Point Add
FADD
¾ Adds source to destination
¾ No-operand version pops the FPU stack after
adding (ST(1) = ST(0)+ST(1); pop ST(0))
Examples:
FDIV
¾ Divides destination by source,
then pops the stack
.code
; if( val2 == val3 ), display "Values are equal".
fld epsilon
fld val2
fsub val3
fabs
fcomi ST(0),ST(1)
ja skip
mWrite <"Values are equal",0dh,0ah>
skip:
Exception Synchronization
Main CPU and FPU can execute instructions concurrently
¾ if an unmasked exception occurs, the current FPU instruction is
interrupted and the FPU signals an exception
¾ But the main CPU does not check for pending FPU exceptions. It
might use a memory value that the interrupted FPU instruction was
supposed
pp to set.
¾ Example:
.data
intVal DWORD 25
.code
fild intVal ; load integer into ST(0)
inc intVal ; increment the integer
Exception Synchronization
(continued)
For safety, insert a fwait instruction, which tells the CPU to
wait for the FPU's exception handler to finish:
.data
intVal DWORD 25
.code
code
fild intVal ; load integer into ST(0)
fwait ; wait for pending exceptions
inc intVal ; increment the integer
FPU Code Example
.data
valA REAL8 1.5
valB REAL8 2.5
valC REAL8 3.0
valD REAL8 ? ; will be +6.0
.code
fld valA ; ST(0) = valA
fchs ; change sign of ST(0)
fld valB ; load valB into ST(0)
fmul valC ; ST(0) *= valC
fadd ; ST(0) += ST(1)
fstp valD ; store ST(0) to valD
Mixed-Mode Arithmetic
Combining integers and reals.
¾ Integer arithmetic instructions such as ADD and MUL cannot handle
reals
¾ FPU has instructions that promote integers to reals and load the
values onto the floating point stack.
Example:
.data
data
N SDWORD 20
X REAL8 3.5
Z REAL8 ?
Y SDWORD ?
.code
fild N ; load integer into ST(0)
fadd X ; add mem to ST(0)
fist Y ; store ST(0) to mem int Y = N + X
fstp Z ; store ST(0) to mem real8 Z = N + X
Changing the Rounding Mode
The RC field (bits 11 and 10) of the FPU Control Word
determines how the FPU will round results:
¾ 00 = Round to nearest, or to even if equidistant (initialized state)
01 = Round down (toward -infinity)
10 = Round up (toward +infinity)
11 = Truncate ((toward 0))
.data
N SDWORD 20
X REAL8 3.5
Z REAL8 ?
ctrlWord WORD ?
.code
fstcw ctrlWord ; store control word
or ctrlWord, 110000000000b ; set RC = truncate
fldcw ctrlWord, ctrlWord ; load control word
fild N ; load integer into ST(0)
fadd X ; add mem to ST(0)
fist Y ; store ST(0) to mem int Y = 23
Masking and Unmasking Exceptions
Exceptions are masked by default
¾ Divide by zero just generates infinity, without halting the
program
.data
ctrlWord WORD ?
val1 DWORD 1
val2 REAL8 0.0
.code
fild val1 ; load int into ST(0)
fdiv val2 ; ST(0) = positive infinity
fstcw ctrlWord ; get the control word
and ctrlWord,1111111111111011b ; unmask divide by zero
fldcw ctrlWord ; load it back into FPU
fild val1 ; load int into ST(0)
fdiv val2 ; MS Windows will show exception
Floating-Point Addition
U np a c k
S ig ns E xp o n e n ts S ig n ific an ds
Ad d ′S u b
Addition A lig n
C o n tr o l s ig nific a nd s
& s ig n
lo g ic
A dd
N o rm a lize
& ro u nd
±
S ig n E xp o n e n t S ig n ific an d
P ack
O u tp ut