Question

In: Computer Science

[Procedures] Write a main program which sets the registers BX and CX and calls a procedure...

[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.

Solutions

Expert Solution

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.


Related Solutions

Write a script that creates and calls a stored procedure named test. This procedure should identify...
Write a script that creates and calls a stored procedure named test. This procedure should identify all of the prime numbers less than 100. (A prime number is an integer that can't be divided by another integer other than 1 and itself.) Then, it should display a string variable that includes the prime numbers like this: 2 1 3 1 5 1 7 1 1 1 1 1 3 1 1 7 1 1 9 1 2 3 1 2...
SQL Code: Write a script that creates and calls a stored procedure named test. This procedure...
SQL Code: Write a script that creates and calls a stored procedure named test. This procedure should identify all of the prime numbers less than 100. (A prime number is an integer that can't be divided by another integer other than 1 and itself.) Then, it should display a string variable that includes the prime numbers like this: 2 1 3 1 5 1 7 1 1 1 1 1 3 1 1 7 1 1 9 1 2 3...
Write and assemble a program to load values into each of registers R20 – R24 and...
Write and assemble a program to load values into each of registers R20 – R24 and then push each of these registers onto the stack. Single-step the program, and examine the stack and the SP register after the execution of each instruction.
This program must have 9 functions: •main() Controls the flow of the program (calls the other...
This program must have 9 functions: •main() Controls the flow of the program (calls the other modules) •userInput() Asks the user to enter two numbers •add() Accepts two numbers, returns the sum •subtract() Accepts two numbers, returns the difference of the first number minus the second number •multiply() Accepts two numbers, returns the product •divide() Accepts two numbers, returns the quotient of the first number divided by the second number •modulo() Accepts two numbers, returns the modulo of the first...
Write a program to add 10 bytes of data and store the result in registers R30...
Write a program to add 10 bytes of data and store the result in registers R30 and R31. The bytes are stored in the Program memory starting at $200. The data would look as follows: MYDATA: .DB 92,34,84,129,... ;pick your own data.
Write a C++ program which consists of several functions besides the main() function. The main() function,...
Write a C++ program which consists of several functions besides the main() function. The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. A value-returning function called Power(int a, int b) that...
Write a JAVA program which prints the root node after the "remove" procedure the the splay...
Write a JAVA program which prints the root node after the "remove" procedure the the splay tree.
Task 1 Write a program that adds the three numbers stored in data registers at 0x20,...
Task 1 Write a program that adds the three numbers stored in data registers at 0x20, 0x30, and 0x40 and places the sum in data register at 0x50 task 4 Modify the program in Task1, so the program will run in infinite loop by using these following functions: GOTO function BRA function CALL function Simulate your program in PIC18 IDE Simulator and attach a screenshot of your simulation while the program is running.
Using the windows 32 framework , write an assembly language program ; write a procedure to...
Using the windows 32 framework , write an assembly language program ; write a procedure to read a string and shift each character of the string by one. As an example, if your input string is Abcz, your output should be Bcda. Note that you must test your string for non-alphabetic characters (such as numbers and special characters). If there are non-alphabetic characters, you should terminate your program with an appropriate message. You should display your converted string using the...
Write a program in Java that will take as input two sets A and B, and...
Write a program in Java that will take as input two sets A and B, and returns a list of all functions from A to B.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT