Sign Extend, Zero Extend, Multiple, Divide

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *

Already have an account? Sign In »

13 hours 15 minutes
Video Transcription
Hello. This is Dr Miller, and this is Episode 3.1 of Assembly.
Today we're gonna talk about changing data sizes, multiplication and division
changing sizes.
So changing sizes that we want to take something and we want to either make it smaller or make it bigger.
So making it smaller means that we're going to just move it to a smaller register. And this could be converting from a word to a bite and removing the upper eight bits.
So if you remember, a X is composed of two smaller registers h a l,
and so inside of a X A l would have
34 hex and a H would have 12 hex.
So if we move into C l. A. H right, the number 12 in hex would get moved into CEO.
We're taking something a X, and we're moving it into a smaller ones. We just remove the bits that we don't need
if you want to increase the size that we have to care about things like the sign. So we've talked about two's complement,
and you should have the ability to write the number negative seven in two's complement using one bite.
You can take the time to do that, and we'll show the results on the next slide.
So negative seven is zero x f nine
and so we can move into the register bl the number negative seven. So again we have that top bit being our sign. And then we have two ways. In order to increase the size, we can either use sign extend so we know that we're using signed numbers. And so we want to extend the sign if needed,
or zero extend if we don't care about the sign and we just want to fill it with zeros.
So, for example, we move into bl negative seven. And then if we do move, sign, extend into a bigger register a x b l. Because this top bid is a one, remember, What if we convert F is 1111?
So it'll feel that all with ones and so e x will have
f f f f f f f nine
if we do the same thing into, say, X right, the number,
um negative seven
and we use sign extend again. It will fill it with zeros, but it will be in a X as opposed to e X, right, yea exes, 32 bits, a excess 16 bits.
And if the number is not signed. So, for example, the number 707 Then we're going to fill the bits with zeros. And so if I move with a sign, extend ye Xbl.
The number seven doesn't have the top bid set, and we're saying Philip zeros. If it's a zero and so that's what will happen
then we also have zero extent. So basically we say we don't care if the number is negative or not. We want to put zeros in it.
So if I do the same operation move bl negative seven again, it's f nine. If I move zero extend into yea X from B l, it will be
you have six zeros and then f nine. So the top zeros top bits are filled with zero
again I could move into This is a 32 bit register a bit.
This is a 16 bit register again, eight bits. And so this is how I can change any of my signs.
And then we get the same result on this 10 extender sign. Extend because
this number is positive, right? So it doesn't have that top bed set, so it would have filled it with zeroes anyway. But Xerox and guarantees it'll fill with zeros.
So arithmetic. Again. We've talked about two's complement, so this allows her bull signed and unsigned numbers.
And then we've mentioned briefly. We have these flags, and these flags will get set when you do some operations. So, for example, that carry flag.
So if I have to,
ah, 16 bit numbers zeros. So here's my 16 bit number 00 to see and my other 16 bit number F f f f
right. If these air not signed numbers, I either not stored in two's complement.
Then when I go ahead and add them together, right then I get a what we call carry or overflow. And so, if these were going into a 16 bit register than our carry flag gets set to say, the number we're trying to store is too big to store in a 16 bit register.
But if I were using a 32 bit register that I'm just taking two numbers and clearly this is not a negative number because all the rest of the top. It's or not set.
And so these two numbers air positive. And so this is my result of doing adding to positive numbers together in a 32 bit register.
You know, again, flags will get set. The carry flag is just one example
So for multiplication, we have two different op codes. So the 1st 1 is going to be mole, and this is for unsigned numbers, the numbers that are always positive
and we can use a eight bit register. So a L is implicit in here. And so we take the source times a l store, the result in a X. So if I do molby l that's gonna multiply bl times a l and store the result in a X.
If I have a 16 bit register,
then we're going toe implicitly multiply by X that register times the source that I pick. So in this example, see ex.
So in this example, I'll take C X times a X, and then I'll store the top 16 bits and D S. D. X and the bottom 16 bits and X.
And then if we have a 32 big register, we are again Mall ebx
Well, e x times a source so e x times ebx and then the top 32 bits gets stored in e. D. X and the bottom 32 bits get stored in E. X.
So here are a couple of examples. So for example, we do mull bl so e b x has to which implicit means bl has the number two So two times 10 is 20
we multiply those together and then we can see that are a result is 14 in hex which is the same as 20 in decimal
And this echo on we're multiplying by C x
And so I take yea x or a X right the number 10
multiplying that by c x three So I should get 30 And if you do that it is in here one e is our result
again Notice that so in the first example we only modified e x
or to be more precise, a X
in the 2nd 1 Remember, we're doing a 16 bit multiplication and so notice here how the beef disappeared. Right? So we took the beef in here and we set it to zero because the top bits of that were set.
So if I take three times 10
that gives me 30. And so 30 goes in the bottom part and then zeros get filled in the top.
And then if I do a full 32 bit multiplication so I take
yea X, which is God, Xerox A. And I multiply that by EBX. So 10 times two is 20 again, we got 14 which is 20 in hex. But then we erased all of the E. D. X, so feed beef disappeared.
So this is an example you can try on your own, so you can either try and do it longhand, or you can open up a terminal and enter your program and then print off the result and see what would change.
Now if we want to do signed multiple signed multiplication,
we use the op code. I'm all so it's gonna take either 2 32 bit numbers or a 32 bit number and a memory address or 32 minute to 32 bit numbers
and a immediate, and it's gonna multiply them together.
So, for example, if I do, I'm all yea x ebx.
Then e X gets the result of multiplying Yay, x times, ebx
Or if I do, I'm all Yes, I e d I 25 That's going to take e d I times 25 then store the result in yes, I
So here is an example. So if I do, I'm all yea x e bx,
it's gonna take e X, multiply it by EBX and then store the result in E X.
So again we have a negative number. So negative two times 10 is negative 20 and you should be able to using two's complement. Convert that and verify that it is the number negative. 20
So division is like multiplication. We have again unsigned with DIV. Or we have signed with idea
and then the results. So we have a quotient and remainder. So when you do division right, how many times that goes into that number is your quotient and then your remainder is what's left.
And so again, we have different
registers to get used, depending on if we're doing an eight bit or a 16 bit or a 32 bit.
So again we are going to take, for example, a X and divided by the source store that question in a L store, the remainder in a H. If we're doing 16 bit again, it's going to use the entire registers of E, D. X and E X put together and divide those by the source
and then start the question in a X and the remainder in DX.
And then if we have a 32 bit, we are using E, d X and yea X together and then dividing those by the source. And then the question gets stored in the X, and the remainder gets stored in E. D. X.
So in this I put an example. So here we have E X has 100 in decimal
and then EBX has seven and I just do division of ebx. So again, that's going to take E, d X and E X together as my number and then divided by
E B X. And so my remainder,
if we take ah 100 divided by seven is e. You can convert that
and then the remainder is too right. So we get the number 98 when we multiply seven times E and then we have a remainder of to
no in here, I put you need to zero ttx or else.
So if you forget to zeroed out and you run this code and you can type it in yourself, you will get a floating point exception because the result that it tried to store couldn't fit in. Yea, X Right. So when you're doing the division, we couldn't store that full thing in E X and so we get a floating point exception.
So this isn't a mistake that often my students make.
So today we talked about changing sizes. We talked about doing multiplication and doing division.
In the future, we're gonna learn about doing multiplication and division, given some actual examples of typing it in and then doing comparisons and doing looping.
If you have questions, you can contact me at Miller MJ at you and Keita et you. And you can find me on Twitter at Milhouse 30
Up Next