COMS W4115 Programming Languages and Translators Homework Assignment 3
Submit this assignment online via Courseworks as a PDF file. Fill in or annotate this PDF or print it out, write on it, and scan it. Please keep your answers in the boxes.
Do this assignment alone. You may consult the instructor and the TAs, but not other students.
Name: Uni:
Copyright By PowCoder代写 加微信 powcoder
1. (20pts.)ForthefollowingCarrayonaprocessorwiththeusualalignmentrules,
int a[2][3];
(a) Showtheorderinwhichitselementsarearrangedinmemory.
(b) Writeanexpressionforthebyteaddressofa[i][j]intermsofa(theaddressofthestartofthearray),i,andj.
(c) Verifypartsa)andb)bywritingasmallCprogramthattestsyourhypothesis.Examinetheassemblylanguageoutputwith the C compiler¡¯s -S flag (e.g., gcc -O -S array.c). Such a program should be simple and contain and access such an array, but not be so simple that the compiler optimizes most of it away. On the next page, include in an annotated assembly listing that explains how it verifies your hypothesis. Make sure the assembly listing is no more than about 40 lines, either by simplifying your program or trimming the output.
C program:
Assembly listing:
2. (20 pts.) For a 32-bit little-endian processor with the usual alignment rules, show the memory layout and size in bytes of the following three C variables.
union { struct {
char a; /* 8-bit */ int b; /* 32-bit */ short c; /* 16-bit */
Size in bytes:
Size in bytes:
Size in bytes:
}s; struct {
int d; /* 32-bit */
short e; /* 16-bit */ }t;
struct { char a;
int b; short short
struct { short
a; char b;
short c ; char d; short e ;
3. (20 pts.) Draw the layout of the stack just before bar is called in foo. Indicate storage for function arguments, local variables, return addresses, and stored frame pointers. Indicate where the stack and frame pointers point.
void bar(int x, int y, int z);
void foo(int a, int b) { int d, e;
bar(2, 5, 7);
4. (20pts.)Drawthelayoutsofs1ands2andthevirtualtablesfortheEllipseandSquareclasses.
public class Shape { double x, y;
public double area() { … } }
class Ellipse extends Shape { private double height , width ; public double area() { … }
class Square extends Shape { private double width ;
public double area () { . . . }
public class Main {
public static void main() {
Shape s1 = new Square(10, 3, 14);
Shape s2 = new Ellipse(3,
Square Virtual Table:
Ellipse Virtual Table:
s1 object:
s2 object:
8, 2, 6); System.out.println( s1.area() );
5. (20pts.)FortheprogrambelowwritteninaC-likelanguagewithnestedfunctiondefinitions,
void main() { intx=5;
void bar () { x=x+2;
void foo () { intx=8;
bar ( ) ; printf(“%d\n”, x);
foo(); /* Body of main() */ }
What would it print if the language used static scoping?
What would it print if the language used dynamic scoping?
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com