Reputation: 33
I have a question for solving the value of K in the opcode for rjmp. The opcode is 1100 kkkk kkkk kkkk. Where Pc<- PC +k + 1. When I solve for this I get k = -1 and then i know the 12 bits for K in the opcode is 0xFFF. I then assumed my answer is CFFF, but it is is incorrect. The answer is CFFD. What am I doing incorrectly? Heres the code I was given.
foo: NOP
NOP
RJMP foo
Upvotes: 1
Views: 2283
Reputation: 58792
You want to jump back 1 word, and the formula gives k + 1 = -1
so k = -2
which is0xFFE
on 12 bits. Plugging in the opcode gives 0xCFFE
. You can check by using an assembler:
00000000 <foo>:
0: 00 00 nop
2: fe cf rjmp .-4 ; 0x0 <foo>
Other way to look at it is that the offset is measured from the next instruction, because the cpu already incremented PC when the offset is added. That's where the +1 comes from.
Upvotes: 2