0% found this document useful (0 votes)
87 views3 pages

Branch Instruction Uses PC-relative Addressing - Target Address Is Offset From

- Branch instructions use PC-relative addressing, where the target address is calculated as an offset from the incremented PC. - The offset is specified as an immediate operand and can be positive (forward branch) or negative (backward branch) depending on whether the target is before or after the branch in the instruction sequence. - Examples show how the offset is calculated for both forward and backward branches relative to the incremented PC.

Uploaded by

syedarida fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views3 pages

Branch Instruction Uses PC-relative Addressing - Target Address Is Offset From

- Branch instructions use PC-relative addressing, where the target address is calculated as an offset from the incremented PC. - The offset is specified as an immediate operand and can be positive (forward branch) or negative (backward branch) depending on whether the target is before or after the branch in the instruction sequence. - Examples show how the offset is calculated for both forward and backward branches relative to the incremented PC.

Uploaded by

syedarida fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PC-Relative Branching

•  Branch instruction uses PC-relative addressing


•  Target address is offset from incremented PC w this
h o
–  offset = immediate operand = words - 1 e in a bit la tion
ll s e al c u
We’ he PC c
–  backward: negative (2’s complement)
affect
s t
r e s s able
–  forward: positive b y te add
on a
•  Examples: a c h ine
m
backward branch forward branch
loc: add ... when branch is executed, loc is
offset = -3 and ... distance = -2 (3 - 1) = 2 instructions away
beq r1,r2,loc (relative to incremented PC)
PC lw ...

bne r1,r2,loc
when branch is executed, loc is PC sw ...
distance = 3
(-2 - 1) = -3 instructions offset = 2 or ...
away (relative to incremented PC) loc: and ...

CPE 412 Morris Slide 1


MIPS and Branch Target Address

•  Offset is specified in words, MIPS is byte addressable


•  Answers earlier question on why immediate is multiplied by 4

•  Offset in MIPS is relative to incremented PC


loop: addi $s3,$s3,1 ..0048 22730001
sll $t1,$s3,2 ..004c 00134880
add $t1,$t1,$s6 ..0050 01364820
offset = -5 words
lw $t0,0($t1) ..0054 8d280000
beq $t0,$s5,loop ..0058 1115fffb
PC <next instruction> ..005c 34020004
MIPS offset = -5 words
•  At runtime
–  0xfffb << 2 = 1111 1111 1111 1011 << 2 = 1111 1111 1110 1100 = 0xffec
–  BTA = 0x005c + 0xffec = 0x0048
–  alternatively, -5 * 4 = -20, 0x5c = 9210, so 92 – 20 = 7210 = 0x48

CPE 412 Morris Slide 2


QtSpim Simulator and Branches

•  Offset in MIPS is relative to incremented PC


–  note example in text on page 115 and QRC
•  Offset in QtSpim is relative to unincremented PC
–  QtSpim is wrong, Hennessey and Patterson are right!
–  dis-cuss-ion at groups.google.com/forum/#!topic/ucsb-ece-15b/0DxItdNUwGI
loop: addi $s3,$s3,1 ..0048 22730001
sll $t1,$s3,2 ..004c 00134880
add $t1,$t1,$s6 ..0050 01364820
offset = -5 words
lw $t0,0($t1) ..0054 8d280000
beq $t0,$s5,loop ..0058 1115fffc
PC <next instruction> ..005c 34020004
QtSpim offset = -4 words
•  QtSpim is a simulator and codes seem to execute properly
–  but the machine code for branches is wrong
•  When you hand-assemble code do it the right way
•  When you hand-disassemble code do it the right way
•  Back to page 117…

CPE 412 Morris Slide 3

You might also like