7 changing program flow (ljmp, sjmp, ajmp) – Texas Instruments MSC1210 User Manual
Page 200
Changing Program Flow (LJMP, SJMP, AJMP)
16-6
16.7 Changing Program Flow (LJMP, SJMP, AJMP)
LJMP, SJMP and AJMP are used as a go to in assembly language. They cause
program execution to continue at the address or label they specify. For exam-
ple:
LJMP
LABEL3
;Program execution is transferred to LABEL3
LJMP 2400h
;Program
execution
is
transferred
to
address
2400h
SJMP LABEL4
;Program execution is transferred to LABEL4
AJMP LABEL7
;Program execution is transferred to LABEL7
The differences between LJMP, SJMP, and AJMP are:
-
LJMP requires 3 bytes of program memory and can jump to any address
in the program.
-
SJMP requires 2 bytes of program memory, but can only jump to an ad-
dress within 128 bytes of itself.
-
AJMP requires 2 bytes of program memory, but can only jump to an ad-
dress in the same 2k block of memory.
These instructions perform the same task, but differ in what addresses they
can jump to, and how many bytes of program memory they require.
LJMP always works. You can always use LJMP to jump to any address in your
program.
SJMP requires two bytes of memory, but has the restriction that it can only
jump to an instruction or label within 128 bytes before or 127 bytes after the
instruction. This is useful if you are branching to an address that is very close
to the jump itself. You save 1 byte of memory by using SJMP instead of AJMP.
AJMP also requires two bytes of memory, but has the restriction that it can only
jump to an instruction or label that is in the same 2k block of program memory.
For example, if the AJMP instruction is at address 0200
H
, it can only jump to
addresses between 0000
H
and 07FF
H—
It can not jump to 800
H
.
Note:
Some optimizing assemblers allow you to use JMP in your code. While there
is no JMP instruction in the 8052 instruction set, the optimizing assembler
will automatically replace your JMP with the most memory-efficient instruc-
tion. That is, it will try to use SJMP or AJMP if it is possible, but will resort to
LJMP if necessary. This allows you to simply use the JMP instruction and let
the assembler worry about saving program memory, whenever possible.