Docsity

Understanding Two's Complement and Representing Signed Numbers in Computers, Slides of Assembly Language Programming

Toggle sidebar
bg1
Chapter 3.1 - Integers 1
Representing Numbers: Review
•32-bit binary representation of (unsigned) number:
–b31
231+ b30
230 +

+ b2
22+ b1
21+ b0
20
–One billion (1,000,000,00010 ) in binary is
0011 1011 1001 1010 1100 1010 0000 00002
228 224 220 216 212 28 24 20
= 1
229 + 1
228 + 1
227 + 1
225 + 1
224 + 1
223 + 1
220 + 1
219 + 1
217 +
1
215 + 1
214 + 1
211 + 1x29
= 536,870,912 + 268,435,456 +134,217,728 +33,554,432 + 16,777,216 +
8,388,608 +1,048,576 +524,288 +131,072 +32,768 +16,384 +2,048 +
512 = 1,000,000,000
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download Understanding Two's Complement and Representing Signed Numbers in Computers and more Slides Assembly Language Programming in PDF only on Docsity!

Chapter 3.1 - Integers

1

Representing Numbers: Review

•^

32-bit binary representation of (unsigned) number:

b

31

^2

31

+ b

30

^2

30

+ b

2

^2

2

+ b

1

^2

1

+ b

0

^2

0

One billion (1,000,000,

10

) in binary is

2

(^28)

24

20

16

12

8

4

0

29

28

27

25

24

23

20

19

17

15

14

11

+ 1x

9

docsity.com

Chapter 3.1 - Integers

2

What If Too Big?

•^

Binary bit patterns are simply representations of numbers.

-^

Numbers really have an infinite number of digits (non-significantzeroes to the left).

  • with almost all being zero except for a few of the rightmost digits.– Don’t normally show leading zeros.

•^

If result of add (or any other arithmetic operation) cannot berepresented by these rightmost hardware bits, overflow is said tohave occurred.

-^

Up to Compiler and OS what to do.

docsity.com

Chapter 3.1 - Integers

4

What If Overflow Detected?

•^

If "exception" (or "interrupt") occurs

  • Address of the instruction that overflowed is saved in a register– Computer jumps to predefined address to invoke appropriate

routine for that exception

  • Like an unplanned hardware function call

•^

Operating System decides what to do

  • In some situations program continues after corrective code is

executed

•^

MIPS hardware support: exception program counter (EPC)contains address of overflowing instruction --- (more in Chpt. 5)

docsity.com

Chapter 3.1 - Integers

5

Representing Negative Numbers

Two

s Complement

•^

What is result for unsigned numbers if subtract larger numberfrom a smaller one?

  • Would try to borrow from string of leading 0s,

so result would have a string of leading 1s

  • With no obvious better alternative, pick representation that made

the hardware simple:

•^

leading 0s

positive,

•^

leading 1s

negative

xxx

xxx

•^

This representation is called two

s complement

docsity.com

Chapter 3.1 - Integers

7

•^

Recognizing role of sign bit, can represent positive and negativenumbers in terms of the bit value times a power of 2:

  • d

31

× -

31

d

30

× 2

30

  • ··· + d

2

× 2

2

  • d

1

× 2

1

  • d

0

× 2

0

•^

Example (given 32-bit two’s comp. number)1111 1111 1111 1111 1111 1111 1111 1100

2

×

31

×

^2

30

×

^2

29

×

^2

2

×

^2

1

×

^2

0

31

30

29

2

10

10

10

Two

s Complement Formula, Example

docsity.com

Chapter 3.1 - Integers

8

Ways to Represent Signed Numbers

(1) Sign and magnitude

  • separate sign bit

(2) Two’s (2’s) Complement (

n

bit positions)

n

-bit pattern d

n -



d

d 2

1

d

0

means:

-1 × d

n -

× 2

n -



  • d

2

× 2

2

  • d

1

× 2

1

  • d

0

× 2

0

  • also, unsigned sum of

n

-bit number and its negation = 2

n

positive

one

negative

one

(2’s

comp)

4

(=zero

if

only

bits)

docsity.com

Chapter 3.1 - Integers

10

Bias= 8 (Subtract 8)

1111111011011100101110101001100001110110010101000011001000010000

-1-2-3 -4-5-6 -7-8^76543210

(^0) -1-2 -3-4-5 -6-7 7 6 5 4 3 2 1 0

(^76543210) -1-2 -3-4-5 -6-7 -

151413121110

9 8 7 6 5 4 3 2 1 0

b

b 3

b 2

b 1

0

Bit-Pattern,

Unsigned,

2’s Comp, 1’s Comp,

Biased

docsity.com

Chapter 3.1 - Integers

11

Signed

Vs

. Unsigned Comparisons

•^

Note: memory addresses naturally start at 0 and continue to thelargest address – they are unsigned.

  • That is, negative addresses make no sense.

•^

C makes distinction in declaration.

  • integer (

int

) can be positive or negative.

  • unsigned integers (

unsigned

int

) only positive.

•^

Thus MIPS needs two styles of comparison.

  • Set on less than (

slt

) and set on less than immediate (

slti

work with signed integers.

  • Set on less than unsigned (

sltu

) and set on less than immediate

unsigned (

sltiu

). (Will work with addresses).

docsity.com

Chapter 3.1 - Integers

13

Two’s Complement Shortcut: Negation

•^

Invert every 0 to 1 and every 1 to 0, then add 1 to the result

  • Unsigned sum of number and its inverted representation must

be 111...

2

10

  • Let

x´

mean the

inverted representation

of

x

  • Then

x

x´

x

x´

x´

x

•^

Example: -4 to +4 to -

-^

x

2

x

´^

:^

2

2

x

2

2

IMPORTANT

SLIDE

docsity.com

Chapter 3.1 - Integers

14

Two’s Complement Shortcut

•^

Convert number represented in

k

bits to more than

k

bits

e.g.

, 16-bit immediate field converted to 32 bits before adding to 32-bit

register in addi

•^

Simply replicate the most significant bit (sign bit) of smaller quantityto fill new bits

  • 2's comp. positive number has infinite 0s to left– 2's comp. negative number has infinite 1s to left– Finite representation hides most leading bits; sign extension restores

those that fit in the integer variable

  • 16-bit -

10

to 32-bit:

2

2

Using Sign extension

docsity.com

Chapter 3.1 - Integers

16

Do It Yourself

-^

Convert the two’s complement number1111 1111 1111 1111 1111 1111 1111 1010

2

into decimal (base ten):

-^

Could use conversion formula (hard)1 x -

31

  • 1 x 2

30

  • … 1 x 2

1

  • 1 x 2

0

-^

Or, first use negation shortcut (easy) 0000 0000 0000 0000 0000 0000 0000 0101

1

0000 0000 0000 0000 0000 0000 0000 0110

= 6 (therefore, answer: -6)

docsity.com

Chapter 3.1 - Integers

17

1-bit Binary Addition

•^

two 1-bit values gives four cases:

0

0

1

1

0

1

0

1

0

1

1

(1) 0

•^

digital logic?: half-adder circuit

carry-out

1-bit sum

HA

a b

sumcarry-out

docsity.com

Chapter 3.1 - Integers

19

Detecting Overflow in 2’s Complement?

•^

Adding 2 31-bit positive 2’s complement numberscan yield a result that needs 32 bits

  • sign bit set with value of result (1) instead of proper

sign of result (0)

  • since need just 1 extra bit, only sign bit can be wrong

°

Op Adding operands with different signs, (subtracting withsame signs) overflow cannot occur

A

B

Result

A

+

B

>=

>=

<

A

+

B

<

<

>=

A

-

B

>=

<

<

A

-

B

<

>=

>=

docsity.com

Chapter 3.1 - Integers

20

Overflow for Unsigned Numbers?

•^

Adding 2 32-bit unsigned integers could yield a resultthat needs 33 bits

  • can't detect from "sign" of result

•^

Unsigned integers are commonly used for addressarithmetic, where overflows are ignored

-^

Hence, MIPS has unsigned arithmetic instructions,which ignore overflow:

  • addu, addiu, subu– Recall that in C, all overflows are ignored, so unsigned

instructions are always used (different for Fortran, Ada)

docsity.com