b1011 Machine Code
description
Transcript of b1011 Machine Code
![Page 1: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/1.jpg)
b1011Machine Code
ENGR xD52Eric VanWyk
Fall 2012
![Page 2: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/2.jpg)
Today
• Review Homework
• Translating Assembly to Machine Code
• Executing Machine Code
• Biggest Endian is Best Endian
![Page 3: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/3.jpg)
Estimation
• Answers ranged from 3 seconds to many trillions of years, less than a cent to more than the cumulative global GDP for our lifetimes
• Lets use dimensional analysis to pull the range in a little bit…
![Page 4: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/4.jpg)
Dimensional Analysis
• Target a specific unit with a series of translating scaling factors
• Make sure units cancel
• Super easy if everything is to the 1st power
![Page 5: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/5.jpg)
Fast Approximations
• Translate scaling factors to powers of 10
• Add to multiply, subtract to divide
• Memorize your Log Tableslog(8) = 0.903 log(5) = 0.698log(3) = 0.477 log(2) = 0.301
![Page 6: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/6.jpg)
Fast Approximations
• Translate scaling factors to powers of 10
• Add to multiply, subtract to divide
• Memorize your Log Tables8 -> E0.9 5 -> E0.73 -> E0.5 2 -> E0.3
![Page 7: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/7.jpg)
Assumptions Made
• 1 billion tests per second– 10k to 10B is ok
• 20 Watts– 5 to 100 Watts is ok
• 10 cents per kWhr– Or whatever
![Page 8: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/8.jpg)
Duration
• Number of Computations
• Computations / second
• Seconds / year
• Result in years
(2^64): 64*.3 : 10^19.2
10^9
3*10^7 = 10^7.5
19.2-9-7.5 = 2.7
![Page 9: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/9.jpg)
Cost
• Number of Seconds
• Seconds / hour
• Kilowatts (0.020)
• Dollars per kWhr
• Dollars
10^10.2
10^3.6?
10^(1.3-3=-1.7)
10^-1
10.2-3.6+(-1.7)+(-1) = 3.9
![Page 10: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/10.jpg)
Results
Time:
Math = 584.5 years
Estimate = 500
Error = -15%
Ratio = 0.85
Log Error = .06ish
Cost:
Math = 10,248 dollars
Estimate = 8,000 dollars
Error = -22%
Ratio = 0.78
Log Error = .1ish
![Page 11: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/11.jpg)
Machine Code
• The actual bits sent to the processor
![Page 12: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/12.jpg)
Encoding Limitations
• Designing an Encoding Scheme is a Game– Best use of limited space?– Favor some options over others
• Take Available Space and divide into “Fields”– Encoding within an Encoding (MOAR BOXES)– Fixed width per field– How many fields does IEEE-754 use?
![Page 13: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/13.jpg)
Types of Fields
• Enumerations– No mathematical meaning, just enumerate options
• Signed / Unsigned– We know these well
• Biased– Mathematically offset by a constant
![Page 14: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/14.jpg)
MIPS Code Encoding Formats• All instructions encoded in 32 bits• Register (R-type) instructions
• Immediate (I-type) instructions
• Jump (J-type) instructions
3130292827262524232221201918171615141312111009080706050403020100
OP RS RT RD SHAMT FUNCT
3130292827262524232221201918171615141312111009080706050403020100
OP RS RT 16 bit Address/Immediate
3130292827262524232221201918171615141312111009080706050403020100
OP 26 bit Address
(OP = 0,16-20)
(OP = any but 0,2,3,16-20)
(OP = 2,3)
![Page 15: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/15.jpg)
Multiple Encoding Formats?
• Make the most of our limited resources
• How do we know which format?
• Why bother re-using encodings at all?
![Page 16: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/16.jpg)
J-Type
• Used for Unconditional Jumps• Simplest MIPS encoding
• How do we encode “j 100”?
2: j (jump)3: jal (jump and link)
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
OP 26 bit Address
![Page 17: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/17.jpg)
J-Type
• Weirdness in the Address Field
• Bottom 2 bits are always ‘00’, so drop’em– Shift everything over by 2
• That’s only 28 effective bits…– Where are the other 4?– How does this limit us?– How do we compensate?
![Page 18: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/18.jpg)
I-Type
• Used for operations with immediate (constant) operand
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
OP RS RT 16 bit Address/Immediate
04: beq05: bne06: blez07: bgtz08: addi09: addiu10: slti11: sltiu12: andi13: ori14: xori32: lb35: lw40: sb43: sw
Op1,L/S addr
Op2, Dest, L/S targ
![Page 19: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/19.jpg)
I-Type
• Used for ops with an immediate operand
• One Op Field (Enumeration)
• Two register address fields
• One Signed/Unsigned field
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
OP RS RT 16 bit Address/Immediate
![Page 20: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/20.jpg)
I-Type Examples
04: beq05: bne06: blez07: bgtz08: addi09: addiu10: slti11: sltiu12: andi13: ori14: xori32: lb35: lw40: sb43: sw
addi $t0, $t1, 100 # $t0 = $t1+1003130292827262524232221201918171615141312111009080706050403020100
beq $a0, $a1, -44 # if $a0 == $a1 GOTO (PC+4+FOO*4)3130292827262524232221201918171615141312111009080706050403020100
lw $t3, 12($t0) # $t3 = Memory[$t0+12]3130292827262524232221201918171615141312111009080706050403020100
![Page 21: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/21.jpg)
I-Type Examples
04: beq05: bne06: blez07: bgtz08: addi09: addiu10: slti11: sltiu12: andi13: ori14: xori32: lb35: lw40: sb43: sw
addi $t0, $t1, 100 # $t0 = $t1+1003130292827262524232221201918171615141312111009080706050403020100
addi $t1 $t0 100
beq $a0, $a1, -44 # if $a0 == $a1 GOTO (PC+4+FOO*4)3130292827262524232221201918171615141312111009080706050403020100
beq $a1 $a0 -11
lw $t3, 12($t0) # $t3 = Memory[$t0+12]3130292827262524232221201918171615141312111009080706050403020100
lw $t0 $t3 12
![Page 22: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/22.jpg)
I-Type Examples
04: beq05: bne06: blez07: bgtz08: addi09: addiu10: slti11: sltiu12: andi13: ori14: xori32: lb35: lw40: sb43: sw
addi $t0, $t1, 100 # $t0 = $t1+1003130292827262524232221201918171615141312111009080706050403020100
8 9 8 100
beq $a0, $a1, -44 # if $a0 == $a1 GOTO (PC+4+FOO*4)3130292827262524232221201918171615141312111009080706050403020100
4 5 4 -11
lw $t3, 12($t0) # $t3 = Memory[$t0+12]3130292827262524232221201918171615141312111009080706050403020100
0x23 8 11 12
![Page 23: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/23.jpg)
R-Type• Used for 3 register ALU operations
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
OP RS RT RD SHAMT FUNCT00: sll02: srl03: sra04: sllv06: srlv07: srav08: jr24: mult26: div32: add33: addu34: sub35: subu36: and37: or38: xor39: nor42: slt
00(10-13 for FP)
Shift amount(0 for non-shift)
add $8, $9, $10 # $8 = $9+$10
sll $8, $9, 6 # $8 = $9<<6
sllv $8, $9, $10 # $8 = $9<<$10
Op2Op1 Dest
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
00 9 10 8 0 32
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
00 X 9 8 6 00
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
00 10 9 8 0 04
![Page 24: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/24.jpg)
Decoding
• The Instruction Decode Unit translates the encoded Machine Code into control signals
• Lets (begin to) figure out the Decode Unit by creating (part of) its truth tables
• To finish, we’d apply Boolean Law and push the design all the way through to gates
![Page 25: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/25.jpg)
Control SignalsFunc 100000 100010 N/A N/A N/A N/A
Op 000000 000000 100011 101011 000100 000010
add sub lw sw beq j
RegDst
ALUSrc
MemToReg
RegWr
MemWr
Branch
Jump
ALUCntrl Add Sub Add Add Sub X
![Page 26: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/26.jpg)
26
Our Single Cycle CPU
SignExtnd
WrEn AddrDin DoutData
Memory
InstructionFetchUnit
Rs Rt Rs Rt Rd Imm16
imm16
Instructions[31:0]
[25:21]
[20:16]
[15:11]
[15:0]
BranchJump
ALUSrc
RegDst
Rd Rt
ALUcntrl
Aw Aa Ab DaDw Db Register
WrEn File RegWr
MemWr MemToRegZero
![Page 27: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/27.jpg)
Control SignalsFunc 100000 100010 N/A N/A N/A N/A
Op 000000 000000 100011 101011 000100 000010
add sub lw sw beq j
RegDst 1 1 0 X X X
ALUSrc 0 0 1 1 0 X
MemToReg
0 0 1 X X X
RegWr 1 1 1 0 0 0
MemWr 0 0 0 1 0 0
Branch 0 0 0 0 1 X
Jump 0 0 0 0 0 1
ALUCntrl Add Sub Add Add Sub X
![Page 28: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/28.jpg)
Design Your Own Encodings
• Memorizing MIPS encodings is super fun, but lets make up our own instead!
• Use the single cycle cpu as a starting point– Modify as necessary to suit your goals
• Come up with an interesting tweak to the design and see it all the way through
![Page 29: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/29.jpg)
Design Your Own Encodings
• Ideas:– What can you fit into a 16 bit Machine Code?– Can you add support for ARM-like shifting?– What about variable length encodings?
• Tools / Tweaks:– How many Registers do you support?– Are some ops redundant?– Nothing is sacred today
![Page 30: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/30.jpg)
Design Your Own Deliverables
• 2 sentences on your idea• Description of supported operations• Description of encoding styles• White Board Schematic of modified CPU• Assemble, Encode, Decode a small example
program to highlight your idea
![Page 31: b1011 Machine Code](https://reader035.fdocuments.in/reader035/viewer/2022081512/568163bb550346895dd4d344/html5/thumbnails/31.jpg)
31
Our Single Cycle CPU
SignExtnd
WrEn AddrDin DoutData
Memory
InstructionFetchUnit
Rs Rt Rs Rt Rd Imm16
imm16
Instructions[31:0]
[25:21]
[20:16]
[15:11]
[15:0]
BranchJump
ALUSrc
RegDst
Rd Rt
ALUcntrl
Aw Aa Ab DaDw Db Register
WrEn File RegWr
MemWr MemToRegZero