Search code examples
assemblyemu8086

How to access an element of a 2D array in assembly?


How to access an element of a 2D array in assembly? I found a few code examples, but they are using edx and eax registers, which are not supported by emu8086 (16-bit registers only).
I want to make variable t equal to the value of array[0][1] (t = 2).
What should I do?

.8086
.model small
.stack 100h

.data 

    t dw ?

    array   dw 1, 2, 3,
            dw 4, 5, 6,
            dw 7, 8, 9

.code 
    ; ax <- array[0][1]
    mov eax, DWORD PTR[array + 8] ; not working
    ; t <- ax
    mov t, eax ; t = 2

Solution

  • I want to make variable t equal to the value of array[0][1] (t = 2).

    The array is filled with words. The 2nd element on the 1st row is at offset 2 in the array.

    mov ax, [array + 2]
    mov [t], ax
    

    Depending on the assembler the last line could read:

    mov t, ax
    

    Since you're working with .model small, did you setup the DS segment register?

    If you change to .model tiny, you wouldn't need to setup any segment registers.