Download presentation
Presentation is loading. Please wait.
Published byRoss Todd Modified over 9 years ago
1
CHAPTER 3 Arithmetic For Computers 1/31/2009 1
2
Topics for discussion 1/31/2009 2 Number system: { radix/base, a set of distinct digits, operations} Radix conversion ASCII versus binary representation Signed arithmetic; sign extension Bounds check; validation Addition and subtraction algorithms Multiplication algorithms Floating point representation Floating point arithmetic algorithms
3
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 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?
4
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 – (2 32 -1). What is this value? Approx: 4,000,000,000 or 4G !
5
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 +(2 31 -1) positive range - 0 to –(2 31 -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.
6
Dealing with Overflow 1/31/2009 6 OperationOperand AOperand BResult indicating overflow A + B>= 0 < 0 A + B< 0 > = 0 A - B>= 0< 0 A - B< 0>= 0> 0
7
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,$t2 xor $t3,$t1,$t2 # check if signs differ slt $t3,$t3,$zero # signs differ? bne $t3,$zero,No_Overflow # signs differ nop # signs area same xor $t3,$t0,$t1 # find sign of sum slt $t3,$t3,$zero # if sum sign is diff bne $t3,$zero,Overflow
8
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 proc3 jr retAddr # jump back to callee beqz $s2, lab4 # is $s2 == 0 then jump to lab4
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.