1 Representing a range of numbers Signed magnitude representation o - - PDF document

1
SMART_READER_LITE
LIVE PREVIEW

1 Representing a range of numbers Signed magnitude representation o - - PDF document

Our mutual friend Numeric Representation o Conversion to and from hex is not too Two's complement and friends BAD 16 = __________ 10 . o Octal is also useful* 0, 1, 2, 3, . . . o Why do machine language programmers confuse Halloween with


slide-1
SLIDE 1

1

CS240 Computer Organization

Department of Computer Science Wellesley College

Numeric Representation

Two's complement and friends

Numeric representation 4-2

Our mutual friend

  • Conversion to and from

hex is not too BAD16 = __________10.

  • Octal is also useful*

0, 1, 2, 3, . . .

  • Why do machine language

programmers confuse Halloween with Christmas?

*And the same conversion trick works between binary and octal. Why?

Numeric representation 4-3

Frankly, other conversions are no where as nice ...

  • Given a number num, we find dn dn-1 ... d1 d0 so that

num = dn rn + dn-1 rn-1 + ... + d1 r1 + d0 r0

  • The algorithm* is simple, but tedious.

for i ← 0 to n

  • dodi ← num mod r
  • num ← num div r

*num = dn rn-1 + ... + d1 r 1 + d0 r0 = (num div r) *r + (num mod r). Repeat.

Numeric representation 4-4

Arithmetic is the same everywhere

Addition 100112 + 001012 Subtraction CDB16

  • C3E16

Multiplication 1348 x 278 Division 112 1000112

slide-2
SLIDE 2

2

Numeric representation 4-5

Representing a range of numbers

  • What range of numbers can be represented using an n-bit

binary number.

0 1 1 0 0 1 1 1 n-1 . . . 2 1 0

Numeric representation 4-6

Signed magnitude representation

  • Using n bits, we could represent the natural numbers up

to 2n - 1 . . .

  • . . . or we could reserve one bit, usually the msb, to

represent a number’s sign.

0 1 2 3 4 5 . . . 2n-1

  • (2n-1-1) -2 -1 0 1 2 2n-1-1

000...0 111...1 111...1 011...1

sign bit (negative) sign bit (positive)

Numeric representation 4-7

Some problems with signed magnitude

  • Additional circuitry is needed to add positive numbers

to negative numbers. 0 1 1 0 1 represents +1101

  • 1 1 1 0 1 represents -1101
  • And, how exactly does one represent zero?*

*And why is this a problem?

Numeric representation 4-8

Click and Clack ride again

  • An automobile odometer

provides an elementary solution, provided it runs backwards as well as forwards number line

  • 500 ... -3 -2 -1 0 1 2 3 ... 499
  • number line
  • 500 ... 997 998 999 000 001 002 003 ... 499

*Rule for taking the negative of a number: Subtract number from 999 (to avoid borrowing) then add 1

slide-3
SLIDE 3

3

Numeric representation 4-9

Digital Tappet Brothers

  • We build a binary odometer
  • The number line works exactly the same as before

number line

  • 4 -3 -2 -1 0 1 2 3
  • number line
  • 100 101 110 111 000 001 010 011

*Rule for taking the negative of a number: Subtract number from 111 (to avoid borrowing) then add 1

Numeric representation 4-10

Addition works perfectly*

  • We retain our handy-dandy 3-bit binary odometer

*And there is no need for a separate circuit to do subtraction. But overflow remains a clear and present danger.

  • 1

+ -2 3 + -1

  • 1

+ -4 2 + -3

Numeric representation 4-11

Detecting overflow

  • Some language, C for

example, ignore integer

  • verflow. Others require

that the program be notified.

  • MIPS detects overflow

with an exception.

  • The exception program

counter (EPC) contains the address of the offending instruction.

Logical operations

add add $s1,$s2,$s3 $s1=$s2 + $s3 Arithmetic subtract sub $s1,$s2,$s3 $s1=$s2 - $s3 add immediate addi $s1,$s2,100 $s1=$s2 + 100 and and $s1,$s2,$s3 $s1=$s2 & $ s3

  • r
  • r $s1,$s2,$s3

$s1=$s2 | $s3 nor nor $s1,$s2,$s3 $s1=~($s2 | $s3) Logical and immediate andi $s1,$s2,100 $s1=$s2 & 100

  • r immediate
  • ri $s1,$s2,100

$s1=$s2 | 100 sll sll $s1,$s2,10 $s1=$s2 << 10 srl srl $s1,$s2,10 $s1=$s2 >> 10 Data transfer load word lw $s1,100($s2) $s1=Mem[$s2+100] store word sw $s1, 100($s2) Mem[$s2+100]=$s1

4-12 Numeric representation

slide-4
SLIDE 4

4

andi $t2, $s0, 15

  • AND applied can be used

to mask or conceal certain bit patterns by forcing 0s.

  • For example, suppose $s0

held the bit pattern 72 A8 8B AD?

  • Similarly, the OR
  • peration may be used

force 1s.

4-13 Numeric representation

Alas, no NOT

  • In keeping with the two-operand format, the

designers of MIPS decided against a NOT

  • peration.
  • A NOR operation is offered in consolation.*

nor $t0, $t1, $t2 # $t0 = ~($t1 | $t3)

  • *How does this help?

4-14 Numeric representation