Assembly Jump Question (PC Offsets) – MSP430

assemblymsp430

So im still trying to understand basic computer architecture, and have been writing a little bit of assembly for the MSP430 (basic LED/Switches stuff).

I've been browsing the instruction set, and things are starting to make more sense.

However for example a JNE/JNZ (Jump if Not Equal/Jump is Not Zero). I understand these are checking the status bits (that were maybe set from a CMP function).

However the operation looks like this(Grabbed from the instruction set):

if Z = 0: PC + 2*offset -> PC
if Z = 1: execute following instruction

So if Z is 0, go to the next instruction…makes sense. But I don't understand the PC + 2*offset -> PC, I think im maybe confused on the PC itself. The user guide doesn't go into a HUGE detail about it. The PC presumably points to the NEXT instruction to be executed?

When it says it points to im guessing it's holding whatever is next? (Like maybe it's holding a MOV #020h,R9 or something? which would convert to some binary opcode + source and destination binary bits (thats a total of 16 bits? (I could be wrong here)

However I do not understand the PC + 2*offset part, offset of what? I think that's the part that gets me. But I think the whole "process" of whats happening is still confusing, I understand WHAT is being written and where but maybe not "how". (im getting there though!)

Edit: I think perhaps Im forgetting also that something like MOV @020h,R9 is actually 3 separate 16 bit words (I Think…..which would be 3 different PC counts) (since the registers are 16 bits large).

Edit2: Here is an Example from the MSP430 book: (Keep in mind I do understand "What" it's doing, but not why? and I don't understand the offset part

DelayLoop: ; 
inc.w R4 ; Increment loop counter 
cmp.w #DELAYLOOPS ,R4 ; Compare with maximum value 
jne DelayLoop ; Repeat loop if not equal 

Best Answer

In the assembler source, the JNE instruction (called BNE - branch if not equal in some instruction sets) will include the destination of the Jump, usually as a lable.

If the comparison before the JNE was equal, the instruction immediately following the JNE will be executed. If it was Not Equal, the distance (offet) to the destination lable will be added to the Program Counter, so the code immediately following the JNE instruction will be skipped, and program execution will continue from the destination address of the instruction.

Edit: In the code sample in the OP's Edit 2, DelayLoop is the destination of the jump in the JNE instruction, so the offset is the difference between the address of the DelayLoop lable, and the address following the JNE Delayloop instruction - in this case, the offset will be subtracted from the PC, rather than being added to it, since we want to jump back, and execute that loop several times, until the result of cmp.w #DELAYLOOPS ,R4 is "equal". When the result of the comparison is equal, program execution will continue with the instruction following the JNE.

I'm not familiar with the MPS430 architecture, so can't explain the "2*Offset" calculation, but it must have something to do with how memory is addressed.