 # Sign Extend, Zero Extend, Multiple, Divide

Video Activity
Join over 3 million cybersecurity professionals advancing their career
or Course
Time
13 hours 15 minutes
Difficulty
Beginner
CEU/CPE
14
Video Transcription
00:01
Hello. This is Dr Miller, and this is Episode 3.1 of Assembly.
00:06
Today we're gonna talk about changing data sizes, multiplication and division
00:12
changing sizes.
00:14
So changing sizes that we want to take something and we want to either make it smaller or make it bigger.
00:20
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.
00:31
So if you remember, a X is composed of two smaller registers h a l,
00:37
and so inside of a X A l would have
00:41
34 hex and a H would have 12 hex.
00:45
So if we move into C l. A. H right, the number 12 in hex would get moved into CEO.
00:52
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
01:03
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,
01:10
and you should have the ability to write the number negative seven in two's complement using one bite.
01:15
You can take the time to do that, and we'll show the results on the next slide.
01:22
So negative seven is zero x f nine
01:26
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,
01:44
or zero extend if we don't care about the sign and we just want to fill it with zeros.
01:49
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?
02:05
So it'll feel that all with ones and so e x will have
02:08
f f f f f f f nine
02:14
if we do the same thing into, say, X right, the number,
02:17
um negative seven
02:20
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.
02:32
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.
02:46
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
02:54
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.
03:00
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
03:10
you have six zeros and then f nine. So the top zeros top bits are filled with zero
03:17
again I could move into This is a 32 bit register a bit.
03:22
This is a 16 bit register again, eight bits. And so this is how I can change any of my signs.
03:28
And then we get the same result on this 10 extender sign. Extend because
03:32
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.
03:45
So arithmetic. Again. We've talked about two's complement, so this allows her bull signed and unsigned numbers.
03:51
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.
04:00
So if I have to,
04:02
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
04:13
right. If these air not signed numbers, I either not stored in two's complement.
04:17
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.
04:34
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.
04:44
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.
04:53
You know, again, flags will get set. The carry flag is just one example
05:00
multiplication.
05:02
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
05:12
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.
05:30
If I have a 16 bit register,
05:31
then we're going toe implicitly multiply by X that register times the source that I pick. So in this example, see ex.
05:39
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.
05:50
And then if we have a 32 big register, we are again Mall ebx
05:56
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.
06:09
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
06: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
06:29
And this echo on we're multiplying by C x
06:31
And so I take yea x or a X right the number 10
06:36
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
06:47
again Notice that so in the first example we only modified e x
06:54
or to be more precise, a X
06:56
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.
07:11
So if I take three times 10
07:13
that gives me 30. And so 30 goes in the bottom part and then zeros get filled in the top.
07:20
And then if I do a full 32 bit multiplication so I take
07:24
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.
07:44
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.
08:01
Now if we want to do signed multiple signed multiplication,
08:03
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
08:16
and a immediate, and it's gonna multiply them together.
08:20
So, for example, if I do, I'm all yea x ebx.
08:24
Then e X gets the result of multiplying Yay, x times, ebx
08:28
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
08:41
So here is an example. So if I do, I'm all yea x e bx,
08:45
it's gonna take e X, multiply it by EBX and then store the result in E X.
08:50
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
09:03
division.
09:07
So division is like multiplication. We have again unsigned with DIV. Or we have signed with idea
09:13
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.
09:22
And so again, we have different
09:26
registers to get used, depending on if we're doing an eight bit or a 16 bit or a 32 bit.
09:33
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
09:48
and then start the question in a X and the remainder in DX.
09:54
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.
10:07
So in this I put an example. So here we have E X has 100 in decimal
10:15
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
10:26
E B X. And so my remainder,
10:30
if we take ah 100 divided by seven is e. You can convert that
10:35
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
10:46
no in here, I put you need to zero ttx or else.
10:50
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.
11:09
So this isn't a mistake that often my students make.
11:15
So today we talked about changing sizes. We talked about doing multiplication and doing division.
11:22
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.
11:33
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