(PPTX, Unknown)

Download Report

Transcript (PPTX, Unknown)

Computer Systems
– Machine & Assembly code
Objectives
•
•
•
•
•
Machine Code
Assembly Language
Op-code
Operand
Instruction Set
Machine code
Machine code - simple instructions that are executed directly by the CPU
As we should hopefully already know, computers can only understand
binary, 1s and 0s. We are now going to look at the simplest instructions
that we can give a computer. This is called machine code
Machine code allows computers to perform the most basic, but essential tasks. For
this section we are going to use the Accumulator (you met this register earlier) to
store the intermediate results of all our calculations. Amongst others, the following
instructions are important for all processors:
LDA - Loads the contents of the memory address or integer into the
accumulator
ADD - Adds the contents of the memory address or integer to the
accumulator
STO - Stores the contents of the accumulator into the addressed location
Assembly code
Assembly code is the easy to read interpretation of machine code,
there is a one to one matching, one line of assembly equals one
line of machine code:
Machine code
Assembly code
000000110101
Store 53
Let's take a look at a quick coding example using assembly code.
LDA #23 ;loads the number 23 into the accumulator
ADD #42 ;adds the number 42 to the contents of the accumulator = 65
STO 34 ;save the accumulator result to the memory address 34
Instruction Set
Instruction set - the range of instructions that a CPU can execute
There are many different instructions that we can use in machine code, you
have already met three (LDA, ADD, STO), but some processors will be
capable of understanding many more. The selection of instructions that a
machine can understand is called the instruction set. Below are a list of
some other instructions that might be used:
ADD ;add one number to another number
SUB ;subtract one number to another number
INC ;increment a number by 1
DEC ;decrement a number by 1
MUL ;multiply numbers together
OR ;boolean algebra function
AND ;boolean algebra function
NOT ;boolean algebra function
XOR ;boolean algebra function
JNZ ;jump to another section of code if a number is not zero (used for loops and ifs)
JZ ;jump to another section of code if a number is zero (used for loops and ifs)
JMP ;jump to another section of code (used for loops and ifs)
Instruction Set
Let us look at a more complex example of assembly code instructions:
LDA #12 ;loads the number 12 into the accumulator
MUL #2 ;multiplies the accumulator by 2 = 24
SUB #6 ;take 6 away from the accumulator = 18
JNZ 6 ;if the accumulator <> 0 then goto line 6
SUB #5 ;take 5 away from the accumulator (this line isn't executed!)
STO 34 ;saves the accumulator result (18) to the memory address 34
Machine Instruction
You'll notice that in general instructions have two main parts:
opcode - instruction name
operand - data or address
Machine Instruction
Depending on the word size, there will be different numbers of bits
available for the opcode and for the operand.
There are two different philosophies at play, with some processors
choosing to have lots of different instructions and a smaller operand (Intel,
AMD) and others choosing to have less instructions and more space for
the operand (ARM).
• CISC - Complex Instruction Set Computer - more instructions allowing
for complex tasks to be executed, but range and precision of the
operand is reduced. Some instruction may be of variable length, for
example taking extra words (or bytes) to address full memory
addresses, load full data values or just expand the available
instructions.
• RISC - Reduced Instruction Set Computer - less instructions allowing
for larger and higher precision operands.
Machine Instruction
Addressing modes
You might notice that some instructions use a # and others don't,
# = number
[no hash] = address
LOAD #10
ADD #12
STORE 12
This code loads the number 10 into the
accumulator, then adds the number 12, it then
stores the result 22 into memory location 12
Machine code and instruction sets
There is no set binary bit pattern for different opcodes in an instruction
set. Different processors will use different patterns, but sometimes it
might be the case that you are given certain bit patterns that represent
different opcodes. You will then be asked to write machine code
instructions using them. Below is an example of bit patterns that might
represent certain instructions.
Machine
code
Instruction
Addressing
mode
Example
0000
STORE
Address
STO 12
0001
LOAD
Number
LDA #12
0010
LOAD
Address
LDA 12
0100
ADD
Number
ADD #12
1000
ADD
Address
ADD 12
1111
HALT
None
HALT
Machine code and instruction sets
Using the table on the last slide, provide
machine code to do the following:
LOAD 12
ADD #6
Answer
0010 1100
0100 0110
Machine code and instruction sets
Using the table on the last slide, provide assembly code for the
following machine code:
0001 00000111
0100 00001001
0000 00011110
Answer
LOAD 7
ADD 9
STORE 30
Explain what the above code does:
Machine code in Hexadecimal
People who work with machine code prefer to display in
Hexadecimal, this is because it takes less space than
binary. Errors are less likely to occur when dealing with
smaller data sets.
Machine
code
Instruction
Addressing
mode
Hexadecimal
Example
0000
STORE
Address
0
STO 12
0001
LOAD
Number
1
LDA #12
0010
LOAD
Address
2
LDA 12
0100
ADD
Number
4
ADD #12
1000
ADD
Address
8
ADD 12
1111
HALT
None
F
HALT
Machine code in Hexadecimal
Convert the following machine code into hexadecimal:
0001 00111011
0100 00001001
0000 00011110
1111 00000000
Answer
13B
49
Key terms…
•
•
•
•
•
Machine Code
Assembly Language
Op-code
Operand
Instruction Set