I want to understand difference between ISR (Interrupt Service Routine) and Function call.
I feel both the function call and ISR are the same from the hardware perspecti
They are not necessarily the same as you statet in the first point on ISRs: Interrupts are asynchronous and therefore have to somehow 'interrupt' the work of the main processor(s).
For example, lets look at this MIPS code decorated with addresses, which does not make anything useful:
4000. add $1, $2, $3
4004. sw $ra, 0($sp)
4008. jal subr # function call, sets $ra to 4012 and jumps to 4024
4012. lw $ra, 0($sp)
4016. jr $ra
4020.
4024. subr: sub $2, $1, $3
4028. jr $ra
This code can be handeled from the main processor: the arithmetic operations (lines 1, 7) are done by the arithmetic unit, memory access (lines 2, 4) by the memory controller, and the jumps (lines 3, 5, 8) are done by the main cpu as well. (The actual address of jal
is set during binding of the object file.)
This is for function calls. At any time it is determined, where the code is right now and which code is executed at the next point in time (i.e. when the programm counter is incremented: PC+=4).
Now there comes the point, when your functions do something complicated but you still want the software to react on a key stroke. Then a so called coprocessor comes into play. This coprocessor waits until some event (like a key stroke on your keyboard) occurs, and then calls the interrupt handler. This is a block of code located on a certain address in the memory.
Think, the processor is in the computation above, but in the meantime you want to store the number of key strokes on address keys
. Then you write a programm starting at address 0x80000180
(this is defined as the exeption handler address in MIPS):
lw $at, keys
addi $at, $at, 1
sw $at, keys
eret
Now what happens at a keystroke?
eret
the PC is set to the PC of the main processor before the interrupt occuredHere there is a switch from normal execution to interrupt handling between steps 2 and 3 and back again from 4 to 5.
Note: I have simplified this a lot, but it should be clear, how interrupts are different from function calls, and how the hardware has to have additionall capabilities for interrupt handling.