Question

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:

Instr. No.Instructioniadd R2, R3, R4i+1sub R5, R6, R7i+2cmp R1, R9, R10i+3beq R1, Offset\begin{array}{ll} \text{Instr. No.} & \text{Instruction} \\ i & \text{add R2, R3, R4} \\ i+1 & \text{sub R5, R6, R7} \\ i+2 & \text{cmp R1, R9, R10} \\ i+3 & \text{beq R1, Offset} \end{array}

If the target of the branch instruction is ii, then the decimal value of the Offset is ______.

Official answer and solution
Official answer
-16
From
Official GATE 2017 answer key

Solution

Calculate the PC-relative byte offset from the next instruction address to the target branch address.

Each instruction is 4 bytes long. Let AA be the memory address of instruction ii.

The addresses of the instructions in sequence are:

  • Address of instruction ii: AA
  • Address of instruction i+1i+1: A+4A + 4
  • Address of instruction i+2i+2: A+8A + 8
  • Address of instruction i+3i+3 (beq R1, Offset): A+12A + 12

The next instruction in sequence is at address (A+12)+4=A+16(A + 12) + 4 = A + 16.

The PC-relative offset is defined with respect to the address of the next instruction:

Target Address=Address(Next Instruction)+Offset\text{Target Address} = \text{Address}(\text{Next Instruction}) + \text{Offset}

Here, the target instruction is instruction ii at address AA. Therefore:

A=(A+16)+Offset  ⟹  Offset=A−(A+16)=−16A = (A + 16) + \text{Offset} \implies \text{Offset} = A - (A + 16) = -16
Final answer

-16.0

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2017 Computer Science and Information Technology (CS) Session 1
Question
49 of 65, printed as CS Q.49
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.