Transcript powerpoint

10/2: Lecture Topics
•
•
•
•
Addressing modes
2’s complement
Floating point
Logical operations
Addressing Modes
• Each ISA specifies a several of
addressing modes
• MIPS supports very few:
– register mode
– displacement mode
– immediate mode
– PC-relative mode
– Pseudodirect mode
Register Mode
• The operand is a register
• Takes 5 bits
• Example: add
add $s0, $s1, $s2
• Nearly all instructions use at least one
register
Displacement Mode
• The operand is at a memory location
• The location is the sum of a register
and a constant
• Takes 16+5 = 21 bits
• Example: lw
lw $t0, -20($t1)
Immediate Mode
• The operand is a constant within the
instruction
• Takes 16 bits
• Example: addi
addi $t0, $t0, 2
Program Counter
• The PC (program counter) is the
address of the current instruction
0x00FF2020
0x00FF2024
0x00FF2028
0x00FF202C
addi
beq
j
Done: lw
$s0, $s0, 1
$s0, $zero, Done
Start
$s0, 0($s1)
• What is PC if current instruction is
beq
$s0, $zero, Done
PC-Relative Addressing
• A branch uses 16 bits to represent the branch
offset
• If branch address was absolute, then could
only jump to instructions 0 to 64K
• Loops aren’t very many instructions
– branch close to current instruction
– branch offset is added to the PC
PC = PC + 4 * offset
0x00FF2020
0x00FF2024
0x00FF2028
0x00FF202C
addi
beq
j
Done: lw
$s0, $s0, 1
$s0, $zero, Done
Start
$s0, 0($s1)
Pseudodirect Addressing
• Jump instructions (j or jal) can go much
further than 32K
• Jump instructions use 26 bits so 64M
instructions
• Jump address is the absolute address of
the instruction (almost)
0x003F2028
...
0x00FE3764
j BadError
BadError: addi $s0, $s0, 1
Negative Numbers
• We can use 32 bits to represent
0 to 232 – 1
-231 to 231 – 1
• How can we represent negative
numbers?
–
–
–
2’s complement
• Converting to/from positive from/to negative
– Flip the bits and add 1
0010 0110 = 38
1101 1001
+
1
1101 1010
=
-38
1101 1010
=
-38
0010 0101
+
1
0010 0110
=
38
• Negative numbers will always have a leading one
2’s complement cont.
• Why use this strange representation?
38 + -38
0010 0110
+ 1101 1010
-72 + -15
=
=
38
-38
1011 1000
+ 1111 0001
=
=
-72
-15
• How do you know if 10000001 is 129 or
-127?
• How does the CPU know?
Floating Point Representation
• Floating point is used to represent
“real” numbers like 0.5, 6.02 * 1023
3.141592653589793238462643383…
• What do you need to store a floating
point number?
Design Goals of Floating Point
• Easy to convert to/from
• Quickly compare two numbers
• Represent as many numbers as possible
Floating Point Format
single:
double:
1.1x 213
-1.0x 217
sign
exponent
significand
1
1
0
1
8 bits
11 bits
13
17
23 bits
52 bits
1.1
1.0
• Each floating point number contains
– sign bit, significand (mantissa) exponent
• Floating point number is normalized
– 100.11 x 212  1.0011 x 214
• Sign bit is 1st. 1 if negative
Floating Point Exponent
• Could be stored in 2’s complement, but
this would make comparisons hard
• Instead, exponent includes bias
– Stored exponent is actual exponent + bias
– Bias for single precision = 127
– Bias for double precision = 1023
– 00000000 is really 00000000 – 127 = -127
– 11111111 is really 11111111 – 127 = 128
Floating Point Significand
• Every number (except zero) has a leading
one in normalized form
• It’s silly to store a bit that is “always” 1
• So, significand assumes a leading 1
– For 1.0001 only store .0001
• How do you represent 0?
Floating Point Example
• What is the single precisions floating point
representation of –75?
• Normalized binary representation =
•
•
•
•
Sign bit =
Exponent =
Exponent w/ bias =
Significand=
Problems with Floating Point
• Integer representations are exact, but FP
representation are approximate
– 0.1 cannot be represented exactly
(0.100000001490116 for floats)
(0.10000000000000001 for doubles)
• Round off error in operations
// this code never terminates
double big
= 1000000000.0;
double small = 0.0000000001;
while( big == 1000000000.0 )
big = big + small;
• Single-precision floating point is too inexact
– Always use doubles
Overflow and Underflow
• Overflow occurs when a number is too big to
represent usually as the result of a numerical
operation
–
–
–
–
unsigned ints, > 232-1
signed ints, > 231-1
floats, > 3.40282347e+38
doubles, > 1.7976931348623157e+308
• Underflow means the number is too small to
represent
–
–
–
–
unsigned ints < 0
signed ints, < -231
floats, > 1.17549435e-38
doubles, > 2.2250738585072014e-308
Logical Operations
• Bitwise operations
– and 0110 0011 & 1100 0110 = 0100 0010
– or
0110 0011 | 1100 0110 = 1110 0111
• Shift operations
– left shift 0000 1111 << 2 = 0011 1100
– right shift 0000 1111 >> 2 = 0000 0011
• Signed >>
– 1110 0010 >> 2 = 1111 1000
• Unsigned >>
– 1110 0010 >> 2 = 0011 1000
Examples
• Evaluate the following
– (1100 1100 & 0001 1111) | 1100 0000
– (1100 1100 >> 3) | 1100 0000
• Fill in the body of this procedure
int GetBitFromPosition( int num, int pos ) {
if( ( pos < 0 ) || ( pos >= 32 ) ) {
fprintf( stderr, “You idiot.\n” );
return 0;
}
return
}
Examples Continued
• f is a single precision floating point
number write code to extract the
actual exponent from f