1 / 39

Machine Languages

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)

brigit
Download Presentation

Machine Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Machine Languages • Different types of CPU’s understand differentinstructions • 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 28 = 256 addresses • Total Memory: 256*2 = 512 bytes

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

  5. 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 1 0 1 1 0 0 1 1 1 8 9 A B C D E F 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1 Recall Hexadecimal

  6. 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 1 0 1 1 0 0 1 1 1 8 9 A B C D E F 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1 Hex Shorthand e.g. 0110 1100 1010 0101

  7. 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 1 0 1 1 0 0 1 1 1 8 9 A B C D E F 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1 Hex Shorthand e.g. • 0110 1100 1010 01016 C A 5

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

  9. Representing Instructions • Machine instructions will also be representedby 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 fewin 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 machineto 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 0 0 F F F 0 9 A C

  12. Data Instructions • Opcode B: Load Direct / Address • Sets a Register to a fixedSpecified Value • Encoding: • Effect: Register A becomesthe 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 registerstogether and puts result in thirdregister • Encoding: • Effect: Register A becomesRegister 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 calculate1 + 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 placein memory if Register is > 0 • Encoding: • Effect: If Register A > 0,Go To Instruction at specifiedaddress (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 specifiedaddress 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 for1 + 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 atspecified address intoRegister 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 ofRegister A into memoryat 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 ataddress [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. Memory Instructions: Indirect • How can CPU tell these apart? • Sneaky trick: • CPU has 8 Registers,so only need 3 bits to specifyRegister A • Use extra bit to tell which type of LOAD 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) 9 regA regB regC

  25. Hacks • This is called a HACK! • Hacks are terrible! (not all)Generally should be avoided. • Sadly, they are everywhere. • Some people get perversepleasure out of hacks. • Hacks are a major source of bugs! 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. Summary • We have looked at a typical Machine Languageand how instructions are represented. • Halt Instruction • Data Instructions • Arithmetic/Logic Instructions • Control Instructions • Memory Instructions

  28. Summary (cont.) • We saw a few simple examples of programsyou can write in Assembly Language. • You now have a pretty solid understandingof 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

  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. Traffic Light Behavior Otherwise IF A=1 AND B=0 Light A Always Always IF A=0 AND B=1 Otherwise Light B

  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

More Related