3
$\begingroup$

A computer has 32-bit instructions and 12-bit addresses. Suppose there are 250 two-address instructions. How many one-address instructions can be formulated? Explain your answer.

Two address instructions are of the form:
$OPCODE$ $ADDRESS$ $ADDRESS$
One address instructions are of the form:
$OPCODE$ $ADDRESS$
The solution provided is:
There are 250 2-address instructions. There are only a total of 256 2-address instructions allowed if we have 32-bit instructions (two addresses take up 24 bits, leaving only 8 bits for the opcode). Looking at the 8 bit opcode, assume bit patterns 00000000 (0) through 11111001 (249) are used for the 250 two-address instructions. Then there are 6 bit patterns left for one address instructions. However, each one of these can use the remaining 12 bits gained from having only one operand, so we have $6 * 2^{12}$.

I dont understand the part starting from "However, each one of these can use the remaining 12 bits gained from having only one operand, so we have $6 * 2^{12}$."

According to me, each of those 6 bit patterns should be able to use 20 bits (32-12) but this gives incorrect answer. So whats wrong with my reasoning? Also according to the given solution total instructions comes out to be $6 * 2^{12} + 250$ which is far lesser than $2^{32}$. What are the remaining bits used for? Sorry if this is a stupid question but I just couldnt understand it even after lot of attempts.

  • 0
    That's a famous job-interview question.2017-01-24
  • 0
    @barakmanos Can you please explain the solution?2017-01-24
  • 0
    Yes, please see answer below...2017-01-24

1 Answers 1

1

This is a famous job-interview question (in different variations).

The processor must be able to distinguish between $1$-operand and $2$-operand instructions.

It can only do so according to some prefix within the instructions.


Every instruction which takes $2$ operands looks like this:

$$\underbrace{\text{operation}}_{8\text{ bits}}|\underbrace{\text{operand #}1}_{12\text{ bits}}|\underbrace{\text{operand #}2}_{12\text{ bits}}$$

Where the first $8$ bits contain values between $0$ and $249$.


Every instruction which takes $1$ operand looks like this:

$$\underbrace{\text{operation}}_{8+12\text{ bits}}|\underbrace{\text{operand #}1}_{12\text{ bits}}$$

Where the first $8$ bits cannot contain values between $0$ and $249$.

Hence the first $8$ bits can only contain values between $250$ and $255$.

Hence we can allow for at most $255-250+1=6$ such instructions.

  • 0
    Thanks for answering. I now understand the solution given by them. Can you also tell how can we calculate the total 1 address and 2 address instructions if there was no restriction like "only 250 2 address instructions"?2017-01-24
  • 1
    @SmittyBacall: Well, there are various options. It's like solving a system of $1$ equation in $2$ variables. You are free to choose the value of one of the variables, which then restricts the value of the other variable. The only thing to note here, is that both variables must be integers, so we have a finite number of solutions (but nevertheless, more than one solution, which is what you're asking).2017-01-24
  • 0
    @SmittyBacall: In other words, you still need to tell me the number of $1$-operand instructions that you want **OR** the number of $2$-operand instructions that you want (and then, given one of them, we can calculate the other).2017-01-24
  • 0
    Yeah that seems reasonable. Thanks for answering.2017-01-24
  • 0
    @SmittyBacall: You're welcome :)2017-01-24