Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 1: The assembly language we used in Homework 1 technically has enough features to program any (integer, numerical) algorithm, but not necessarily conveniently. One

image text in transcribed

image text in transcribed

Problem 1: The assembly language we used in Homework 1 technically has enough features to program any (integer, numerical) algorithm, 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): 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): What are some limitations of the calling convention this code seems to be using? (d): Draw the call stack as it would look during the execution of the first multiply cal 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 Problem 1: The assembly language we used in Homework 1 technically has enough features to program any (integer, numerical) algorithm, 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): 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): What are some limitations of the calling convention this code seems to be using? (d): Draw the call stack as it would look during the execution of the first multiply cal 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 3 Lnai 6323

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

3642159389, 978-3642159381

More Books

Students also viewed these Databases questions