DNJZ Address Question
Okay, here is a simple question for your guys
DNJZ fr, addr
If addr = $ then the command jumps back to itself right?
Now if addr = $-2 where does this jump to? 2 lines prior or two words prior the the command?
Pete
DNJZ fr, addr
If addr = $ then the command jumps back to itself right?
Now if addr = $-2 where does this jump to? 2 lines prior or two words prior the the command?
Pete

Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
Consider the problem in mathematical terms. $ is the program counter value (physical memory address) for the current instruction. therefore the jump is to is this address - 2. In the case of the SX chip thats two words. Thats how assemblers usually work it out. To jump to a specific line of the source code then just put a label on the required line and DJNZ fr,label
Brian
Some of the "tricks" are just not spelled out in the documentation.
Pete
DJNZ fr, addr is a so-called "compund" statement, i.e. the SX does not "understand" it directy. Instead, the Assembler generates two subsequent instructions as replacement:
DECSZ fr
JMP addr
In case you specify $ for addr, the Assembler is clever enough to replace $ with the address of the DECSZ instruction, and not with the address of the JMP instruction which would not make too much sense.
The same is true for an argument like $-2, IOW, the Assembler always replaces $ with the address of the first instruction of a compount statement.
BTW, a good method to test the behavior of certain instructions is writing a simple program, and then single-stepping it with the debugger to see what happens.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Greetings from Germany,
G
In general, unless you REALLY know what you are doing, don't use $. Put in a lable instead.
3 =00000010 org $10 4 0010 0000 nop 5 0011 0000 nop 6 0012 02E9 djnz 9, $-2 0013 [color=green]0A10[/color] 7 8 =00000020 org $20 9 0020 0000 nop 10 0021 0000 nop 11 0022 02E9 decsz 9 12 0023 [color=red]0A21[/color] jmp $-2 13 14 untl macro 2 15 decsz \1 16 jmp \2 17 endm 18 [color=blue]At this point the output differs:[/color] [color=#0000ff] SASM evaluates the $-2 in the macro The SXKey asm evaluates it before[/color] 19 =00000030 org $30 =00000030 org $30 20 0030 0000 nop 0030 0000 nop 21 0031 0000 nop 0031 0000 nop 22 untl 9, $-2 untl 9, $-2 23 0032 02E9 m decsz 9 0032 02E9 m decsz 9 24 0033 [color=red]0A31[/color] m jmp $-2 0033 [color=green]0A30[/color] m jmp $-2 26 27 untl2 macro 2 28 djnz \1,\2 29 endm 30 31 =00000040 org $40 32 0040 0000 nop 33 0041 0000 nop 34 untl2 9, $-2 35 0042 02E9 m djnz 9,$-2 0043 [color=green]0A40[/color]▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
---
James Newton, Host of SXList.com
james@sxlist.com 1-619-652-0593 fax:1-208-279-8767
SX FAQ / Code / Tutorials / Documentation:
http://www.sxlist.com Pick faster!