SEP105 Dr. Benjamin Champion Deakin University CRICOS Provider Code: 00113B What is a Program? Deakin University CRICOS Provider Code: 00113B What is a program? You should have an appreciation of what a computer program is from modern day to day life. Essentially a program is a set of instructions that is executed by a Central Processing Unit (CPU) to complete a task or process. So is it that simple? From a high level, yes. But how does this actually work? Deakin University CRICOS Provider Code: 00113B What is a program? When we are writing software, regardless of the computer language that we are using, the program must be converted into machine code before it can be executed. This might be done at compile time of the program, or as the interpreter interprets the program. Machine code is the lowest possible form of code. Machine code is the 1’s and 0’s that make the computer work! While it is possible to write programs in machine code, you would almost never do this on a modern device as it is very slow to write, device specific, easy to make mistakes and can be difficult to debug Deakin University CRICOS Provider Code: 00113B Assembly Code In its simplest state, machine code consists of numbers that our CPU can interpret as instructions and addresses. In its human readable format, this is called assembly code. These instructions are typically for relatively simple tasks, like load a value from memory, add two values together, etc. Because of this, a PC cannot think! It can simply follow the instructions that it is given. These instructions can be combined together and executed very quickly to give a PC the illusion of intelligence but in reality, it is confined to the instructions that it is able to read from the predefined machine code. Deakin University CRICOS Provider Code: 00113B Instruction Function add Add two values together sub Subtract two one value from another ld Load data st Store data Push Push a value onto the stack Pop Pop (retrieve) a value from the stack jmp Jump to a different address (change the value of the PC) etc Fetch, Decode, Execute cycle Once the machine code has been developed, how is this code actioned on by the CPU? In most modern CPUs, the Fetch-Execute cycle is used (or a derivative there of). The Fetch Execute cycle can be described using a simplified model with only the following components: • Program Counter – This is a value stored in a register that knows the current memory address of the program. An instruction is loaded from this address. For the program to continue, the Program Counter must change (eg be incremented) or the same value will always be executed. • Instruction Register – This is the current instruction that is being worked on by the CPU • Accumulator Register – This is a temporary memory location that can have work done to it (eg store a value to add something to it) • Memory – This is somewhere where the program and values are being stored, like RAM • Clock – This is a signal that drives how fast the cycle can execute. Everything in a PC happens on a clock cycle, not in-between! Therefore, the faster the clock, the faster the PC will process commands. Deakin University CRICOS Provider Code: 00113B Fetch, Decode, Execute cycle The system might look something like this CPU MEMORY Program Counter (PC) Instructions Instruction Register (IR) Accumulator (Ac) Clock (CLK) Deakin University CRICOS Provider Code: 00113B Storage Fetch First the instruction is FETCHed from memory. The program counter is used to determine which instruction we should retrieve from memory. Initially, the program counter is set to 0. Therefore, on the first clock cycle the first instruction is loaded from memory into the instruction register. MEMORY CPU PC = 0x00 Instructions IR = ld 5 Ac CLK Deakin University CRICOS Provider Code: 00113B Storage Address Instruction 0x00 ld 5 0x01 add 6 0x02 st 5 0x03 jmp 0x00 0x04 4 0x05 23 0x06 7 0x07 45 Decode Next the instruction is DECODEd. Typically, an instruction will have what it wants to do, and where it wants to do it. On the second clock cycle, the CPU will get ready to execute the instruction from the specific memory location MEMORY CPU PC = 0x00 Instructions IR = ld 5 Ac CLK Deakin University CRICOS Provider Code: 00113B Storage Address Instruction 0x00 ld 5 0x01 add 6 0x02 st 5 0x03 jmp 0x00 0x04 4 0x05 23 0x06 7 0x07 45 Execute Next the instruction is EXECUTEd. At this point, the accumulator can be used to store the result. This may take several clock cycles, depending on the instruction that is being executed. MEMORY CPU PC = 0x00 Instructions IR = ld 5 Ac = 23 CLK Deakin University CRICOS Provider Code: 00113B Storage Address Instruction 0x00 ld 5 0x01 add 6 0x02 st 5 0x03 jmp 0x00 0x04 4 0x05 23 0x06 7 0x07 45 Fetch-Execute Cycle Finally, the program counter is incremented, and the Fetch command is repeated again. This will load in the next instruction from memory, and the process repeats until we run out of instructions to execute! MEMORY CPU PC = 0x01 Instructions IR = add 6 Ac = 23 CLK Deakin University CRICOS Provider Code: 00113B Storage Address Instruction 0x00 ld 5 0x01 add 6 0x02 st 5 0x03 jmp 0x00 0x04 4 0x05 23 0x06 7 0x07 45 Jump When we get to the end of the program, often we may want to go back to the start of the program. Or at some point in time we may want to run a different part of the program (eg function). To do this, we can use the jump command! Fetch MEMORY CPU PC = 0x03 Instructions IR = jmp 0x00 Ac = 30 CLK Deakin University CRICOS Provider Code: 00113B Storage Address Instruction 0x00 ld 5 0x01 add 6 0x02 st 5 0x03 jmp 0x00 0x04 4 0x05 30 0x06 7 0x07 45 Jump When we get to the end of the program, often we may want to go back to the start of the program. Or at some point in time we may want to run a different part of the program (eg function). To do this, we can use the jump command! Decode MEMORY CPU PC = 0x03 Instructions IR = jmp 0x00 Ac = 30 CLK Deakin University CRICOS Provider Code: 00113B Storage Address Instruction 0x00 ld 5 0x01 add 6 0x02 st 5 0x03 jmp 0x00 0x04 4 0x05 30 0x06 7 0x07 45 Jump When we get to the end of the program, often we may want to go back to the start of the program. Or at some point in time we may want to run a different part of the program (eg function). To do this, we can use the jump command! Execute MEMORY CPU PC = 0x00 Instructions IR = jmp 0x00 Ac = 30 CLK Deakin University CRICOS Provider Code: 00113B Storage Address Instruction 0x00 ld 5 0x01 add 6 0x02 st 5 0x03 jmp 0x00 0x04 4 0x05 30 0x06 7 0x07 45 Fetch-Execute Cycle Then the program can loop MEMORY CPU PC = 0x00 Instructions IR = ld 5 Ac = 30 CLK Storage Address Instruction 0x00 ld 5 0x01 add 6 0x02 st 5 0x03 jmp 0x00 0x04 4 0x05 30 0x06 7 0x07 45 Good explanation: https://www.youtube.com/watch?v=Z5JC9Ve1sfI&ab_channel=TomScott Deakin University CRICOS Provider Code: 00113B