beautypg.com

5 looping operations, 19 .7 .5 looping operations -13, 5loopingoperations – Maxim Integrated MAX31782 User Manual

Page 178

background image

MaximIntegrated 19-13

MAX31782 User’s Guide

Revision 0; 8/11

19.7.5LoopingOperations

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’ instruc-
tion 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 sup-
plied 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 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

If opting to preload the loop address to an internal 16-bit register, the most time and code efficient means is by perform-
ing the load in the instruction just prior to the top of the loop:

move

LC[1], #10h

; Set loop counter to 16

move

LC[0], IP

; Set loop address to the next address

LoopTop:

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

...