Floating Point Comparison Example
Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or
Already have an account? Sign In »

Video Transcription
00:01
Hello. This is Dr Miller, and this is Episode 8.4 of Assembly.
00:06
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.
00:15
So comparison gone wrong.
00:17
So here is a example. See program where we have a floating point number. So float X,
00:23
we go ahead and read that in using scan F, and you can use a percent f 4%. LF,
00:28
um, here and then we're trying to just compare this to the number 100.1.
00:34
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
00:45
but that when it printed it off, it was 100.9998
00:50
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
00:58
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
01:04
so when we're doing floating point comparisons, some do's and don't so again, avoid comparing for equality
01:11
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
01:19
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
01:34
conversions will will get done for you.
01:38
And then, if you're using imagers, make sure use the eye version of the floating point command,
01:42
so that will help you get the right value in, because that's not often a mistake that happens.
01:49
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
01:57
if statements that were going to incorporate.
02:01
So I've already got it set up
02:07
so the top will do again as typical. Our definition of some of the strings that we might need.
02:19
So our SCANA format is going to be percent.
02:23
LF for floating point, make sure you put a zero. So it knows with into the string is
02:29
we'll have an okay string.
02:37
Make sure terminate my string. I'll put a new line in and then null terminate that
02:43
we'll have one called
02:45
bad
02:50
again, followed by a new line and a zero terminated.
02:53
Then we're gonna have a couple numbers who have X and Y,
02:58
and I'm using keywords for that. So at 64 bits,
03:04
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,
03:12
but it makes sure that I'm actually reading in the value. So when I printed off, you'll be a bro. See that
03:16
excess changed from this 99.9 to something different.
03:23
Didn't my print F format
03:24
again? You can pick anything you want. I'll just put a percent, um, f for floating here.
03:32
Terminate my quotes, put a new line in a zero inside of there,
03:38
and then we're gonna be using print off and scan F
03:43
to make sure we declare those his ex turns.
03:50
All right, so now we're going to want to scan our number in,
03:54
so we have to put the pointer on for where x is. So we're going to say push X.
04:00
We can push the s format for the scan F
04:04
We can call
04:06
scan if
04:09
then we pushed to things on, so we'll correct our stack.
04:14
Adding a TSB.
04:15
And so now we want to print the number,
04:17
and so again, it's gonna want 64 bits on the stack. So I've gotto
04:21
push additional data on. So
04:26
move in T A x X plus force. This gets the top four bites
04:32
Bushie X
04:34
movie X
04:40
value of X.
04:42
Push that onto the stacks. I pushed to things.
04:45
I'm gonna push my print format
04:50
and then I'm gonna have to correct the stack by 12. So I pushed three things on.
05:00
Here we go.
05:01
All right, So now I want to do my comparison here,
05:06
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.
05:19
Same thing for why
05:21
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
05:34
and then we'll use our comparison. So f comp I and then p for pop
05:40
and we'll compare s t zero tost one
05:44
and then
05:46
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
05:56
and then we can do our jump, so jump above, we'll go to our equal label.
06:02
So let's define equal.
06:08
So here I'm just going to use the print strings and movie a X
06:12
okay, call print
06:18
thing,
06:20
and I'll just jump
06:25
to done. So I'll put a label down here
06:30
done
06:32
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,
06:44
um the jump above will do the greater so this will automatically jump to
06:49
not
06:51
a of
07:00
moving your ex
07:02
and
07:04
call print
07:06
string
07:09
and then John
07:13
done.
07:15
All right, So the main part here was that when we did our come hair put a comment here. Let's see.
07:23
So,
07:24
um, why is
07:27
bigger
07:31
on X?
07:34
Take
07:35
jump?
07:39
All right, So if why is bigger than X, then we're gonna go and jump. Because why is s T zero?
07:46
All right, we'll see if I got it right here.
07:50
All right, 34.
07:57
And I think I put an extra letter in here. Ah, there we go.
08:01
An extra p.
08:05
All right.
08:09
So if I put 1.234
08:13
um, we try, that's equal, right? And so it
08:16
I won't do that. It's not above. But if I do 100
08:22
or if I do too
08:24
one so one is below
08:28
1.233
08:31
is below,
08:33
um, 1.2
08:37
23
08:39
39
08:41
It's probably still below, but 1.4 is not gonna be below.
08:50
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
08:58
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.
09:07
So just to make sure that you keep track of that when you're trying to do your comparisons
09:13
looking forward, we're gonna have an example where we try and reading three numbers and figure out what the maximum of those is.
09:20
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,
09:30
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.
Up Next
Similar Content