You are on page 1of 3

MIT COLLEGE OF ENGINEERING, PUNE

Calculating Short Jump Offset


For 8051
Prof. Rajendra Khope

2011

WWW.FIGMENTSOL.COM

8051 Calculating Short jump Offset SJMP reladdr Short Jump The SJMP instruction transfers execution to the specified address. The address is calculated by adding the signed relative offset in the second byte of the instruction to the address of the following instruction. The range of destination addresses is from 128 before the next instruction to 127 bytes after the next instruction (-128 or +127) SJMP jumps unconditionally to the address specified reladdr. Reladdr must be within -128 (For backward) or +127 (for forward) bytes of the instruction that follows the SJMP instruction
Operation
SJMP PC = PC + 2 PC = PC + offset SJMP LABEL

Example

In case of forward Jump we add offset till label, which can be calculated by Offset = LABEL Location - Current PC. Remember PC is always pointing at Next instruction. So if SJUMP is at location 2000H, Current PC value should be assumed at 2000+2 as SJUMP is two byte instruction and then calculate offset location of Label. (in case of JNB, PC will be +3 as it is 3 byte) If Jump is backward then offset can be calculated as being ve offset. So calculate the location from current PC to Label and then represent the number in its ve sign form (or 2s complement form): Offset = LABEL Location - Current PC. -Offset (or can be represented as a 2s complement of offset magnitude)

Why 2s complement: The two's complement of a binary number is defined as the value obtained by subtracting the number from a large power of two (specifically, from 2N for an N-bit two's complement). The two's complement of the number then behaves like the negative of the original number in most arithmetic, and it can coexist with positive numbers in a natural way. Two's Complement is referred to as Binary Number Representation (or BNR) in protocols used in Aviation (ARINC_429). A two's-complement system, or two's-complement arithmetic, is a system in which negative numbers are represented by the two's complement of the absolute value;[1] this system is the most common method of representing signed integers on computers.

Prof. Rajendra Khope MIT College of Engineering, Pune

8051 Calculating Short jump Offset So in our case:

If its a jump from SJMP MAIN, ie 9014 This is a backward jump Current PC will be +2, i.e. 9014+2=9016 (As PC always point to next instruction to be executed) So Offset = LABEL Location - Current PC. -Offset (or can be represented as a 2s complement of offset magnitude) Offset = 9000-9016 = -16 In binary -16 => 1110 1010 which is EA in HEX Or Take 2s complement of Magnitude 16=> In binary 16= 0001 0110 -- -- 2s complement will be 1110 1010 which is EA in HEX IF instruction is JNB sfr,label For Ex:

Here 98 is address of RI, hence JNB is 3 byte instruction. Here current PC will be at 900E and jump label is at 900B Hence offset-> 900B-900E=FD (or 2s complement of 3)

Prof. Rajendra Khope MIT College of Engineering, Pune

You might also like