Hello. This is Dr Miller, and this is Episode 8.4 of Assembly.
Today, we're going to show some comparison gone wrong with floating points, and then we're going to give an example of using floating point comparisons.
So comparison gone wrong.
So here is a example. See program where we have a floating point number. So float X,
we go ahead and read that in using scan F, and you can use a percent f 4%. LF,
um, here and then we're trying to just compare this to the number 100.1.
And so we're checking for equality to see if they're equal and it should print good if they're equal. And you can see here that I entered the number 100.1 which went into that scan f
but that when it printed it off, it was 100.9998
And so one of the things we will not want to do is we don't want to do direct comparison because, as you can see that sometimes
because of the way the floating point is stored, that it may go wrong. And so it's something that we may not want to do
so when we're doing floating point comparisons, some do's and don't so again, avoid comparing for equality
and along that line, either used greater than or equal to or less than or equal to if you can write, and so you can
try and trap it with a certain condition if you want. But just trying to use greater than or less than is the best way and then make sure you read the right size. So, for example, percent LF is a safe one to read in any kind of floating point number
conversions will will get done for you.
And then, if you're using imagers, make sure use the eye version of the floating point command,
so that will help you get the right value in, because that's not often a mistake that happens.
So we're going to read in a number, will print it off to make sure it's right, and then we'll compare it to a specific value, and then we'll have some
if statements that were going to incorporate.
So I've already got it set up
so the top will do again as typical. Our definition of some of the strings that we might need.
So our SCANA format is going to be percent.
LF for floating point, make sure you put a zero. So it knows with into the string is
we'll have an okay string.
Make sure terminate my string. I'll put a new line in and then null terminate that
we'll have one called
again, followed by a new line and a zero terminated.
Then we're gonna have a couple numbers who have X and Y,
and I'm using keywords for that. So at 64 bits,
you can pick any number here that you want, and then I'm just giving an initial value. Two x. I don't have to do that,
but it makes sure that I'm actually reading in the value. So when I printed off, you'll be a bro. See that
excess changed from this 99.9 to something different.
Didn't my print F format
again? You can pick anything you want. I'll just put a percent, um, f for floating here.
Terminate my quotes, put a new line in a zero inside of there,
and then we're gonna be using print off and scan F
to make sure we declare those his ex turns.
All right, so now we're going to want to scan our number in,
so we have to put the pointer on for where x is. So we're going to say push X.
We can push the s format for the scan F
We can call
then we pushed to things on, so we'll correct our stack.
Adding a TSB.
And so now we want to print the number,
and so again, it's gonna want 64 bits on the stack. So I've gotto
push additional data on. So
move in T A x X plus force. This gets the top four bites
value of X.
Push that onto the stacks. I pushed to things.
I'm gonna push my print format
and then I'm gonna have to correct the stack by 12. So I pushed three things on.
Here we go.
All right, So now I want to do my comparison here,
so I need to do a floating point load. I am using a cue word. That was the size that I defined in the value that I'm getting is X.
Same thing for why
now I'm gonna put some comments here. So X is S t one because it was pushed on, It started off SD zero and then we pushed on Why? So now why is S t zero
and then we'll use our comparison. So f comp I and then p for pop
and we'll compare s t zero tost one
we'll do it just a pop to get rid of that second number. Right? So this pops off one of the numbers, which would be why this will pop off X so that our stack is back clean again
and then we can do our jump, so jump above, we'll go to our equal label.
So let's define equal.
So here I'm just going to use the print strings and movie a X
okay, call print
and I'll just jump
to done. So I'll put a label down here
So we jumped if they were above, If they're equal, and then we can just do jump, unconditional jump. So if you could do jump less than or equal to,
um the jump above will do the greater so this will automatically jump to
moving your ex
and then John
All right, So the main part here was that when we did our come hair put a comment here. Let's see.
um, why is
All right, So if why is bigger than X, then we're gonna go and jump. Because why is s T zero?
All right, we'll see if I got it right here.
All right, 34.
And I think I put an extra letter in here. Ah, there we go.
An extra p.
So if I put 1.234
um, we try, that's equal, right? And so it
I won't do that. It's not above. But if I do 100
or if I do too
one so one is below
It's probably still below, but 1.4 is not gonna be below.
So today I showed an example, and we did some floating point comparisons, and we learned some of the sort of do's and don't
making sure that you don't try and check for equality because that is due to the way floating points of store. It's not gonna work right.
So just to make sure that you keep track of that when you're trying to do your comparisons
looking forward, we're gonna have an example where we try and reading three numbers and figure out what the maximum of those is.
And then we'll also start looking at the arm architecture and future lectures. And so, seeing how that's a little bit different than the X 86 architecture that we've been looking at,
if you have questions, you can email me at Miller, MJ at you and Katie you and you can find me on Twitter at Milhouse 30.