The text before

Today is the first day to study, as expected no pressure is not the same ah!! Go to bed at 12:30, and then if it is during the postgraduate entrance examination, eight o ‘clock is the last to get up, but today more than eight o ‘clock wake up, and then head to continue to sleep, wake up to see “10:30”, heartache! I am a man without a morning!!


The body of the

1, add

Computers perform binary addition, and there is also the concept of carry. Then for ALU (arithmetic logic unit), when two add several symbols in the same time, there is high risk of overflow, so it is best to use 33 to indicate whether finally carry (overflow), but the two together by different symbols, there is no this problem, expressed in 32-bit registers results can be directly:

  1101
  + 11
----------
 10000Copy the code

2, subtraction

Subtraction is simply expressing an assignment as a complement and adding it to another number, After the transformation, the rule is the same as the addition 1-1 = 1 + (-1) =[0000 0001] original + [1000 0001] Original =[0000 0001] complement + [1111 1111] complement =[0000 0000] complement =[0000 0000] original

3, multiplication

Direct example, slowly explain the principle is really difficult to understand and also trouble multipliers:

1101 (13) X 0101 (5) = 01000001 (65) 1101 multiplier X 0101 multiplier ---------- 1101 0000 1101 0000 ------------------- 01000001 (65) product // This is the binary multiplication operation, so how to implement in the adder arithmetic logic unit (ALU)?Copy the code

So according to the human algorithm above, we can think of it as follows: first vacate a register for the product, initialize it to 0, and then execute the following program:

It’s obvious why the multiplier moves to the right, but why does it move to the left? Because when you move the multiplier to the right, but it doesn’t change the order of magnitude, it’s 2 to the NTH, it’s not the ones place, so the corresponding thing, since it doesn’t show up in the multiplier, of course you have to change the multiplicator. Thanks to the binary optimization that X2 can shift to the left, every time you do a higher bit of the multiplier, you don’t need to change anything else, just the multiplier X2. Naturally, the multiplier shift to the left makes sense, just as in the example above, The third step is to add four 1101’s to the original product, so we just need to move the multiplicand to the left twice, and of course we get four 1101’s, which is 110100. The exit condition is usually after 32 executions.

4, division

Division is the most troublesome, no doubt about it. Dividend is equal to quotient X divisor plus remainder

1001 (commercial) (divisor) -- -- -- -- -- - (dividend), 1000-1000 | 1001010-10, 101, 1010, 1000 - - - 10 (remainder)Copy the code

76/8=9 ··· 4, we initialize the remainder register to the dividend, and then subtract the divisor each time, as shown in the following flowchart:

The calculation method of division and multiplication have some similarities, but it is different to a great extent. The main difference is that the concept of remainder is introduced. And cleverly put the divisor 32 bits higher in the 64-bit register, so that you don’t have to calculate how many bits to move right in order to subtract correctly in the first place.

5. Signed multiplication and division

This is a cumbersome processing item, although it is obvious to humans at a glance, but computers are different. So we need some other mechanism to make sure that the signed calculation is correct. For multiplication, remember the sign bits, and determine that the result is exactly negative, and then add the last time, only the highest 31 bits of multiplication. The division rules are different. Because we have to set the sign of the remainder, so the remainder can’t be negative, otherwise it would be (-7) /2 = -3 ··(-1) or (-7) /2 = -4 ··(-1).

These are two seemingly correct cases with completely different results. So you want to keep the dividend and the remainder the same sign. Then we can get the following conclusion:

The correct signed division algorithm has a negative quotient when the sign of the source operand is opposite, and the sign of the non-zero remainder is the same as that of the dividend! Note the dividend not the divisor and quotient!!

After the body

Sometimes I wonder if it’s worth it to spend at least an hour or two typesetting every day. Although it hasn’t see much effect to fans for this thing I also see more and more weak, but still good, after all, to relive once a day, and is equivalent to do the reading notes, feel the ideas of the author, equivalent to myself to write a book, or a great roar ~ ~ this notes so far, the whole last night about the shell programming, Estimated this evening will be a Linux Shell entry, hope like!!

Here’s a nice leg