For the CPU, there is no difference between JMP and JSR,
it's only a software thing.
Each time a value is written into PC, the old PC contents
are saved into LR, thand e software has to take care of LR
from there on.
Now an example:
If Bit 31 of a 32 Bit instruction word is zero, said 32 Bit word
is written into PC.
Let's suppose, that our code is in the "lower half" of the four
Giga_longword Address range of the CPU, and that our assembler
places the address of a label (or subroutine) straight into the
machine code of our program as a constant, if a label (or subroutine)
doesn't stand on the left side of the screen:
LOOP
FOO ;//call first subroutine
BAR ;//call second subroutine
LOOP ;//then start again.
;
FOO
SP--, [SP]=LR, nop; //save LR
;
;//do some stuff
;
PC=[SP], SP++, nop; //RETURN
;
BAR
SP--, [SP]=LR, nop; //save LR
;
;//do some stuff
;
PC=[SP], SP++, nop; //RETURN
;
Pretty neat, isn't it ?
This way, the address of a label (or a subbroutine) actually becomes
an instruction, pretty much as in a Forth environment.
Looks like we are getting a nice/efficient support for massive use
of subroutines.
Of course, immediate addressing also causes a JMP/JSR:
PC=[PC++], nop, nop; .word ADDRESS;
But as you can see, it's less efficient than the JMP/JSR in our first
example, which did not resort to instruction triples.
Now an example for a relative branch/call:
R6=[PC++], PC+=R6, nop; .word OFFSET;
So far, so good.
But for writing some software, we also need a conditional jump/branch.
So we just make use of the SKIP instruction:
R6=[PC++], SKIP C, PC=R6; //JNC, jump if C=0. .word ADDRESS;
The example above loads a literal value into R6, stops execution of
instruction triples in this 32 Bit word, if the C Flag is set, skipping
the instruction in the last triple which writes R6 into PC.
So basically, the example is a "jump if C=0" instruction.
BNC, branch if C=0:
R6=[PC++], SKIP C, PC+=R6; //BNC, branch if C=0. .word OFFSET;
Last example: a conditional return from a subroutine:
SKIP C, PC=[SP], SP++; //RETNC, return if C=0.
[HOME] [UP]/ [BACK] [1] [2] [3] [4] [5] [6] [7] [8] [NEXT]
(c) Dieter Mueller 2007, 2008