Computer Organization and Design--计组作业习题(2)

Computer Organization and Design

 

  ----------------------个人作业,如果有后辈的作业习题一致,可以参考学习,一起交流,请勿直接copy

Problem 1:

 

Consider the following MIPS assembly program:

 

0: lbu $5,100($0)

1: sh $5,-42($5)

2: lh $4,2($8)

3: sw $4,101($0)

 

Recall that register $0 always contains the value zero. The memory is organized in big-endian format. The initial value for register 8 is: $8 = 0x00000064

 Show the state of the memory and of the two 32-bit registers ($4 and $5) after executing each instruction of the snippet above. The initial values for register file and memory are provided. Fill in the following tables to show your answers (Leave a box empty if its value is the same as the previous one):

 

Reg

Initial

After inst 0

After inst 1

After inst 2

After inst 3

$4

0x00000020

 

 

0xFFFFC700

 

$5

0x00000074

0x00000091

 

 

 

 

Memory address

Initial Value

After inst 0

After inst 1

After inst 2

After inst 3

10010

0x91

 

 

 

 

10110

0x34

 

 

 

 0xFF

10210

0xC7

 

 

 

 0xFF

10310

0x84

 

 0x00

 

 0xC7

10410

0x57

 

 0x00

 

 0x00

10510

0xB3

 

 

 

 

 

 

Problem 2 (7 points)

Given the following C variable declarations: 

 

char ee; 

short cs; 

struct { 

  short x3; 

  struct { 

    char *x7; 

    double x0[3]; 

    int r; 

  } u; 

  char l[2]; 

  float *e; 

} s;

 

Assuming the data memory starts at location 100 (decimal), answer the following questions: 

a. Fill in the size, start and end address for each variable in the table below for the MIPS architecture using the native data type sizes and alignment rules discussed in class. (5 pts)

Variable

size(bytes)

starting address

ending address

ee

 1

 100

  100

cs

 2

 102

  103

s

 56

 104

  159

x3

 2

 104

  105

u

 40

 112

  151

x7

 1

 112

  112

x0[0]to x0[2]

 24

 120

  143

r

 4

 144

  147

l[0] to l[1]

 2

 152

  153

e

 4

 156

  159

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

b. Calculate the total memory used for the data declarations. (0.5 pts)

         total memory:(159-100)+1=60bytes;

c. Suggest a reorganization of the above data structures to save maximum possible amount of memory noting that you cannot add or remove fields from the structs. How much memory can be saved? (Hint, the structs can be reorganized such that there is no wasted memory). (1.5 pts)

 

short cs;        ------   100-101

char ee;        ------   102-102

struct {                         

  short x3;     ------   104-105

  char l[2];     ------   106-107

  float *e;  ------   108-111

  struct { 

    double x0[3];   ------   112-135

    int r;      ------ 136-139

    char *x7;  ------ 140-140

  } u;           ------ 112-143

} s;                            ------        100-143

 

total memory:(143-100)+1=44bytes;  

 so,16bytes are saved;

 

Problem 3 (5 points)

Convert the following C code into MIPS assembly code by filling in the blanks below. Assume that the variable data is an array of integers starting at address 400.

 

int index = 100, counter = 0;

while ( index > 0 ) {

  if ( data[index] == data[index+3] ) {

    counter+=5;

  }

  else {

    counter-=5;

  }

  index--;

}

 

MIPS Assembly Code:

 

    addi  $1, __$0_, 100

    addi  $2, $0, 0

loop     ble   $1, $0, __exit__

    addi  $3, $1, 0

    sll   $3, $3, __2__

    lw    $4, 400(__$3__)

    addi  $3, $3, __12__

    lw    $3, ___400__($3)

    bne   $3, $4, __diff___

    addi  $2, $2, __5___

    beq   $0, $0, next

diff       addi  $2, $2, __-5___

next     addi  _$1__, _$1__, -1       

    beq  $0, $0, loop

exit  halt

Problem 4 (8 points)

You are using the “Great Old Compiler”(GOC) to compile the three functions below. function1 is called from main once, while function2 and function3 are both called from function1 only. Relevant snippets of the functions‟ code, including all the calls to other functions is shown below. The architecture that GOC is targeting has 2 caller-saved registers and 2 callee-saved registers 

 

int function1(int tintin){ 

  int a,b,c,d; 

  a = 15; 

  b = 11; 

  function2(a); 

  c = a + b; 

  while(a > 0) { 

    //iterates 15 times 

    function3(a); 

    a--; 

  } 

  d = 9; 

  d = 8 + tintin; 

  d++; 

  return 0; 

 

int function2(int haddock){ 

  int k,l,m; 

  k = 17; 

  m = 18; 

  printf(“captain\n”); 

  l = k + m; 

  l = l / 2; 

  return 0; 

}

 

int function3(int snowy){ 

  int w,x,y; 

  w = 1; 

  x = 2; 

  y = w + x; 

  printf(“dog\n”) ; 

  y = w; 

  return 0; 

}

 

GOC has mapped the local variables of the functions as follows: 

 Function

In caller saved register

In callee saved register

function 1

a,b

c,d

function 2

k,l

m

function 3

w,y

x

 

(a) How many executed save/restore instruction pairs exist in the assembly code of function1 ONLY for the purpose of preserving register values across function calls? Show your calculation per variable. (2 pts)

(b) Can you do better than GOC? Write down the number of executed S/R operation pairs per function basis in the table, if a variable is mapped to caller saved register or in a callee saved register. Provide the best assignment to caller or callee-saved registers on a per-register basis. If caller and callee-save has the same performance for a variable, then put “either” for your answer. State the total number of save/restore instruction pairs inserted by the compiler under the best assignment. (6 pts)

 

a:16 pairs;

b:1 pairs;

c:1 pairs;

d:1 pairs;

 

Tatal:2+2+1=5 pairs;

 

variable

Caller-save S/R op pairs

Callee-save S/R op pairs

Answer

function1

a

 16

 1

 callee

b

 1

 1

 either

c

 0

 1

 caller

d

 0

 1

 caller

function2

k

 1

 1

 either

l

 0

 1

 caller

m

 1

 1

 either

function3

w

 1

 1

 either

x

 0

 1

 caller

y

 0

 1

 caller

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Problem 5 (7 points)

Given the following C code in a file called awesome.c. Note that sizeof is not a function call, the compiler evaluates sizeof to a constant before it actually compiles the code.

 

0 int g;

1 int func1(char inp) {

2   int *data = malloc( 50*sizeof(int) );

3   static char c;

4   c = ‟b‟;

5   data[3] = g + 5;

6   bar(c);

7   return g;

8 }

9

10 void func2(char arg) {

11   printf(“%c”, arg);

12 }

 

(a) Indicate the memory region where the following variables/strings are stored (static, stack, heap, text) (3.5 pts)  

  

*data

 heap

g

 static

arg

 stack

inp

 stack

c

 static

%c

 Stack

 

(b) Circle all symbols that are placed in the symbol table of awesome.o (2.5 pts)

 

func1  inp  c  arg  data  func2

 

g  printf malloc  

 

(c) Please list any two line numbers that will have entries in the relocation table: (1 pt)

      

       Line : 5 ,6

转载于:https://www.cnblogs.com/nanashi/p/6661627.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值