Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Systems – Machine & Assembly code. Objectives Machine Code Assembly Language Op-code Operand Instruction Set.

Similar presentations


Presentation on theme: "Computer Systems – Machine & Assembly code. Objectives Machine Code Assembly Language Op-code Operand Instruction Set."— Presentation transcript:

1 Computer Systems – Machine & Assembly code

2 Objectives Machine Code Assembly Language Op-code Operand Instruction Set

3 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

4 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 codeAssembly 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

5 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) 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)

6 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

7 Machine Instruction You'll notice that in general instructions have two main parts: opcode - instruction name operand - data or address You'll notice that in general instructions have two main parts: opcode - instruction name operand - data or address

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

9 Machine Instruction Addressing modes You might notice that some instructions use a # and others don't, # = number [no hash] = address # = 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

10 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 0000STOREAddressSTO 12 0001LOADNumberLDA #12 0010LOADAddressLDA 12 0100ADDNumberADD #12 1000ADDAddressADD 12 1111HALTNoneHALT

11 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 01100 1000 00110 Answer 0010 01100 1000 00110

12 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 29 Answer LOAD 7 ADD 9 STORE 29 Explain what the above code does:

13 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 HexadecimalExample 0000STOREAddress0STO 12 0001LOADNumber1LDA #12 0010LOADAddress2LDA 12 0100ADDNumber4ADD #12 1000ADDAddress8ADD 12 1111HALTNoneFHALT

14 Machine code in Hexadecimal Convert the following machine code into hexadecimal: 0001 00111011 0100 00001001 0000 00011110 1111 00000000 Answer 1 3B 4 9 0 1E F 0 Answer 1 3B 4 9 0 1E F 0

15 Key terms… Machine Code Assembly Language Op-code Operand Instruction Set


Download ppt "Computer Systems – Machine & Assembly code. Objectives Machine Code Assembly Language Op-code Operand Instruction Set."

Similar presentations


Ads by Google