Presentation is loading. Please wait.

Presentation is loading. Please wait.

Machine Languages Different types of CPU’s understand different instructions Pentium family / Celeron / Xeon / AMD K6 / Cyrix … (Intel x86 family) PowerPC.

Similar presentations


Presentation on theme: "Machine Languages Different types of CPU’s understand different instructions Pentium family / Celeron / Xeon / AMD K6 / Cyrix … (Intel x86 family) PowerPC."— Presentation transcript:

1 Machine Languages Different types of CPU’s understand different instructions Pentium family / Celeron / Xeon / AMD K6 / Cyrix … (Intel x86 family) PowerPC (Mac) DragonBall (Palm Pilot) StrongARM/MIPS (WinCE) Many Others (specialized or general-purpose) They represent instructions differently in their assembly/machine languages (even common ones) Let’s look instructions for a simple example CPU

2 An Example CPU We’ll look at a “toy” CPU’s Machine Language Our CPU has: 8 Registers – each holds 16 bits (2 bytes)

3 Our Example CPU We’ll look at a toy CPU’s Machine Language: Our CPU has: 8 Registers – each holds 16 bits (2 bytes) Our RAM: Reads and writes (loads and stores) in blocks of 16 bits (2 bytes) Has 2 8 = 256 addresses Total Memory: 256*2 = 512 bytes

4 Writing it down We are going to be looking at lots of 16-bit sequences. E.g. 0110110010100101 It can be tiring/confusing to read so many bits. So we use Hexadecimal representation of data and instructions

5 Recall Hexadecimal 0123456701234567 0000000100100011010001010110011100000001001000110100010101100111 89ABCDEF89ABCDEF 1000100110101011110011011110111110001001101010111100110111101111

6 Hex Shorthand e.g. 0123456701234567 0000000100100011010001010110011100000001001000110100010101100111 89ABCDEF89ABCDEF 1000100110101011110011011110111110001001101010111100110111101111 0110 1100 1010 0101

7 Hex Shorthand e.g. 0123456701234567 0000000100100011010001010110011100000001001000110100010101100111 89ABCDEF89ABCDEF 1000100110101011110011011110111110001001101010111100110111101111 0110 1100 1010 0101 6 C A 5

8 Machine “Core” Everything is in binary (hex) Registers R0: 0000 R1: 0CA8 R2: A9DB R3: 0705 R4: 1011 R5: 90A0 R6: 0807 R7: 00A0 00: 0000000000000000 04: 0000000000000000 08: 0000000000000000 0C: 0000000000000000 10: B106B200B0011221 … F8: 0000000000000000 FC: 0000000000000000 Memory

9 Representing Instructions Machine instructions will also be represented by 16 bits. We’ll divide those bits up into groups: The first 4 bits are called the Op-Code: Tells what type of instruction it is. How many possibilities does 4 bits give us? 6 C A 5

10 Instructions 16 possible Op-Codes: We’ll only look at a few in detail… 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left

11 Halt Instruction Opcode 0 : Halt This just tells the machine to stop. Other 12 bits are ignored. So: All have same effect. 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left 0 0 0 F F F 0 9 A C

12 Data Instructions Opcode B : Load Direct / Address Sets a Register to a fixed Specified Value Encoding: Effect: Register A becomes the specified value 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left B regA value (8 bits)

13 Arithmetic/Logic Instructions Opcode 1 : Add Adds contents of two registers together and puts result in third register Encoding: Effect: Register A becomes Register B + Register C 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left 1 regA regB regC

14 Adding Numbers Simple Program to calculate 1 + 2 + 3 + 4 + 5 + 6 = 21 = 15 (hex) 10: Load R0  01 (always 1) 11: Load R2  00 (running total) 12: Load R1  01 (current number) 13: Add R2  R2 + R1 (R2=1) 14: Add R1  R1 + R0 (R1=2) 15: Add R2  R2 + R1 (R2=3) 16: Add R1  R1 + R0 (R1=3) 17: Add R2  R2 + R1 (R2=6) 18: Add R1  R1 + R0 (R1=4) 19: Add R2  R2 + R1 (R2=A) 1A: Add R1  R1 + R0 (R1=5) 1B: Add R2  R2 + R1 (R2=F) 1C: Add R1  R1 + R0 (R1=6) 1D: Add R2  R2 + R1 (R2=15) 1E: halt Algorithm: Initialize the ‘current number’ to 1. Keep incrementing the ‘current number’ by 1 until it reaches 6, and keep adding it to a running total

15 Arithmetic/Logic Instructions Opcode 2 : Subtract Similar to Add… Encoding: Effect: Register A gets the value of Register B - Register C 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left 2 regA regB regC

16 Control Instructions Opcode 6 : Jump if Positive Jump to a different place in memory if Register is > 0 Encoding: Effect: If Register A > 0, Go To Instruction at specified address (I.e. change IP to address) 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left 6 regA address (8 bits)

17 How to Simplify the Program Wrote the same instruction pairs 6 times What if we were adding from 1 to 15000? Can we reduce this? Solution: Loops Implemented with jump (or branch) instructions Conditionally change IP to jump back to earlier point in program

18 Adding Numbers Revisited Use a Loop, with a simple jump instruction, to calculate 1 + 2 + 3 + 4 + 5 + … + N 10: Load R1  0006 (N is 6) 11: Load R2  0000 (running total) 12: Load R0  0001 (always 1) 13: Add R2  R2 + R1 (add in N) 14: Sub R1  R1 - R0 (N = N-1) 15: Jump to 13 if (R1>0) (If N isn’t 0 yet, go back) 16: halt (N is now 0, and R2 = 1+2+…+N) Notice: Decrements, instead of incrementing, current number

19 Advanced Control Instructions Opcode 7 : Jump and count (backwards) Jump to a different place in memory if Register value is > 0, AND decrement Register by 1 Encoding: Effect: If Register A > 0, Go To Instruction at specified address and set A = A - 1 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left 7 regA address (8 bits)

20 Adding Numbers Revisited Alternate Loop using Jump & Count for 1 + 2 + 3 + 4 + 5 + … + N 10: Load R1  0006 (N) 11: Load R2  0000 (running total) 12: Add R2  R2 + R1 (add in N) 13: If (R1>0), Jump to 12 (If N isn’t 0 yet, and decrease R1 Let N=N-1, and go back) 14: halt (N is now 0, and R2 = 1+2+…+N) No need for R0, which stored increment/decrement ‘size’ of 1, and no need for subtract instruction: these are incorporated in Jump&Count instruction

21 Memory Instructions Opcode 9 : Load (from memory) Load from Memory into Register Encoding: Effect: Load data from RAM at specified address into Register A 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left 9 regA address (8 bits)

22 Memory Instructions Opcode A : Store (into memory) Store Register into Memory Encoding: Effect: Store contents of Register A into memory at specified address 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left A regA address (8 bits)

23 Memory Instructions: Indirect Opcode 9 : Load (from memory) Load from Memory into Register Encoding: Effect: Load from RAM at address [B+C] into Register A 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left 9 regA regB regC

24 How can CPU tell these apart? Sneaky trick: CPU has 8 Registers, so only need 3 bits to specify Register A Use extra bit to tell which type of LOAD Memory Instructions: Indirect 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left 9 regA regB regC 9 regA address (8 bits)

25 This is called a HACK! Hacks are terrible! (not all) Generally should be avoided. Sadly, they are everywhere. Some people get perverse pleasure out of hacks. Hacks are a major source of bugs! Hacks 0: halt 1: add 2: subtract 3: multiply 4: bus output 5: jump 6: jump if positive 7: jump & count 8: bus input 9: load A: store B: load direct/addr. C: NAND D: AND E: Shift Right F: Shift Left

26 A Virus! Copies itself on top of other programs! 10: Load R1  0006 (Length of Virus) 11: Load R2  001F (Memory Location to copy self To -1) 12: Load R3  000F (Memory Location to copy self From -1) 13: Load R0  Address[R3+R1] (Load last instruction from source) 14: Store Address[R2+R1]  R0 (Store to last instr in destination) 15: If (R1>0), Jump to 13 (If haven’t copied whole virus, and decrease R1 decrease R1, and go back) 16: halt (Virus has replicated)... ______________________________________________________________ 20:.... (Program about to be destroyed.) 21:.......

27 We have looked at a typical Machine Language and how instructions are represented. Halt Instruction Data Instructions Arithmetic/Logic Instructions Control Instructions Memory Instructions Summary

28 We saw a few simple examples of programs you can write in Assembly Language. You now have a pretty solid understanding of how computers really work ! But you don’t want to write programs in Assembly Language Modern programming. Writing programs in high- level languages. Start with traffic light example, in software rather than in hardware Summary (cont.)

29 Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that your rules are complete Translate the rules into the computer language –Build structures to hold your data –Build tools to manipulate the structures Make sure that the program does what the rules say

30 Elements of Programming We looked at machine language –Single instructions that tell the hardware what to do –Primitive Arithmetic, simple branching, communication with memory We built state machines –States using memory –Transitions modeling tasks –A “hardwired” program

31 Elements of Programming We’ve seen –Truth tables –Logic gates –States and transitions in a state machine –Machine language Now, higher level programming language

32 To build a computer program Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that your rules are complete Translate the rules into the computer language –Build structures to hold your data –Build tools to manipulate the structures Make sure that the program does what the rules say

33 Figuring out the rules For traffic lights: –We stored data that told us the current color of lights –We read input from sensors –We had rules that told us whether to change state –We had rules that told us how to change state

34 Light A Traffic Light Behavior IF A=1 AND B=0 Always IF A=0 AND B=1 Otherwise Light B Otherwise Always

35 Turn Memory, Inputs and Outputs Into Variables Store data to tell current color of lights –Dim LightA, LightB as Integer 0 for red, 1 for yellow, 2 for green Read input from sensors –Dim SensorA, SensorB as Integer tell if cars are waiting

36 Turn Rules Into Statements Decide whether to change state –If LightA = 0 And LightB = 2 And SensorA = 1 And SensorB = 0 Then here we want to specify that the colors change –If LightA = 2 And LightB= 0 And SensorA = 0 And SensorB = 1 Then again, we want to specify that the colors change

37 Build shell of program Dim LightA, LightB as Integer Dim SensorA, SensorB as Integer If LightA = 2 And LightB = 0 And SensorA = 0 And SensorB = 1 Then ChangeGreenToYellow(LightA) ChangeYellowToRed(LightA) ChangeRedToGreen(LightB) If LightA = 0 and LightB = 2 And SensorA = 1 And SensorB = 0 Then ChangeGreenToYellow(LightB) ChangeYellowToRed(LightB) ChangeRedToGreen(LightA)

38 Some Rules Statements have to be in blocks How does the computer know that the instructions are If LightA = 2 And LightB = 0 And SensorA = 0 And SensorB = 1 Then ChangeGreenToYellow(LightA) ChangeYellowToRed(LightA) ChangeRedToGreen(LightB) And not … If LightA = 2 And LightB = 0 And SensorA = 0 And SensorB = 1 Then ChangeGreenToYellow(LightA) ChangeYellowToRed(LightA) ChangeRedToGreen(LightB)

39 Some Rules Statements have to be in blocks If LightA = 2 And Light B = 0 And SensorA = 0 And SensorB = 1 Then ChangeGreenToYellow(LightA) ChangeYellowToRed(LightA) ChangeRedToGreen(LightB) End If


Download ppt "Machine Languages Different types of CPU’s understand different instructions Pentium family / Celeron / Xeon / AMD K6 / Cyrix … (Intel x86 family) PowerPC."

Similar presentations


Ads by Google