• Aucun résultat trouvé

From Decimal to Hex

Dans le document Assembly Language Step-by-Step (Page 65-68)

The lights should be coming on about now. This is good, because going in the other direction, from our familiar decimal base 10 to hex, ismuchharder and involves more math. What we have to do is find the hex column values

‘‘within’’ a decimal number—and that involves some considerable use of that fifth-grade bogeyman, long division.

But let’s get to it, again starting with a fairly easy decimal number: 449. The calculator will be handy with a vengeance. Tap in the number 449 and store it in the calculator’s memory.

What we need to do first is find the largest hex column value that is contained in 449 at leastonce. Remember grade-schoolgazintas? (12gazinta855 how many times?) Division is often introduced to students as a way of finding out how many times some number is present in—‘‘goes into’’—another. This is something like that. Looking back at Table 2-6, we can see that 256 is the largest power of 16, and hence the largest hex column value, that is present in 449 at least once. (The next largest power of 16—512—is obviously too large to be present in 449.)

Therefore, we start with 256, and determine how many times 256 ‘‘gazinta’’

449: 449 / 256 = 1.7539. At least once, but not quite twice, so 449 contains

30 Chapter 2 Alien Bases

only one 256. Write down a 1 on paper.Don’t enter it into your calculator.We’re not keeping a running total here; if anything, you could say we’re keeping a running remainder. The ‘‘1’’ is the leftmost hex digit of the hex value that is equivalent to decimal 449.

We know that only one 256 is contained in 449. What we must do now is remove that 256 from the original number, now that we’ve ‘‘counted’’ it by writing a 1 down on paper. Subtract 256 from 449. Store the difference, 193, into memory.

The 256 column has been removed from the number we’re converting. Now we move to the next column to the right, the 16s. How many 16s are contained in 193? 193 / 16=12.0625. This means the 16s column in the hex equivalent of 449 contains a. . . 12? Hmm. Remember the digit shortage, and the fact that in hex, the value we call 12 is represented by the letter C. From a hex perspective, we have found that the original number contains C in the 16s column. Write a C down to the right of your 1: 1C. So far, so good.

We’ve got the 16s column, so just as with the 256s, we have to remove the 16s from what’s left of the original number. The total value of the 16s column is C×16=12×16=192. Bring the 193 value out of your calculator’s memory, and subtract 192 from it. A lonely little 1 is all that’s left.

So we’re down to the units column. There is one unit in one, obviously.

Write that 1 down to the right of the C in our hexadecimal number: 1C1.

Decimal 449 is equivalent to hex 1C1.

Now perhaps you can appreciate why programmers like hexadecimal cal-culators so much.

Glance back at the big picture of the decimal-to-hex conversion. We’re looking for the hexadecimal columns hidden in the decimal value. We find the largest column contained in the decimal number, find that column’s value, and subtract that value from the decimal number. Then we look for the next smallest hex column, and the next smallest, and so on, removing the value of each column from the decimal number as we go. In a sense, we’re dividing the number by consecutively smaller powers of 16 and keeping a running remainder by removing each column as we tally it.

Let’s try it again. The secret number is 988,664:

1. Find the largest column contained in 988,664 from Table 2-6: 65,536.

988,664 / 65,536=15 and change. Ignore the change. 15=F in hex. Write down the F.

2. Remove F×65,536 from 988,664. Store the remainder: 5,624.

3. Move to the next smallest column. 5,624 / 4,096=1 and change. Write down the 1.

4. Remove 1× 4,096 from the remainder: 5,624 − 4096 = 1528. Store the new remainder: 1,528.

Chapter 2 Alien Bases 31 5. Move to the next smallest column. 1,528 / 256 = 5 and change. Write

down the 5.

6. Remove 5×256 from the stored remainder, 1,528. Store 248 as the new remainder.

7. Move to the next smallest column. 248 / 16 =15 and change. 15=F in hex. Write down the F.

8. Remove F × 16 from stored remainder, 248. The remainder, 8, is the number of units in the final column. Write down the 8.

There you have it: 988,664 decimal=F15F8H.

Note the presence of theHat the end of the hex number. From now on, every hex number in this book will have that H affixed to its hindparts. It’s important because noteveryhex number contains letter digits to scream out the fact that the number is in base 16. There is a 157H as surely as a 157 decimal, and the two arenotthe same number. (Quick, now: by how much are they different?) Don’t forget that H in writing your assembly programs, as I’ll be reminding you later.

Practice. Practice! PRACTICE!

The best (actually, the only) way to get a gut feel for hex notation is to use it a lot. Converteachof the following hex numbers to decimal. Lay each number out on the dissection table and identify how many 1s, how many 16s, how many 256s, how many 4,096s, and so on, are present in the number, and then add them up in decimal:

CCH 157H D8H BB29H 7AH 8177H A011H 99H 2B36H FACEH 8DB3H 9H

32 Chapter 2 Alien Bases

That done, now turn it inside out, and convert each of the following decimal numbers to hex. Remember the general method: from Table 2-6, choose the largest power of 16 that islessthan the decimal number to be converted. Find out how many times that power of 16 is present in the decimal number, and write it down as the leftmost hex digit of the converted number. Then subtract the total value represented by that hex digit from the decimal number. Repeat the process, using the next smallest power of 16, until you’ve subtracted the decimal number down to nothing.

39 413 22 67,349 6,992 41 1,117 44,919 12,331 124,217 91,198 307

112,374,777

(Extra credit for that last one.) If you need more practice, choose some decimal numbers and convert them to hex, and then convert them back. When you’re done, check your work with whatever hex-capable calculator that you prefer.

Dans le document Assembly Language Step-by-Step (Page 65-68)