Title: Machine Languages
1Machine Languages
- Different types of CPUs 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) - Lets look instructions for a simple example CPU
2An Example CPU
- Well look at a toy CPUs Machine Language
- Our CPU has
- 8 Registers each holds 16 bits (2 bytes)
3Our Example CPU
- Well look at a toy CPUs 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 2562 512 bytes
4Writing 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
5Recall Hexadecimal
6Hex Shorthand
e.g.
0110 1100 1010 0101
7Hex Shorthand
e.g.
- 0110 1100 1010 0101 6 C A
5
8Machine Core
- Everything is in binary (hex)
Registers
R0 0000 R1 0CA8 R2 A9DB R3 0705 R4 1011 R5
90A0 R6 0807 R7 00A0
9Representing Instructions
- Machine instructions will also be representedby
16 bits. - Well 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
10Instructions
- 16 possible Op-Codes
- Well 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
11Halt 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
12Data 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)
13Arithmetic/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
14Adding 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
(R21) 14 Add R1 ? R1 R0 (R12) 15 Add
R2 ? R2 R1 (R23) 16 Add R1 ? R1 R0
(R13) 17 Add R2 ? R2 R1 (R26) 18 Add
R1 ? R1 R0 (R14) 19 Add R2 ? R2 R1
(R2A) 1A Add R1 ? R1 R0 (R15) 1B Add
R2 ? R2 R1 (R2F) 1C Add R1 ? R1 R0
(R16) 1D Add R2 ? R2 R1 (R215) 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
15Arithmetic/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
16Control 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)
17How 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
18Adding 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 (R10) (If N isnt
0 yet, go back) 16 halt (N
is now 0, and R2 12N)
- Notice Decrements, instead of incrementing,
current number
19Advanced 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)
20Adding 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 (R10), Jump to 12
(If N isnt 0 yet, and decrease R1 Let
NN-1, and go back) 14 halt
(N is now 0, and R2 12N)
- No need for R0, which stored increment/decrement
size of 1, and no need for subtract
instruction these are incorporated in JumpCount
instruction
21Memory 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)
22Memory 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)
23Memory Instructions Indirect
- Opcode 9 Load (from memory)
- Load from Memory into Register
- Encoding
- Effect Load from RAM ataddress BC 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
24Memory 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
25Hacks
- 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
26A 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 ? AddressR3R1 (Load last
instruction from source) 14 Store
AddressR2R1 ? R0 (Store to last instr in
destination) 15 If (R10), Jump to 13 (If
havent copied whole virus, and decrease R1
decrease R1, and go back) 16 halt
(Virus has replicated) ...
__________________________________________________
____________ 20 . . . .
(Program about to be destroyed.) 21 . . . .
...
27Summary
- We have looked at a typical Machine Languageand
how instructions are represented. - Halt Instruction
- Data Instructions
- Arithmetic/Logic Instructions
- Control Instructions
- Memory Instructions
28Summary (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 dont 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
29Writing 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
30Elements 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
31Elements of Programming
- Weve seen
- Truth tables
- Logic gates
- States and transitions in a state machine
- Machine language
- Now, higher level programming language
32To 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
33Figuring 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
34Traffic Light Behavior
Otherwise
IF A1 AND B0
Light A
Always
Always
IF A0 AND B1
Otherwise
Light B
35Turn 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
36Turn 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
37Build 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)
38Some 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)
39Some 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