130 likes | 326 Views
MACHINE CODE. What is Machine Code?. Machine code - simple instructions that are executed directly by the CPU. 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.
E N D
What is Machine Code? • Machine code - simple instructions that are executed directly by the CPU
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 Accumulatorto store the intermediate results of all our calculations.
INSTRUCTIONS 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 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:
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 The code above is the equivalent of saying x = 23 + 42 in VB.NET.
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.
You'll notice that in general instructions have two main parts:
Addressing modes • You might notice that some instructions use a # and others don't, why? • # = number [no hash] = address
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).
Below is a list of some other instructions that might be used: ADD ; add one number to another number SUB ; subtract one number from another number INC ; increment a number by 1 DEC ; decrement a number by 1 MUL ; multiply numbers together OR ; booleanalgebra function AND ; booleanalgebra function NOT ; booleanalgebra function XOR ; booleanalgebra 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)
Let us look at a more complex example of assembly code instructions: