In: Computer Science
[Procedures] Write a main program which sets the registers BX and CX and calls a procedure Add-Two. The procedure Add-Two adds the values in registers BX and CX and returns the output (which is the sum) in AX. Single step through the program, displaying the value of the stack pointer so that you understand how the call and return are implemented.
I have used EMU8086 for runnign the code and viewing the stack.
Main Program
MOV BX,12H //Load 12h to BX
MOV CX,14H //Load 14h to CX
CALL AddTwo //Calling to procedure AddTwo
HLT //End of MAIN Program
Procudure
PROC AddTwo //Procudure
MOV AX,CX //Copies the content of CX to AX
ADD AX,BX //Add the contents of BX and CX(Cx is already copied into
AX)
RET //End of procedure and execution return to the main program
Snapshots during every single steps are given below with the explanation.
1. First instruction is ready to execute after the compilation.
Leftmost window shows the machine code loaded into the memory and the register contents. Top right box shows the compiled program and the yellow coloured highlightes instruction is the one which is the next instruction to be executed next.
Here the instruction to be executed is MOV BX,12h
In the register panel at the leftmost side,Instruction Pointer(IP) also pointing to the same instrucrtion by holding address 100(Base plus offset =7100). This is a 3 byte instruction so it takes 3 bytes in memory to store the corresponding machine code(B2,12,00) .
The right bottom side shows the stack and presently it is showing nothing. You can see some changes once the procedure call happens.
2. Second instruction is ready to execute
This instruction also is a 3 byte instruction and see the IP value?? It is 103. Still the stack is empty.
3. Third instruction is ready to execute and its a procedure call
This instruction calls the procedure using the instruction CALL. See the IP value...Its 106...
Once this is executed, the execution will start from the procedure and once it is completed, the control will go back to the main program. So how it will go back to the main program after the execution of procedure? There comes the need of stack.
When the main program call to this procedure, the next instruction address will be pushed on to the stack so that after the execution of the procedure, the execution control can proceed correctly from the next instruction after the CALL
4. Now the control is in procedure. Its ready to execute the first instruction in the procedure.
SEE THE STACK NOW. IT IS PUSHES WITH SOME VALUES. WHAT IS THAT?
Stack is having a value 109.
That is the offset address(Address of the instruction, immediately after the CALL instruction in the main program) to be used by the processor to go back to the main program. after the completion of the procedure.
The highlighted arrow shows the execution sequence. It starts with the begginning of main program and once the CALL instruction is executed, the control jumps to the procedure after pushing the return address on to the stack(The address of the next instruction after the CALL. In this case it is the address of HLT) and then it complete it.
5. Now Executing the first instructions in the procedure.
Once the procedure is done, the CPU copies the next address into the IP from the TOP OF THE STACK and execution starts from there. In this case it is 109, the address of HLT
6. Executing RET instruction that makes the system to copy the stacked address into IP and continues execution from there.
Completed the execution.