Program implementation with using specific CPU instructions

54 Views Asked by At

I want to realize following steps:

unsigned c = 5
c=c*5

I must do this by using this architecture and using 3 instructions at MOST:

CPU Architecture

Sample instructions for the cpu are like:

instructions

Extra info about the problem:

Suppose that the program is stored in instruction memory starting at address 0, and that each address in the memory holds a single 16-bit instruction. Assume that each CPU register has a starting value of 0xFF and is 8 bits wide. Assume that variable "a" is mapped to the R0 register in the program below and that R1 is used as a temporary register for calculations.

I tried many ways but in all ways i used more than 3 instructions.

extra: datapath

1

There are 1 best solutions below

2
Erik Eidt On

Let's do some analysis of what we can see.


Let's analyze the ADI instruction.  If, say, a 5 bit unsigned immediate is allowed we could simply add 26 to the pre-existing -1 to get the 25 in one instruction.

So, our question is what are the possible immediate values allowed?  To answer this, we can look at the definition in RTL of ADI, which is R[RD] = R[RS] + zf I(2:0).  This says that there is an immediate field encoded in the low three bits of the instruction.  The RTL also suggests that the 3-bit immediate field is zero filled or zero extended, suggesting that field encoded values 0 to 7 mean operand values of the same.  (If it hinted toward sign extension of the 3-bit immediate, that would imply a range of -4 to 3.)


In one diagram there's a shifter near the ALU.  It takes an H input of 2 bits, which I take as an encoding for the shift amount.  (This is almost certainly encoded in the low 2 bits of the instruction for shifting, similar to how the immediate is encoded in the ADI' instruction.)

(Given such a small field, 2 bits, it is silly to waste an encoding with a meaning of shift by 0 bits.)

For the 2-bit field, we have the possible encodings and their meaning that we're guessing at.

Instruction Encoded Value Possible meaning (A) Possible meaning (B)
00 shift left by 1 shift left by 1
01 shift left by 2 shift left by 2
10 shift left by 3 shift right by 1
11 shift left by 4 shift right by 2

Either way, with an educated guess, it looks like it can shift left by either 1 or 2 bits.  (In fact shifting left by 1 bit is potentially a silly encoding choice since that can be accomplished with addition to self.)


So, armed with that analysis of the potential instructions, can you try some more alternatives?