GATE 2017 (Session 1)
64 questions
MCQ 44 · NAT 201-mark 30 · 2-mark 34
Questions 61–64 of 64
Consider the expression ( a-1)* (((b + c) / 3) + d)) . Let X be the minimum number of registers required by an optimal code generation (without any register spill) algorithm for a load/store architecture in which (i) only loads and store instructions can have memory operands and (ii) arithmetic instructions can have only register or immediate operands. The value of X is _________.
Answer: 2
On a load/store machine an arithmetic instruction works only on registers or immediate values. So needs and in two registers at the same time. This means 1 register is not enough.
Two registers are enough if the larger subtree is evaluated first:
R1 = b, R2 = c, R1 = R1 + R2 (R2 is free again), R1 = R1 / 3 (3 is an immediate), R2 = d, R1 = R1 + R2
R2 = a, R2 = R2 1, R1 = R2 * R1
At no point are more than 2 registers in use.
Answer: 2
Two registers are enough if the larger subtree is evaluated first:
R1 = b, R2 = c, R1 = R1 + R2 (R2 is free again), R1 = R1 / 3 (3 is an immediate), R2 = d, R1 = R1 + R2
R2 = a, R2 = R2 1, R1 = R2 * R1
At no point are more than 2 registers in use.
Answer: 2
Consider the following C program.
#include <stdio.h>
#include <string.h>
void printlength (char *s, char *t) {
unsigned int c = 0;
int len = ((strlen(s) - strlen (t)) > c) ? strlen(s): strlen(t);
printf ("%d\n", len);
}
void main ( ) {
char *x = "abc";
char *y ="defgh";
printlength (x,y);
} Recall that strlen is defined in string.h as returning a value of type size_t, which is an unsigned int. The output of the program is _____________.Answer: 3
strlen returns an unsigned value, so in 3 5 the result is not 2. It wraps around and becomes a very large positive number, which is 0. So len = strlen(s) = 3.
Answer: 3
Answer: 3
A cache memory unit with capacity of N words and block size of B words is to be designed. If it is designed as a direct mapped cache, the length of the TAG field is 10 bits. If the cache unit is now designed as a 16-way set-associative cache, the length of the TAG field is ______bits.
Answer: 14
A direct mapped cache has N/B lines. A 16-way set-associative cache has (N/B)/16 sets. This means there are 16 = times fewer sets the index loses 4 bits, and these 4 bits go to the tag.
Tag = 10 + 4 = 14 bits
Answer: 14
Tag = 10 + 4 = 14 bits
Answer: 14
The output of executing the following C program is ________.
#include <stdio.h>
int total (int v) {
static int count = 0;
while (v) {
count += v & 1;
v >>= 1;
}
return count;
}
void main ( ) {
static int x = 0;
int i = 5;
for (; i> 0; i--) {
x=x + total (i) ;
}
printf ("%d\n", x) ; } Answer: 23
The function total(v) counts the 1-bits in v and adds them to static count. The count is never reset, and the function returns it.
- i = 5 (101): count = 2 x = 2
- i = 4 (100): count = 3 x = 5
- i = 3 (11): count = 5 x = 10
- i = 2 (10): count = 6 x = 16
- i = 1 (1): count = 7 x = 23
Answer: 23
- i = 5 (101): count = 2 x = 2
- i = 4 (100): count = 3 x = 5
- i = 3 (11): count = 5 x = 10
- i = 2 (10): count = 6 x = 16
- i = 1 (1): count = 7 x = 23
Answer: 23