Shop OBEX P1 Docs P2 Docs Learn Events
Assembly Language Array Addressing — Parallax Forums

Assembly Language Array Addressing

Randy HarrisRandy Harris Posts: 11
edited 2005-09-25 23:33 in General Discussion
I have just started using SX in assembly language.· I am familiar with several older microprocessor assembly languages but not the SX.· My current problem is:

I have not figured out how to·store date into·an Array in assembly language.
I tried different things like using

mov· location+offset,data

and I tried setting FSR using BANK command and then setting the lower 4 bits of FSR as an offset and then

mov ind,data

None of this seems to work.
Any help would be appreciated.
Thanks,
Randy
·

Comments

  • Guenther DaubachGuenther Daubach Posts: 1,321
    edited 2005-09-25 08:37
    Randy,

    the trick is "indirect addressing" here: First load the FSR register with the address of the desired array location, then use indirect addressing to save that data, and finally, restore FSR to point to the register bank required next like in:

    mov w, #Array    ; Array base address
    add w, Index     ; Index holds the current array index (must either be located in the bank currently selected by FSR or in the global register bank 0
    mov fsr, w       ; "Point" FSR to array item
    mov w, Data      ; Data must be located in the global register bank 0
    mov ind, w       ; Save data in the array item
    bank Whatever    ; Restore FSR
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Greetings from Germany,

    G
  • Randy HarrisRandy Harris Posts: 11
    edited 2005-09-25 20:09
    Thanks,

    I was coming to a very similar conclusion that that must be the way to do it.

    Does it matter if you do the arithmatic in w or in fsr (adding the offsets)?

    What is the default bank?· Could you save fsr in a temp storage and then replace it in fsr when you were finished with the array manipulations?

    Randy
  • Guenther DaubachGuenther Daubach Posts: 1,321
    edited 2005-09-25 23:33
    Randy,

    You can also use instructions like

    mov fsr, #Array
    add fsr, Index

    to do the arithmetic in FSR but you should keep in mind that these both are no "native" SX instructions. The SASM Assembler accepts such "compound" or "multi-word" instructions but it will convert them into instruction codes equivalent to the following sequence of "native", or "single-word" instructions:

    mov w, #Array
    mov fsr, w
    mov w, Index
    add fsr, w

    As you can see, this resulting code sequence comes close to what I had suggested, and it also expands to four instruction words.

    Don't rely on a "default bank" - you better use a BANK instruction for setting FSR to the bank containing the variables you want to access next. Keep in mind that the "global" variables located at $08...$0f (on SX28 and "smaller" devices) can always be accessed, no matter what value FSR currently holds.

    You can nicely save FSR in a temp storage, and restore it's original value when finished with the array manipulation, or any other indirect addressing operation that needs to change the contants of FSR. As FSR may hold any arbitrary value when its contents is to be saved or restored, it is important to place this temp storage in the "global" variable area as this can be accessed indepentently of the current value in FSR. Using a temp storage for FSR makes a lot sense when you use subroutines that manipulate the contents of FSR. This way, a subroutine can save FSR to a FSRSave variable (in the global variable area), change FSR as needed, and restore FSR from FSRSave just before returning.

    It is important to note that this will only work as long as you don't nest subroutine calls where "lower-level" subroutines also make use of FSRSave. In my book, I have included an example how to implement a stack for saving and restoring FSR together with nested subroutines.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Greetings from Germany,

    G
Sign In or Register to comment.