开发者

Assembly language 8086

开发者 https://www.devze.com 2022-12-27 19:35 出处:网络
I\'ve a problem with Assembly 8086. I don\'t know how to use an 2D array. 开发者_如何学PythonWhen I m using like this

I've a problem with Assembly 8086. I don't know how to use an 2D array. 开发者_如何学PythonWhen I m using like this mov ar[cx][dx] I get an error, and when I want to us SIand DI in an array it also returns an error.


I would be very impressed in a CPU that provided array lookup semantics in its assembly language. Or rather, I would be annoyed if it meant something more important had been sacrificed.

The general way to do array lookup in assembly is by doing the calculation yourself to turn the two indexes for a 2D array into a single index for a 1D array, and adjust for the element size. For example (pseudo-code):

ax = cx * major_dimension
ax = ax + dx
ax = ax * element_size
ax = peek[base+ax]

where major_dimension is one of the dimensions of the 2D array (which dimension you use depends entirely on how the data is laid out in memory), element_size is the size of each element, base is the start of the array and cx/dx are the indexes you're using to access the array.

For example, if you have a 3-by-4 (a[0-2][0-3]) array at memory location 0x0700 and these are 32-bit integers:

        +--------+--------+--------+--------+
0x0700: | a[0,0] | a[0,1] | a[0,2] | a[0,3] |
        +--------+--------+--------+--------+
0x0710: | a[1,0] | a[1,1] | a[1,2] | a[1,3] |
        +--------+--------+--------+--------+
0x0720: | a[2,0] | a[2,1] | a[2,2] | a[2,3] |
        +--------+--------+--------+--------+

To find array element a[n,m], you calculate the major index multiplied by four plus the minor index, scale it to the correct element size (4 bytes) then add the base. To find element a[2,1]

addr = base   + (n * 4 + m) * 4
     = 0x0700 + (2 * 4 + 1) * 4
     = 0x0700 + (8     + 1) * 4
     = 0x0700 + (9        ) * 4
     = 0x0700 + 36
     = 0x0700 + 0x24
     = 0x0724

Then that's the address you use for looking up the 1D array.


And, based on the comment that:

ar   db   3dup(3dup(0))
     mov  ar[bx][si],al

would work, that's not quite right (ar[bx][si] is masm-specific syntax equivalent to ar[bx+si]).

All that does is a simple addition of the ar address with the bx and si registers. It does not scale the bx or si register to take into account the major dimension and it does not scale the bx+si value for the element size. So it will only work as-is for a 2D array of bytes where the major dimension is 1, which I'm pretty sure would make it a 1D array :-)

To work for any case, you would first need to multiply bx or si (depending on which is being used for the major dimension) by the major dimension then both bx and si by the element size.


I'm not really clear on the exact question you're asking but are you after something like this (using base/indexed addressing mode)?

lea bx, array_base
mov si, array_index
mov ax, [bx][si]


This could help you.

2D arrays are stored in a memory just like 1D array but represent it as having rows and columns.

Below is the code to declare the 2D array which is stored in memory linearly.

Like this:

                +----------+----------+----------+----------+----------+----------+----------+
          index | arr[0][0]| arr[0][1]| arr[0][2]| arr[0][3]| arr[0][4]| arr[1][0]| arr[1][1]| 
                +----------+----------+----------+----------+----------+----------+----------+
         value  |    0     |    1     |    2     |    3     |    4     |    10    |    11    |
                +----------+----------+----------+----------+----------+----------+----------+
 memory address | 1000     | 1004     | 1008     | 1012     | 1016     | 1020     | 1024     |
                +----------+----------+----------+----------+----------+----------+----------+

Here each row have 4 entries and 5 columns.

Now if i want to locate value 11 ,arr[1][1] row 1 col 1 ,then the memory address will be 1024. To locate this entry first I have to calculate row index then column index.

Calculating row index:

So each row have 5 entries each of 4 bytes therefore each row will be of 4*5=20 bytes. Entry 11 is in row index 1 so 1*20 = 20 which will be at row 1.

Calculating column index:

After that entry 11 is in the column index 1 so 1*4= 4 which be at column index 1. We multiply it by 4 because we have declared it as DWORD which is of 4 bytes.

At last just add the results (row index + col index) 20+4 = 24. When we add this to the memory address of first entry we will be jumped to entry 11

i.e: 1000+24 = 1024

.data

arr DWORD 0, 1, 2, 3, 4 ; This is declaration of 2D array having 4 rows, 5 cols each entry takes 4 bytes because its a DWORD

      DWORD 10,11,12,13,14

      DWORD 20,21,22,23,24

      DWORD 30,31,32,33,34

ROWSIZE EQU SIZEOF arr ; its a named constant which means each row is having 20 bytes. In each row there are 5 cols means 5 entries and each entry is of 4 bytes so 4*5 = 20.

.code

mov ebx, 2*ROWSIZE ; row index = 2

mov esi, 3 ; col index = 3

mov eax, arr[ebx+esi*4] ; EAX = arr[2][3], Here when we call arr[0] it will be entry 0 so entry 11 will be arr[24]. It means skip 24 bytes from the starting memory address of the array.

0

精彩评论

暂无评论...
验证码 换一张
取 消