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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
---
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!