Answered step by step
Verified Expert Solution
Question
1 Approved Answer
THANKS!!! Problem 1: The assembly language we used in Homework 1 technically has enough features to program any algorithm that operates on integers, but not
THANKS!!!
Problem 1: The assembly language we used in Homework 1 technically has enough features to program any algorithm that operates on integers, but not necessarily conveniently. One feature it lacks is the ability to define functions. In all high-level programming languages, code can be put into reusable functions, which can be called with different parameters from different places in the program. To make functions possible in our language, we'll add two new instructions: CALL label Conceptually, call the function. Specifically: Jump to label, and push the address of this call site onto the call stack. RET Conceptually, return from the function. Specifically: Jump to the instruction immediately after the last call site that was saved on the call stack, and then pop (remove) that address from the call stack. Consider this program, which defines a function multiply and then computes 2*3*4 by calling multiply twice. JMP start multiply: LOAD 0, R3 LOAD -1,R4 loop: ADD R1,R3 ADD R4, R2 JGZ R2, loop MOV R3, R1 RET start: LOAD 2, R1 LOAD 3,R2 CALL multiply LOAD 4, R2 CALL multiply (a): Why did we need CALL? Couldn't we just JMP to multiply each time we wanted to use it? (b): Function calling at the assembly level only works if there is a calling convention where the caller and callee agree on where parameters will be passed, and where results will be stored. From looking at the example above, what can you infer about the calling convention? Explain the apparent) rules for how functions receive their parameters and return their values. (c): Can you think of any problems or limitations of the calling convention that this code seems to be using? (d): Draw the call stack as it would look during the execution of the first multiply call (i.e. after the first CALL, but before the first RET). Treat start as a function with zero parameters called at the start of the program, for the purposes of drawing the call stack (even though it isn't called with CALL). (e): Draw the call tree showing all function invocations that would happen during the execution of this program. A call tree should have one box (called the activation record) for each time a function was called, along with arrows showing who called who. Again, treat start as a function with zero parameters for this purpose. Extra credit: This implementation of multiply isn't quite correct, although it works fine for computing 2*3*4. That doesn't change what the calling convention or call stack looks like. But for extra credit, explain what's wrong with it, and provide a correct version that works for all integers. Problem 1: The assembly language we used in Homework 1 technically has enough features to program any algorithm that operates on integers, but not necessarily conveniently. One feature it lacks is the ability to define functions. In all high-level programming languages, code can be put into reusable functions, which can be called with different parameters from different places in the program. To make functions possible in our language, we'll add two new instructions: CALL label Conceptually, call the function. Specifically: Jump to label, and push the address of this call site onto the call stack. RET Conceptually, return from the function. Specifically: Jump to the instruction immediately after the last call site that was saved on the call stack, and then pop (remove) that address from the call stack. Consider this program, which defines a function multiply and then computes 2*3*4 by calling multiply twice. JMP start multiply: LOAD 0, R3 LOAD -1,R4 loop: ADD R1,R3 ADD R4, R2 JGZ R2, loop MOV R3, R1 RET start: LOAD 2, R1 LOAD 3,R2 CALL multiply LOAD 4, R2 CALL multiply (a): Why did we need CALL? Couldn't we just JMP to multiply each time we wanted to use it? (b): Function calling at the assembly level only works if there is a calling convention where the caller and callee agree on where parameters will be passed, and where results will be stored. From looking at the example above, what can you infer about the calling convention? Explain the apparent) rules for how functions receive their parameters and return their values. (c): Can you think of any problems or limitations of the calling convention that this code seems to be using? (d): Draw the call stack as it would look during the execution of the first multiply call (i.e. after the first CALL, but before the first RET). Treat start as a function with zero parameters called at the start of the program, for the purposes of drawing the call stack (even though it isn't called with CALL). (e): Draw the call tree showing all function invocations that would happen during the execution of this program. A call tree should have one box (called the activation record) for each time a function was called, along with arrows showing who called who. Again, treat start as a function with zero parameters for this purpose. Extra credit: This implementation of multiply isn't quite correct, although it works fine for computing 2*3*4. That doesn't change what the calling convention or call stack looks like. But for extra credit, explain what's wrong with it, and provide a correct version that works for all integersStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started