Machine code

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Machine code

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which form of instruction can the CPU run directly?
A Machine code
B Python source code
2. Machine code is written using...
A 0s and 1s (binary)
B English words and symbols
3. In a machine code instruction, what does the opcode show?
A The operation to carry out, such as add or load
B The data or address the instruction uses
4. In a machine code instruction, what does the operand hold?
A The name of the programming language
B The data or address to be used
5. A program written in a high-level language must be...
A Sent straight to the ALU as typed
B Translated into machine code before the CPU can run it

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
110110001 00000101 AThe CPU can only run machine code. Python code must be translated into machine code first.
2opcode BA machine code instruction written as 0s and 1s, which the CPU can run directly
3Each machine code instruction is split into an opcode (the operation) and an operand (the data or address) CThe opcode tells the CPU which operation to do. The operand is the data or address the operation uses.
4A program written in a high-level language must be translated into machine code before the CPU can run it DThe part of a machine code instruction that tells the CPU which operation to perform
5The CPU can run Python code directly without any translation. Esource code -> translator -> 01001101
6In an instruction, the operand tells the CPU which operation to do. Fopcode: 0001 operand: 0111

Definitions

machine code

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
instruction ← ______ + operand
2.
instruction ← opcode + _______
3.
instruction ← 00010101 // 8 ____
4.
1010 0011 // opcode: ____ operand: 0011
5.
code ← _________(source)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A machine_code = '10110021'
B opcode = machine_code[:4]
C operand = machine_code[4:]
D print(opcode, operand)
Question 2
A instruction = '00110010'
B opcode = instruction[4:]
C operand = instruction[4:]
Question 3
A source = 'ADD 5'
B translator = Assembler()
C machine_code = translator.run(source)
D cpu.run(source)
Question 4
A # 8-bit instruction
B opcode = 'ADD'
C operand = '1100'
D instr = opcode + operand
Question 5
A opcode = '0001'
B operand = '0101'
C instruction = opcode - operand
D print(instruction)

Answer Key

Machine code
Section A: Quick Questions
Q1
A — Machine code
Q2
A — 0s and 1s (binary)
Q3
A — The operation to carry out, such as add or load
Q4
B — The data or address to be used
Q5
B — Translated into machine code before the CPU can run it
Section B: Match the Pairs
Q1
10110001 00000101 → A machine code instruction written as 0s and 1s, which the CPU can run directly
Q2
opcode → The part of a machine code instruction that tells the CPU which operation to perform
Q3
Each machine code instruction is split into an opcode (the operation) and an operand (the data or address) → opcode: 0001 operand: 0111
Q4
A program written in a high-level language must be translated into machine code before the CPU can run it → source code -> translator -> 01001101
Q5
The CPU can run Python code directly without any translation. → The CPU can only run machine code. Python code must be translated into machine code first.
Q6
In an instruction, the operand tells the CPU which operation to do. → The opcode tells the CPU which operation to do. The operand is the data or address the operation uses.
Section C: Definitions
Q1
machine code — The binary (0s and 1s) form of instructions that the CPU can run directly. Each instruction has an opcode and an operand. Programs must be translated into it to run.
Section D: Fill in the Blanks
Q1
instruction ← opcode + operand
Q2
instruction ← opcode + operand
Q3
instruction ← 00010101 // 8 bits
Q4
1010 0011 // opcode: 1010 operand: 0011
Q5
code ← TRANSLATE(source)
Section E: Spot the Error
Q1
A — Machine code is made only of 0s and 1s. The digit 2 is not allowed, so the line should be '10110001'.
Q2
B — The opcode is the first part of the instruction, so it should be instruction[:4]. [4:] gives the operand, which is the data or address.
Q3
D — The CPU can only run machine code. The source code must be translated first, so it should be cpu.run(machine_code).
Q4
B — In machine code the opcode is written in binary, for example '0101'. The word ADD is assembly language, not machine code.
Q5
C — A machine code instruction is the opcode followed by the operand, joined together with +. The - operator cannot join the two binary strings.