UMBC CMSC 313 -- Assembly Language Segment Previous | Next


The FPU Stack

The Intel manuals says: "The x87 Floating-Point Unit (FPU) provides high-performance floating-point processing capabilities for use in graphics processing, scientific, engineering, and business applications. It supports the floating-point, integer, and packed BCD integer data types and the floating-point processing algorithms and exception handling architecture defined in the IEEE Standard 754 for Binary Floating-Point Arithmetic."

What is important to us is that there are 8 data registers, which are treated as a stack. The top of the stack is referred to as ST0 and things are pushed and popped onto the stack. Actually, the top is ST0, the next one down is ST1, then ST2. Whenever something is put on the stack, the new item becomes ST0, the old ST0 becomes ST1, etc. When something comes off the stack, the old ST1 becomes ST0, the old ST2 becomes ST1, etc. Load operations push a value on top of the stack. Store operates pop a value of the stack. Well, sort of! There are load and store intstructions that do not do a push or pop.

Example

The following program has the effects of all operations shown as comments:

	
section .data
fnr1	dd	1.2
fnr2	dd	2.3
fnr3	dd 	3.4
outstr	db	'result is %f',10,0

section .bss

qnr1	resq	1

section .text
    global main                       ;must be declared for linker (ld)
;; just like main in C -- if linking with gcc, must be main, otherwise does not have to.

main: 
                                ;tell linker entry point
extern	printf

;; put your code here

	finit				;  Initialize the FPU and clear errors

	fld	dword [ fnr1 ]          ; st0 :  1.2000000476837158203125

	fld	dword [ fnr2 ]          ; st0 :  2.2999999523162841796875
                                        ; st1 :  1.2000000476837158203125

	fadd    st1	                ; st0 :  3.5
                                        ; st1 :  1.2000000476837158203125

	fld	dword [ fnr3 ]          ; st0 :  3.400000095367431640625
                                        ; st1 :  3.5
                                        ; st2 :  1.2000000476837158203125

	fadd	st1	                ; st0 :  6.900000095367431640625
                                        ; st1 :  3.5
                                        ; st2 :  1.2000000476837158203125

	fstp	qword [ qnr1 ]          ; st0 :  3.5
                                        ; st1 :  1.2000000476837158203125

	
	push	dword [ qnr1 +4]
	push	dword [ qnr1 ]
	push	outstr
	call	printf
	add	esp, 8

;; The final part of the program must be a call to the operating system to exit
;;; the program.
        mov     ebx,0   ;successful termination of program
        mov     eax,1   ;system call number (sys_exit)
        int     0x80    ;call kernel


Previous | Next

©2004, Gary L. Burt