컴퓨터구조(3) - 컴퓨터 연산(2)

‘컴퓨터 구조 및 설계’를 공부하여 정리한 내용입니다.

3. 곱셈

용어 정리

  • A * B = C
  • Multiplicand: A
  • Multiplier: B
  • Product: C

곱셈 알고리즘 하드웨어의 순차적 버전

  • (추후 업데이트)

곱셈 알고리즘 하드웨어의 수정된 버전

  • 2 * 3 = 6 -> 0010 * 0011 = 0110
  • Multiplicand: 0010, Multiplier: 0011, Product: 0110
  • 아래 표에서 순차적으로 보여주고 있다.
step Multiplicand Multiplier Product(이전) + Multiplicand or 0 Product(결과)
1 001(1) 0000_0010 0000_0000 + 0000_0010
(because Multiplicand’s LSB is 1)
0000_0010
2 000(1)
(shift right)
0000_0100
(shift left)
0000_0010 + 0000_0100
(because Multiplicand’s LSB is 1)
0000_0110
3 000(0)
(shift right)
0000_1000
(shift left)
0000_0110 + 0000_0000
(because Multiplicand’s LSB is 0)
0000_0110
4 000(0)
(shift right)
0001_0000
(shift left)
0000_0110 + 0000_0000
(because Multiplicand’s LSB is 0)
0000_0110

추가 내용

  • 부호있는 곱셈의 경우는 Multiplicand와 Multiplier의 부호만 따로 빼서 기억하고 둘 모두 부호 없는 버전으로 만들고 연산 이후에 부호를 추가하면 된다.
  • 더 빠른 곱셈을 하기 위해서는 이 loop를 병렬로 진행하면 된다. 물론 간단히 되는 것은 아니고 각 자리에서의 연산에서 생기는 carry out 등을 고려해야 하지 그래도 만 더 빨리 하는 것이 가능하다. 이는 하드웨어를 추가해서 구현하는 것이 가능하다.

4. 나눗셈

용어 정리

  • A / B = C, A % B = D
  • Dividend: A
  • Divisor: B
  • Quotient: C
  • Remainder: D

나눗셈 알고리즘과 하드웨어

  • 7 / 2 = 3, 7 % 2 = 1
    • 0111 / 0010 = 0011, 0111 % 0010 = 0001
    • Dividend: 7, Divisor: 2, Quotient: 3, Remainder: 1
  • 아래 표에서 순차적으로 보여주고 있다.
step Quotient(이전) Divisor Remainder - Divisor Remainder Quotient(이후)
0 0000 0010_0000
(2 << 4)
(because Dividend is 4bit)
NULL 0000_0111
(Remainder is initialized with Dividend)
0000
1 0000 0010_0000 1110_0111 0000_0111
(because result of (Remainder - Divisor) is minus)
0000
2 0000 (shift left) 0001_0000 (shift right) 1111_0111 0000_0111
(because result of (Remainder - Divisor) is minus)
0000
3 0000 (shift left) 0000_1000 (shift right) 1111_1111 0000_0111
(because result of (Remainder - Divisor) is minus)
0000
4 0000 (shift left) 0000_0100 (shift right) 0000_0011 0000_0011
(because result of (Remainder - Divisor) is plus)
0001
5 0010 (shift left) 0000_0010 (shift right)
(reach first divisor value 0010 = 2)
0000_0001 0000_0001
(because result of (Remainder - Divisor) is plus)
(result)
0011
(result)

추가 내용

  • 부호있는 나눗셈은 곱셈과 마찬가지로 부호만 따로 빼서 가지고 있는 방법이 있다.
    • 부호있는 나눗셈의 경우는 나머지의 부호도 결정해줘야 하는 불편함이 있는데, 부호에 관계없이 Remainder와 Quotient의 절대값은 같다는 것을 기억하면 쉽게 계산이 가능하다.
    • Ex) +7 / -2 => Quotient: -3, Remainder: 1
    • Ex) -7 / +2 => Quotient: -3, Remainder: -1
    • Ex) -7 / -2 => Quotient: 3, Remainder: -1
  • 곱셈처럼 하드웨어를 추가해서, 나눗셈의 속도를 증가시키는 단순한 방법은 없다. 하지만 예측하는 방법을 통해서 한번에 몫을 두 비트 이상으로 만드는 기술은 있다.
    • 추후에 업데이트 하겠다.

Leave a Comment