beautypg.com

3 – conditional jumps, 4 – calling subroutines, 5 – looping operations – Maxim Integrated DS4830A Optical Microcontroller User Manual

Page 198: Ds4830a user’s guide

background image

DS4830A User’s Guide

198


23.7.3 – Conditional Jumps
Conditional jumps transfer program execution based on the value of one of the status flags (C, E, Z, S). Except
where noted for JUMP E and JUMP NE, the absolute and relative operands allowed are the same as for the
unconditional JUMP command.

jump c, Label1

; jump to Label1 if Carry is set

jump nc, LongJump

; jump to LongJump if Carry is not set

jump z, LC[0]

; jump to 16-bit register destination if Zero is set

jump nz, Label1

; jump to Label1 if Zero is not set (Acc<>0)

jump s, A[2]

; jump to A[2] if Sign flag is set

jump e, Label1

; jump to Label1 if Equal is set

jump ne, Label1

; jump to Label1 if Equal is cleared

JUMP E and JUMP NE may only use immediate destinations.

23.7.4 – Calling Subroutines
The CALL instruction works the same as the unconditional JUMP, except that the next execution address is pushed
on the stack before transferring program execution to the branch address. The RET instruction is used to return from
a normal call, and RETI is used to return from an interrupt handler routine.

call Label1

; if Label1 is relative, assembles to : call #immediate

call LongCall

; assembles to: move PFX[0], #high(LongCall)

; call #low(LongCall)

call LC[0]

; call to address in LC[0]

LongCall:

ret

; return from subroutine


23.7.5 – Looping Operations
Looping over a section of code can be performed by using the conditional jump instructions. However, there is built-
in functionality, in the form of the ‘DJNZ LC[n], src’ instruction, to support faster, more compact looping code with
separate loop counters. The 16-bit registers LC[0], and LC[1] are used to store these loop counts. The ‘DJNZ LC[n],
src’ instruction automatically decrements the associated loop counter register and jumps to the loop address
specified by src if the loop counter has not reached 0.

To initialize a loop, set the LC[n] register to the count you wish to use before entering the loop’s main body.
The desired loop address should be supplied in the src operand of the ‘DJNZ LC[n], src’ instruction. When the
supplied loop address is relative (+127/-128 words) to the DJNZ LC[n] instruction, as is typically the case, the
assembler automatically calculates the relative offset and inserts this immediate value in the object code.

move LC[1], #10h

; loop 16 times

LoopTop:

; loop addr relative to djnz LC[n],src instruction

call LoopSub

djnz LC[1], LoopTop

; decrement LC[1] and jump if nonzero

When the supplied loop address is outside of the relative jump range, the prefix register (PFX[0]) is used to supply
the high byte of the loop address as required.

move LC[1], #10h

; loop 16 times

LoopTop:

; loop addr not relative to djnz LC[n],src

call LoopSub

...
djnz LC[1], LoopTop

; decrement LC[1] and jump if nonzero

; assembles to: move PFX[0], #high(LoopTop)

; djnz LC[1], #low(LoopTop)

If loop execution speed is critical and a relative jump cannot be used, one might consider preloading an internal 16-
bit register with the src loop address for the ‘DJNZ LC[n], src loop. This ensures that the prefix register will not be
needed to supply the loop address and always yields the fastest execution of the DJNZ instruction.

move LC[0], #LoopTop

; using LC[0] as address holding register

; assembles to: move PFX[0], #high(LoopTop)

; move LC[0], #low(LoopTop)

move LC[1], #10h

; loop 16 times

...

LoopTop:

; loop address not relative to djnz LC[n],src

call LoopSub

...
djnz LC[1], LC[0]

; decrement LC[1] and jump if nonzero