edited by
14,426 views
54 votes
54 votes
Consider a RISC machine where each instruction is exactly $4$ bytes long. Conditional and unconditional branch instructions use PC-relative addressing mode with Offset specified in bytes to the target location of the branch instruction. Further the Offset is always with respect to the address of the next instruction in the program sequence. Consider the following instruction sequence$$\begin{array}{ll} \text{Instr. No.} & \text{Instruction} \\\hline  \text{i:} & \text{add R2, R3, R4} \\   \text{i+1:} & \text{sub R5, R6, R7} \\  \text{i+2:} & \text{cmp R1, R9, R10}  \\ \text{i+3:} & \text{beq R1, Offset}  \\ \end{array}$$If the target of the branch instruction is $i,$ then the decimal value of the Offset is ____________ .
edited by

4 Answers

Best answer
62 votes
62 votes

Answer is $-\bf{16.}$

Program Counter is updated with the address of next instruction even before the current instruction is executed.

That is why the question says that the address of the next instruction is updated with next instruction in sequence.

Before executing instruction $\bf{i + 3}$, the current state looks as under:

Please note: BEQ instruction is for Branch Equal

Question says that the target of branch instruction is 'i' which is at $2000$ in our example.

So, we need to go to address $\bf{2000}$ from address $2016$ (which is currently pointed by PC)

$\bf{2016 - 2000 = 16}$

So, we have to specify Offset as $-16$ which would mean that $16$ should be subtracted from next address instruction ($2016$).

edited by
19 votes
19 votes
Ans:(-16)

assume addresses start with $2000$ for the first instruction.

$2000---\text{add }R2,R3,R4$

$2004---\text{sub }r5,r6,r7$

$2008---r1,r9,r10$

$2012---\text{beq } r1,$offset  //pc after instruction fetch of this instruction will be $2016$,
and branch target is $2000,$offset will be $(2016-16)=2000$

$2016$------next instruction
edited by
4 votes
4 votes

Size of each instruction = 4 Bytes
Program counter Relative Addressing Mode
⇾ Assuming the first instruction starts at address zero

Offset should go from Address 16 to Address 0
⇒ Offset = 0 – 16 = (-16) ⇾ Final answer

Answer:

Related questions