Shop OBEX P1 Docs P2 Docs Learn Events
ORG help for a newbie! — Parallax Forums

ORG help for a newbie!

MCassidyMCassidy Posts: 7
edited 2005-02-06 19:00 in General Discussion
Hello all,
I'm trying to get the hang of this ASM programming stuff (on SX 28), but I'm having some trouble with a few key concepts, especially memory mapping.· I understand that the lower half of each bank is always the same set of registers ($00-$0F), and that $03, $05, etc. represent different banks of data registers (8 total)·and that there are 4 program data pages, but I get confused with using the ORG instruction.·

Can someone point me towards some documentation that clearly explains it?

Thanks.

-MichaelC.

Comments

  • BeanBean Posts: 8,129
    edited 2004-08-27 18:09
    ORG just sets the address of the next instruction. It has NOTHING to do with the RAM banks.

    Bean.
  • Guenther DaubachGuenther Daubach Posts: 1,321
    edited 2004-08-27 18:59
    Michael,

    here is a code snippet copied from one of my programs:

    ; Global variables
    ;
    org 8
    Global_ByteCount ds 1
    Global_Index ds 1
    ; more (max. 8 variables)

    org $30 ; Serial variable area
    Serial = $
    Serial_TxHighPC ds 1
    Serial_TxLowPC ds 1
    ; more (max. 16 variables)...

    org $50 ; FIFO buffer
    Buffer ds 16 ; 16-byte array, indirectly addressed

    org $70 ; Miscellaneous stuff
    Misc = $
    Misc_FIFOHead ds 1
    Misc_FIFOTail ds 1
    ; more (max. 16 variables)...

    org $90
    Free1 = $

    org $b0
    Free2 = $

    org $d0
    Free3 = $

    org $f0
    Free4 = $

    Here, the org directives tell the assembler where (i.e. in which bank) it shall set aside memory for the variables. I·always also define a symbolic name for each bank, e.g. Serial = $. Later, when you want to access a variable, be careful that the most recently executed bank instruction has selected the bank, where the variable is located. If necessary, insert an instruction like "bank Serial" before accessing TXHighPC in my sample code.

    In the meantime, for variables, I strictly use the naming convention <Bank>_<VarName>, i.e. the first part of each name consists of the bank name. This makes the code easier to read because you always know, where a variable is located, and helps to check if the right bank is selected.

    BTW: The ORG directive is also used to specify the starting points of program code, like in

    org 0

    ; The interrupt service routine must always start here (if any)
    ;
    ISR
    ; some code
    mov w, #IntPeriod
    retiw

    org $100

    ; The Main program
    ;
    Main
    ; instructions following


    The assembler is clever enough to find out when an ORG is used to specify a location in data memory, or in program memory.

    Each memory bank has a size of 32 bytes where the lower half, i.e. the first 16 bytes are always mapped to bank 0, no matter which bank is actually selected. Within the first 16 lower bytes of bank 0, only the higher eight locations are free to be used as variable space because the lower eight bytes are used as special registers, e.g. the PC, the FSR, the ports, etc.

    Therefore, there is the ORG 8 in the above example. As these upper eight bytes in bank 0 can always be accessed, no matter which bank is actually selected, they are usualy called "Global Variables".

    More confused than before ?

    Greetings,

    Günther

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    Post Edited (Guenther Daubach) : 8/27/2004 7:03:17 PM GMT
  • CatwareCatware Posts: 14
    edited 2005-02-06 19:00
    Thanks a lot Guenther; your explanation was really useful to me! It also helped simplify my code a lot.·smile.gif·
Sign In or Register to comment.