machine languages different types of cpu’s understand different instructions pentium family /...

39
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

Post on 21-Dec-2015

229 views

Category:

Documents


0 download

TRANSCRIPT

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

An Example CPU• We’ll look at a “toy” CPU’s Machine

Language

• Our CPU has:

• 8 Registers – each holds 16 bits (2 bytes)

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

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

Recall Hexadecimal

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.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

0110 1100 1010 0101

Hex Shorthand

e.g.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

0110 1100 1010 0101

6 C A 5

Machine “Core”

• Everything is in binary (hex)

Registers

R0: 0000R1: 0CA8R2: A9DBR3: 0705R4: 1011R5: 90A0R6: 0807R7: 00A0

00: 0000 0000 0000 000004: 0000 0000 0000 000008: 0000 0000 0000 00000C: 0000 0000 0000 000010: B106 B200 B001 1221… …F8: 0000 0000 0000 0000FC: 0000 0000 0000 0000

Memory

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

Instructions

• 16 possible Op-Codes:

• We’ll only look at a fewin detail…

0: halt1: add2: subtract3: multiply4: bus output5: jump6: jump if positive7: jump & count8: bus input9: loadA: storeB: load direct/addr.C: NANDD: ANDE: Shift RightF: Shift Left

Halt Instruction

• Opcode 0: Halt

• This just tells the machineto stop.

• Other 12 bits are ignored. So:

All have same effect.

0: halt1: add2: subtract3: multiply4: bus output5: jump6: jump if positive7: jump & count8: bus input9: loadA: storeB: load direct/addr.C: NANDD: ANDE: Shift RightF: Shift Left

0 0 0 0

0 F F F

0 9 A C

Data Instructions

• Opcode B: Load Direct / Address

• Sets a Register to a fixedSpecified Value

• Encoding:

• Effect: Register A becomesthe specified value

0: halt1: add2: subtract3: multiply4: bus output5: jump6: jump if positive7: jump & count8: bus input9: loadA: storeB: load direct/addr.C: NANDD: ANDE: Shift RightF: Shift Left

B regA value (8 bits)

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: halt1: add2: subtract3: multiply4: bus output5: jump6: jump if positive7: jump & count8: bus input9: loadA: storeB: load direct/addr.C: NANDD: ANDE: Shift RightF: Shift Left

1 regA regB regC

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

Arithmetic/Logic Instructions• Opcode 2: Subtract

• Similar to Add…

• Encoding:

• Effect: Register A gets the value of Register B - Register C

0: halt1: add2: subtract3: multiply4: bus output5: jump6: jump if positive7: jump & count8: bus input9: loadA: storeB: load direct/addr.C: NANDD: ANDE: Shift RightF: Shift Left

2 regA regB regC

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: halt1: add2: subtract3: multiply4: bus output5: jump6: jump if positive7: jump & count8: bus input9: loadA: storeB: load direct/addr.C: NANDD: ANDE: Shift RightF: Shift Left

6 regA address (8 bits)

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

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

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: halt1: add2: subtract3: multiply4: bus output5: jump6: jump if positive7: jump & count8: bus input9: loadA: storeB: load direct/addr.C: NANDD: ANDE: Shift RightF: Shift Left

7 regA address (8 bits)

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

Memory Instructions

• Opcode 9: Load (from memory)

• Load from Memory into Register

• Encoding:

• Effect: Load data from RAM atspecified address intoRegister A

0: halt1: add2: subtract3: multiply4: bus output5: jump6: jump if positive7: jump & count8: bus input9: loadA: storeB: load direct/addr.C: NANDD: ANDE: Shift RightF: Shift Left

9 regA address (8 bits)

Memory Instructions

• Opcode A: Store (into memory)

• Store Register into Memory

• Encoding:

• Effect: Store contents ofRegister A into memoryat specified address

0: halt1: add2: subtract3: multiply4: bus output5: jump6: jump if positive7: jump & count8: bus input9: loadA: storeB: load direct/addr.C: NANDD: ANDE: Shift RightF: Shift Left

A regA address (8 bits)

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: halt1: add2: subtract3: multiply4: bus output5: jump6: jump if positive7: jump & count8: bus input9: loadA: storeB: load direct/addr.C: NANDD: ANDE: Shift RightF: Shift Left

9 regA regB regC

• 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

Memory Instructions: Indirect

0: halt1: add2: subtract3: multiply4: bus output5: jump6: jump if positive7: jump & count8: bus input9: loadA: storeB: load direct/addr.C: NANDD: ANDE: Shift RightF: Shift Left

9 regA regB regC

9 regA address (8 bits)

• 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!

Hacks

0: halt1: add2: subtract3: multiply4: bus output5: jump6: jump if positive7: jump & count8: bus input9: loadA: storeB: load direct/addr.C: NANDD: ANDE: Shift RightF: Shift Left

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: . . . . ...

• We have looked at a typical Machine Languageand how instructions are represented.

• Halt Instruction

• Data Instructions

• Arithmetic/Logic Instructions

• Control Instructions

• Memory Instructions

Summary

• 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

Summary (cont.)

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

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

Elements of Programming

• We’ve seen– Truth tables– Logic gates– States and transitions in a state machine– Machine language

• Now, higher level programming language

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

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

Light

ATraffic Light Behavior

IF A=1

AND B=0

Always

IF A=0

AND B=1

Otherwise

Light BOtherwise

Always

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

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

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 ThenChangeGreenToYellow(LightB)ChangeYellowToRed(LightB)ChangeRedToGreen(LightA)

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 ThenChangeGreenToYellow(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)

Some Rules

• Statements have to be in blocksIf LightA = 2 And Light B = 0 And SensorA = 0 And

SensorB = 1 Then

ChangeGreenToYellow(LightA)

ChangeYellowToRed(LightA)

ChangeRedToGreen(LightB)

End If