Header

Saturday 7 September 2013

IGNOU BCA 1st sem Solved Assignment -What is an instruction in the context of a Computer? Explain with the help of an example. How is an instruction executed by a computer?

What is an instruction in the context of a Computer? Explain with the help of an example. How is an instruction executed by a computer?
Ans
When referring to the computer processor, an instruction is a segment of code that contains steps that need to be executed by the computer processor. For example, if you were to ask the computer to draw a square it would need a set of instruction in order to complete the task. In logo, a user could complete this task by giving the computer the below set of instructions. In the picture to the right, is an example of the results
forward 100
right 90
forward 100
right 90
forward 100
right 90
forward 100
Computer instructions are a set of steps or documentation that includes information on how to operate, perform, or otherwise maintain particular computer software or hardware. If a program or hardware device includes an instruction manual or online instructions
A computer should have a set of instructions so that the user can construct machine language programs to evaluate any function that is known to be computable. The set of instructions are said to be complete if the computer includes a sufficient number of instructions.



Six Steps

The main purpose of a CPU is to execute instructions. We've already seen some simple examples of instructions, i.e., add and addi.
The CPU executes the binary representation of the instructions, i.e., machine code.
Since programs can be very large, and since CPUs have limited memory, programs are stored in memory (RAM). However, CPUs do its processing on the CPU. So, the CPU must copy the instruction from memory to the CPU, and once it's in the CPU, it can execute it.
The PC is used to determine which instruction is executed, and based on this execution, the PC is updated accordingly to the next instruction to be run.
Essentially, a CPU repeatedly fetches instructions and executes them.
The following is a summary of the six steps used to execute a single instruction.
Step 1: Fetch instruction
For some reason, the verb "fetch" is always used with instruction. We don't "get an instruction" or "retrieve an instruction". We "fetch an instruction".
To fetch an instruction involves the following steps:
CPU must place an address to the MAR.
CPU must activate the tri-state buffer so MAR contents are placed on the address bus.
CPU sends R/\W = 1 and CE = 1 to memory, to indicate it wants to do a read.
Memory eventually puts instruction on the data bus.
Memory sends ACK = 1.
CPU loads the instruction to the MDR.
CPU transfers instruction from MDR to IR.
CPU sets CE = 0 to memory indicate it's done with fetching the instruction.
As you can see, the steps are rather involved. You can speed up this step if you assume instructions are in a fast instruction cache. For now, we won't assume that.
You should go back to the notes on memory if you have forgotten how it works, in particular, if you have forgotten the control signals used by memory.
Step 2: Decode instruction and Fetch Operands
In the second step, the bits used for the opcode (and function, for R-type instructions) are used to determine how the instruction should be executed. This is what is meant by "decoding" the instruction.
Recall that operands are arguments to the assembly instruction.
However, since R-type and I-type instructions both use registers, and those registers are in specific locations of the instruction, we can begin to fetch the values within the registers at the same time we are decoding.
In particular, we're going to do the following:
Get IR31-26, the opcode
Get IR25-21, which is $rs, the first source register.
Get IR20-16, which is $rt, the second source register.
Get IR15-11, which is $rd, the destination register.
Get IR15-0, the immediate value
Get IR5-0, the function code
You'll notice that we're extracting these bits directly from the instruction register.
You'll also notice that we extracted IR15-11 and IR15-0. How can we do both? Well, they're merely wires, so there's no reason you can't get both quantitie out.
The key is to realize that sometimes we use IR15-11 and sometimes we use IR15-0. We need to have both of them ready because this is hardware. It's easier to have everything we need, and then figure out what we need, than to decide what we need and try to get it.
In particular, when we fetch the operands (i.e., the registers) we want to send the source and destination registers bits to a device called the register file.
For example, if IR25-21 has value 00111, this means we want register $r7 from the register file. We sent in 00111 to this circuit, and it returns the contents back to us.
We'll be discussing the register file soon.
If we are executing an I-type instruction, then typically, we'll sign-extend (or zero-extend, depending on the instruction) the immediate part (i.e., IR15-0) to 32 bits.
Step 3: Perform ALU operation
The ALU has two 32-bit data inputs. It has a 32-bit output. The purpose of the ALU is to perform a computation on the two 32-bit data inputs, such as adding the two values. There are some control bits on the ALU. These control bits specify what the ALU should do.
For example, they may specify an addition, or a subtraction, or a bitwise AND.
Where do the input values of the ALU come from?
Recall that an instruction stores information about its operands. In particular, it encodes registers as 5-bit UB numbers. These register encodings are sent to the register file as inputs.
The register file then outputs the 32-bit values of these registers. These are the sent as inputs to the ALU.
Step 4: Access memory
There are only two kind of instructions that access memory: load and store.
load copies a value from memory to a register. store copies a register value to memory.
Any other instruction skips this step.
Step 5: Write back result to register file
At this point, the output of the ALU is written back to the register file. For example, if the instruction was: add $r2, $r3, $r4 then the result of adding the contents of $r3 to the contents of $r4 would be stored back into $r2.
The result could also be due to a load from memory.
Some instructions don't have results to store. For example, branch and jump instructions do not have any results to store.
Step 6: Update the PC
Finally, we need to update the program counter. Typically, we perform the following update:
PC <- PC + 4
Recall that PC holds the current address of the instruction to be executed. To update it means to set the value of this register to the next instruction to be executed.
Unless the instruction is a branch or jump, the next instruction to execute is the next instruction in memory. Since each instruction takes up 4 bytes of memory, then the next address in memory is PC + 4, which is the address of the current instruction plus 4.
The PC might change to some other address if there is a branch or jump.
These are the six steps to executing an instruction. Not every instruction goes through every step. However, we label each step so that you can be aware they exist.
Some of these steps may not make much sense now, but hopefully, they're be clearer once we start implementing the steps in depth.

No comments:

Post a Comment