Search code examples
arraysassemblyelementx86-16emu8086

Accessing an array in emu8086


Can someone please tell me the most effective way to access an array's element in emu8086?
As an example I got this array:

tab db 30h,35h,32h,37h,38h,39h,31h

Is it wrong to write mov si, offset tab and then access to the element using si? Like tab[si], tab[si+1], tab[si+n], etc.
Am I obliged to keep using lodsb/lodsw?


Solution

  • Is it wrong to write mov si, offset tab and then access to the element using si? like tab[si], tab[si+1], tab[si+n]... etcetera.

    That would be very wrong indeed! Assuming emu8086 can accept tab[si] where (part of) the displacement component lies outside of the square brackets, you would erroneously be adding the offset to the array twice. Once in the initialization of SI (mov si, offset tab), and once more in the load/store instruction that uses the addressing mode tab[si].

    Four ways to solve it:

    • Setup SI with mov si, offset tab and access the first element with [si] and the second element with [si+1].

    • Setup SI with xor si, si (same as mov si, 0) and access the first element with tab[si] and the second element with tab[si+1].

    • Setup SI with mov si, offset tab and access all of the elements with [si] because in between accesses you will have incremented the SI register as part of a loop's logic.

    • Setup SI with xor si, si (same as mov si, 0) and access all of the elements with tab[si] because in between accesses you will have incremented the SI register as part of a loop's logic.

    Am I obliged to keep always using lodsb/lodsw?

    Definitely no. But should you ever need to write the most compact solution, then often the use of these string primitives will do the job.