Lec2 Instructions
Lec2 Instructions
Signed Integer
3 major representations:
Sign-magnitude Ones complement Twos complement
Assumptions:
4-bit machine word 16 different values can be represented Roughly half are positive, half are negative
+0
0000 0001
+1 +2 +3 +4
+ 0 100 = + 4 1 100 = - 4 -
1101
0010
+5
-1
+6
-0
+7
High order bit is sign: 0 = positive (or zero), 1 = negative Three low order bits is the magnitude: 0 (000) thru 7 (111) Number range for n bits = +/-2n-1 -1 Two representations for 0
+0
0000 0001
+1 +2 +3 +4
+ 0 100 = + 4 1 011 = - 4 -
1101
0010
+5
-6
+6
-7
+7
Subtraction implemented by addition & 1's complement Still two representations of 0! This causes some problems Some complexities in addition
+0
0000 0001
+1 +2 +3 +4
+ 0 100 = + 4 1 100 = - 4 -
1101
0010
-4 -5 -6
+5
-7
+6
-8
+7
Only one representation for 0 One more negative number than positive number
4 -3 1
-4 +3 -1
Simpler addition scheme makes twos complement the most common choice for integer number systems within digital systems
( + 4)
(f)
(g)
( + 3)
(h)
( - 2)
(i)
( - 8)
(j)
( + 5)
Overflow - Add two positive numbers to get a negative number or two negative numbers to get a positive number
-1 -2 -3 -4 -5 -6 -7 -8
1110 1101 1100 1011 1010 1001 1000 1111
+0
0000 0001 0010 0011 0100 0101 0110 0111
-1 +1 +2 +3 +4 +5 -6 -7 -8 -7 - 2 = +7 -4 -5 -3 -2
1110 1101 1100 1011 1010 1001 1000 1111
+0
0000 0001 0010 0011 0100 0101 0110 0111
+1 +2 +3 +4 +5
+6
+6
+7 5 + 3 = -8
+7
Overflow Conditions
5 3 -8 Overflow 5 2 7 No overflow 0000 0101 0010 0111 0111 0101 0011 1000 -7 -2 7 Overflow -3 -5 -8 1111 1101 1011 11000 1000 1001 1100 10111
No overflow
Overflow when carry-in to the high-order bit does not equal carry out
Sign Extension
Task: Given w-bit signed integer x Convert it to w+k-bit integer with same value Rule: Make k copies of sign bit: X = xw1 ,, xw1 , xw1 , xw2 ,, x0
X
k copies of MSB
short int x = 15213; int ix = (int) x; short int y = -15213; int iy = (int) y;
x ix y iy
Memory consists of many millions of storage cells, each of which can store 1 bit. Data is usually accessed in n-bit groups. n is called word length.
i th word
last word
Memory words.
Sign bit: b31= 0 for positive numbers b31= 1 for negative numbers (a) A signed integer
8 bits
8 bits
8 bits
8 bits
ASCII character
ASCII character
ASCII character
ASCII character
Big-Endian: lower byte addresses are used for the most significant bytes of the word
Little-Endian: opposite ordering. lower byte addresses are used for the less significant bytes of the word
Word address 0 4
k k k k k k k k
k k
2 -4
2 -4
2 -3
2- 2
2 - 1
2 - 4
2- 1
2 - 2
2 -3
2 -4
Memory Operation
Load (or Read or Fetch)
Copy the content. The memory content doesnt change. Address Load Registers can be used
Must-Perform Operations
Data transfers between the memory and the processor registers Arithmetic and logic operations on data Program sequencing and control I/O transfers
CPU Organization
Single Accumulator
Result usually goes to the Accumulator Accumulator has to be saved to memory quite often
General Register
Registers hold operands thus reduce memory traffic Register bookkeeping
Stack
Operands and result are always in the stack
Instruction Formats
Three-Address Instructions
ADD ADD ADD ADD R1, R2, R3 R1, R2 M R3 [R1] + [R2] R2 [R1] + [R2] AC AC + [M] TOS [TOS] + [(TOS 1)]
Instruction Formats
Example: Evaluate X = (A+B) (C+D) Three-Address
1. ADD 2. ADD 3. MUL A, B, R1 C, D, R2 R1, R2, X ; R1 [A] + [B] ; R2 [C] + [D] ; X [R1] [R2]
Instruction Formats
Example: Evaluate X = (A+B) (C+D) Two-Address
1. 2. 3. 4. 5. 6. MOV ADD MOV ADD MUL MOV A, R1 B, R1 C, R2 D, R2 R2, R1 R1, X ; R1 [A] ; R1 [R1] + [B] ; R2 [C] ; R2 [R2] + [D] ; R1 [R1] [R2] ; X [R1]
Instruction Formats
Example: Evaluate X = (A+B) (C+D) One-Address
1. 2. 3. 4. 5. 6. 7. LOAD ADD STORE LOAD ADD MUL STORE A B T C D T X ; AC [A] ; AC [AC] + [B] ; T [AC] ; AC [C] ; AC [AC] + [D] ; AC [AC] [T] ; X [AC]
Instruction Formats
Example: Evaluate X = (A+B) (C+D) Zero-Address
1. 2. 3. 4. 5. 6. 7. 8. PUSH PUSH ADD PUSH PUSH ADD MUL POP A B C D ; TOS [A] ; TOS [B] ; TOS [A] + [B] ; TOS [C] ; TOS [D] ; TOS [C] + [D] ; TOS (C+D)(A+B) ; X [TOS]
Instruction Formats
Example: Evaluate X = (A+B) (C+D) RISC
1. 2. 3. 4. 5. 6. 7. 8. LOAD LOAD LOAD LOAD ADD ADD MUL STORE A, R1 B, R2 C, R3 D, R4 R1, R2, R1 R3, R4, R3 R1, R3, R1 R1, X ; R1 [A] ; R2 [B] ; R3 [C] ; R4 [D] ; R1 [R1] + [R2] ; R3 [R3] + [R4] ; R1 [R1] [R3] ; X [R1]
Using Registers
Registers are faster Shorter instructions
The number of registers is smaller (e.g. 32 registers need 5 bits)
Potential speedup Minimize the frequency with which data is moved back and forth between the memory and processor registers.