CHAPTER 3 Arithmetic For Computers 1/31/2009 1. Topics for discussion 1/31/2009 2 Number system: {...

8
CHAPTER 3 Arithmetic For Computers 1/31/2009 1

description

Number system 1/31/ Radix or Base: 10 for decimal system, 2 for binary system, 8 for octal, 12 for duo-decimal (to count dozens), 16 for hexa-decimal Decimal digits: {0,1,2,3,4,5,6,7,8,9} Binary {0,1}: binary digit is a “bit” Octal {0,1,2,3,4,5,6,7} Hex {0,..9, A, B, C, D, E, F} If we assume digits are number from right to left starting from 0 th digit as the least significant digit (Little Endian), the value of the i th digit d in is: d x base i Example: Your pay is $101 per hour. Would you prefer it (the base) in decimal, octal, hexadecimal or binary?

Transcript of CHAPTER 3 Arithmetic For Computers 1/31/2009 1. Topics for discussion 1/31/2009 2 Number system: {...

Page 1: CHAPTER 3 Arithmetic For Computers 1/31/2009 1. Topics for discussion 1/31/2009 2 Number system: { radix/base, a set of distinct digits, operations} Radix.

CHAPTER 3

Arithmetic For Computers

1/31/2009

1

Page 2: CHAPTER 3 Arithmetic For Computers 1/31/2009 1. Topics for discussion 1/31/2009 2 Number system: { radix/base, a set of distinct digits, operations} Radix.

Topics for discussion

1/31/2009

2

Number system: { radix/base, a set of distinct digits, operations}

Radix conversionASCII versus binary representationSigned arithmetic; sign extensionBounds check; validationAddition and subtraction algorithmsMultiplication algorithmsFloating point representation Floating point arithmetic algorithms

Page 3: CHAPTER 3 Arithmetic For Computers 1/31/2009 1. Topics for discussion 1/31/2009 2 Number system: { radix/base, a set of distinct digits, operations} Radix.

Number system

1/31/2009

3

Radix or Base: 10 for decimal system, 2 for binary system, 8 for octal , 12 for duo-decimal (to count dozens), 16 for hexa-decimal

Decimal digits: {0,1,2,3,4,5,6,7,8,9} Binary {0,1}: binary digit is a “bit” Octal {0,1,2,3,4,5,6,7} Hex {0,..9, A, B, C, D, E, F} If we assume digits are number from right to left starting

from 0th digit as the least significant digit (Little Endian), the value of the ith digit d in is:

d x basei

Example: Your pay is $101 per hour. Would you prefer it (the base) in decimal, octal, hexadecimal or binary?

Page 4: CHAPTER 3 Arithmetic For Computers 1/31/2009 1. Topics for discussion 1/31/2009 2 Number system: { radix/base, a set of distinct digits, operations} Radix.

Number Representation

1/31/2009

4

On the keyboard it is represented by ASCII: American Standard Code for Information Interchange: 7 bit code represented by a byte container.

For character representation this is a nice system.How efficient is this representing numbers for

processing?Consider 4 ASCII digits. What is the range of

integers you can represent with this? 0 – 9999 4 X 8 = 32 bits

With 32 bits and binary systems and only positive numbers: 0 – (232 -1). What is this value? Approx: 4,000,000,000 or 4G !

Page 5: CHAPTER 3 Arithmetic For Computers 1/31/2009 1. Topics for discussion 1/31/2009 2 Number system: { radix/base, a set of distinct digits, operations} Radix.

Signed Numbers

1/31/2009

5

When we allow negative and positive numbers, half the range is occupied by positive numbers and the other by negative numbers.

How to represent the sign? Using a bit ? 0 for positive and 1 for negative?

Then with 32 bits: + 0 to +(231 -1) positive range - 0 to –(231 -1) negative range A better representation for negative number is 2’s

complement. How to compute 2’s complement?What’s the advantage of 2’s complement?

Subtraction is equivalent to adding 2’s complement of the second operand.

Sign extension can be used to extend the number from, from 16 bits to 32 bits for example.

Page 6: CHAPTER 3 Arithmetic For Computers 1/31/2009 1. Topics for discussion 1/31/2009 2 Number system: { radix/base, a set of distinct digits, operations} Radix.

Dealing with Overflow

1/31/2009

6

Operation Operand A Operand B Result indicating overflow

A + B >= 0 >= 0 < 0A + B < 0 < 0 > = 0A - B >= 0 < 0 < 0A - B < 0 >= 0 > 0

Page 7: CHAPTER 3 Arithmetic For Computers 1/31/2009 1. Topics for discussion 1/31/2009 2 Number system: { radix/base, a set of distinct digits, operations} Radix.

Dealing with overflow

1/31/2009

7

Signed operations that result in overflow cause an exception whereas unsigned operations on overflow do not cause exception.

Example: add, addi, sub will cause exception on overflow addu, addiu, and subu will not cause exception on overflow

How to detect overflow in unsigned then?addu $t0,$t1,$t2xor $t3,$t1,$t2 # check if signs differslt $t3,$t3,$zero # signs differ?bne $t3,$zero,No_Overflow # signs differnop# signs area samexor $t3,$t0,$t1 # find sign of sumslt $t3,$t3,$zero # if sum sign is diffbne $t3,$zero,Overflow

Page 8: CHAPTER 3 Arithmetic For Computers 1/31/2009 1. Topics for discussion 1/31/2009 2 Number system: { radix/base, a set of distinct digits, operations} Radix.

Branches and Jumps

1/31/2009

8

beq $s1,$s2,lab1 # if $s1 == $s2 jump to label lab1

bne $s1,$s2,lab2 # if $s1 ≠ $s2 jump to label lab2

j lab3 # unconditional jump to lab3

jal proc3 # jump and link to proc3jr retAddr # jump back to calleebeqz $s2, lab4 # is $s2 == 0 then jump to

lab4