MIPS: Calculating Displacement -
working through problems , confused calculating displacement.
some examples are:
top: addi $s2, $s2, -1 addi $s1, $s1, 1 bne $s2, $0, top
assume top has value of 0x1000 0008.
top: bne $s1, $s2, end addi $s1, $s1, 1 end: j top
assume top has value of 0x1000 0008.
what displacement in bne instructions?
can explain how calculate these? thanks.
the absolute address doesn't matter. matters distance between jump , target.
to calculate offset, calculate distance between jump target , instruction following bne
. in first example distance 12 bytes because there 3 instructions between target label , instruction following bne
, , each instruction 4 bytes in size. , since backwards jump has negative offset, i.e. -12.
since instructions need word aligned (4 bytes), offset stored in instruction word shifted 2 bits right (since 2 least significant bits 00
anyway). arithmetic shift, meaning sign bit preserved. need take -12 , arithmetic right shift 2 bits actual offset gets stored in instruction word. gets easier if view -12 in hexadecimal form, 0xfff4. shifting 0xfff4 gives 0xfffd, we'd put in instruction word.
in second example offset positive, apart way calculate same.
Comments
Post a Comment