Floating Point / Quadratic Equation
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
This example of using the floating point coprocessor is an implementation of the quadratic equation. You should be familiar with this. Any quadratic equation can be written in this form. That is, it can be set equal to zero and a standard equation can be used to solve it. There are some special situations that the program has to watch out for. First, you need to know there are normally two solutions; that's because of the square root which, as you know, can be either positive or negative so the values of the equation has to be calculated once with the square root value added and again with it subtracted. The other situation comes up when the value inside the square root is negative. That means the solutions are imaginary numbers. Now, the algorithm presented here doesn't deal with imaginary numbers. It just quits. Let me start out by showing you how the function is called. Inside a loop the three values A, B and C are prompted for. Then the function is called with the three values and with two addresses of the locations that are to receive the results. If real solutions exist, the function returns non-zero and the two values are displayed. Now for the function itself. At the very top are some defines. Notice that the names being defined refer to the stack on which the argument values are to be found. Now, doing it this way allows the code to refer to the arguments by names and reduces the likelihood of reference mistakes. These last two defines are for variables in the work area. For convenience, the value of the square root and the reciprocal of 2A are stored in temporary variables for later use. Notice that the working storage is defined as being on the stack also. There are no local working variables. Doing it this way makes the function thread safe. That is, in its middle the function can be interrupted and called by someone else and each call will have its own work area. The function begins with the standard preamble code. Notice that the value of the ESP Register is adjusted by 16. That's the amount of space needed for those two local variables. A one constant is included inside the equation so space is set aside for it and it's defined here. Now for the body of the function itself. The function begins by pushing some convenient values on the stack. Oh, by the way, that's what the comments all do. They show you what's on the stack. When you write code for the floating point coprocessor, your first step is to convert your equation to post Polish notation, which is a form that lends itself to direct stack processing. Post Polish order is simply number or numbers followed by the operations that are to be performed on the numbers. This code is all annotated with comments listing the contents of the stack so it should be easy to follow. At this point the stack contains the value inside the square root operator and if it's negative, there will be no real solutions so the comparison here is done with the number zero. The resulting flags are copied into the flag register. This sets the condition codes according to the results of the floating point operation. The jump commands are the same one you would use for unsigned integer compares. In this case, if it is below zero, a jump is made because there are no real solutions to the equation. So now we know we're going to have a solution. The number is positive so we can take its square root. The results of the square root are stored in the work area until it's needed later. It's easier to store it away like this because it's going to be needed twice. Alternatively, it could be pushed lower onto the stack and if it were going to be used only once, that might be easier. There are dozens of ways to organize this algorithm. For instance, one over 2A is stored rather than just 2A. And that means that the operation for using it is multiply instead of divide. With these two instructions, the address of the variable to receive the first solution is copied into the EAX Register. Then the value is stored in that location. After the second solution is calculated, it's stored the same way. The return value of one is stored in the EAX Register to inform the caller of the presence of the solutions. Then all that's left to do is reverse the actions taken on the stack at the beginning of the function and return. You need to enter the three values to run it. To get a solution, it's most common to use negative numbers for either A or C to keep that radical positive. That's because with both of them the same sign, it's easy to wind up in a situation with no solutions.
Tutorial Information
| Course: | Assembly Language Programming |
| Author: | Arthur Griffith |
| SKU: | 33995 |
| ISBN: | 1-935320-44-0 |
| Release Date: | 2009-05-28 |
| Duration: | 5.5 hrs / 70 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 