Author Topic: How can i make a multidimensional array and access each element  (Read 9065 times)

hellfix

  • Guest
Im trying to make a program that can access a multidimensional array and how can i access them in masm

array byte 12 dup(?)
array byte 12,13,44
                  33,03,10
                  54,98,09

is this how you make if yes my question is how can i access them like how i would
do in c++ is that possible

Farabi

  • Member
  • ****
  • Posts: 970
  • Neuroscience Fans
Re: How can i make a multidimensional array and access each element
« Reply #1 on: June 12, 2012, 12:11:14 PM »
On MASM you only able to create a one dimensional array, I never know if there is such a thing for multidimensional array for MASM.
http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165


dedndave

  • Member
  • *****
  • Posts: 8751
  • Still using Abacus 2.0
    • DednDave
Re: How can i make a multidimensional array and access each element
« Reply #3 on: June 12, 2012, 12:54:30 PM »
you can create a linear array, then use different registers to address different row or columns
Code: [Select]
        mov     eax,[ebx+esi+8]
        mov     eax,[ebx+4*esi+8]
in those examples, EBX addresses a row, ESI addresses a column and "+8" may be used to address a 3rd dimension

jj2007

  • Member
  • *****
  • Posts: 7756
  • Assembler is fun ;-)
    • MasmBasic
Re: How can i make a multidimensional array and access each element
« Reply #4 on: June 12, 2012, 01:30:28 PM »
Either you follow Dave's approach, or you use a library:

include \masm32\MasmBasic\MasmBasic.inc   ; download
   Init
   Dim MyRect(2, 3) As RECT   ; Dim: allowed are word, dword, qword, real4/8/10, structures, ...

   mov eax, 5
   Dim MyArray(eax, 3) As DWORD   ; simple example

   mov MyArray(0, 0), 1000
   mov MyArray(0, 1), 1001
   mov MyArray(1, 0), 1100

   add MyArray(0, 0), 200
   add MyArray(0, 1), 200
   add MyArray(1, 0), 200

   Print Str$("MyArray(0, 0)=%i\n", MyArray(0, 0))
   Print Str$("MyArray(0, 1)=%i\n", MyArray(0, 1))
   Print Str$("MyArray(1, 0)=%i\n", MyArray(1, 0))
   
   Inkey "ok"
   Exit
end start

Code: [Select]
MyArray(0, 0)=1200
MyArray(0, 1)=1201
MyArray(1, 0)=1300

Pure MasmTM  :biggrin:

FORTRANS

  • Member
  • ****
  • Posts: 946
Re: How can i make a multidimensional array and access each element
« Reply #5 on: June 12, 2012, 11:48:19 PM »
Hi,

   The page Gunner pointed to is good.  Here is a sample
of some of my code.

Code: [Select]
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                        ; Number of elements (dimensions+1) in a
Element EQU     3       ; homogeneous vector.  (I.e. 3 for 2D or 4 for 3D.)
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; - - - Vectors - - -
TstVec1 DD      Element DUP ( 0.0 )
vec0    DD      Element DUP ( 0.0 )
vec1    DD      Element DUP ( 0.0 )
; - - - Matrices - - -
MatA    DD      Element * Element DUP ( 0.0 )
IF Element EQ 3
MatB    DD      11.0, 12.0, 13.0,  21.0, 22.0, 23.0,  31.0, 32.0, 33.0
ELSE
MatB    DD      11.0, 12.0, 13.0, 14.0,  21.0, 22.0, 23.0, 2.4
        DD      31.0, 32.0, 33.0, 44.0,  41.0, 42.0, 43.0, 4.4
ENDIF

; - - - Results / Other - - -
MultC   DB      4               ; Size of column in bytes.
MultR   DB      Element * 4     ; Size of row in bytes.
IndexI  DW      0               ; Word addressinf.
IndexJ  DW      0
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; - - -
        MOV     DX,Element      ; I Loop count
MMul_2:
        MOV     [DXReg],DX      ; Protect from multiply
        MOV     AX,DX           ; Create I Index
        DEC     AX              ; Zero based arrays
        MUL     [MultR]         ; Row Size
        MOV     [IndexI],AX
; - - -
        MOV     CX,Element      ; J Loop
MMul_3:
        MOV     AX,CX           ; Create J Index
        DEC     AX              ; Zero based arrays
        MUL     [MultC]         ; Column Index
        MOV     [IndexJ],AX
; - - -
;...
; - - - Actually Process Data - - -
        MOV     BX,[IndexI]
        ADD     BX,[IndexJ]
        ADD     BX,OFFSET MatA  ; A(I,J)
        FLD     DWORD PTR [BX]
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

   Or some such?

Cheers,

Steve N.

hellfix

  • Guest
Re: How can i make a multidimensional array and access each element
« Reply #6 on: June 13, 2012, 02:46:17 AM »
Is there a simpler way of making and accessing an array of bytes using row major
Im using the books example but i dont understand it does ecx change the row
the program is on or does it read the next byte starting at five in a linear manner

.386
.model flat,c
.stack 100h

includelib msvcrt

.data

printf proto arg:ptr byte
string byte "Abby"
                  ,"Fred"
                  ,"john"
                  ,"kent"
                 ,"Mary",0

buffer byte 20 dup(?),0

.code
public main
main proc

        mov ecx,5
        lea esi,string
        lea edi,buffer
        cld                         ;set the direction flag to decrement
                        ;ecx instead of incrementing it
while1: cmp ecx,0                   
        je endloop
   
   push ecx   
   mov ecx,4             ;will the next byte start at four plus 1 starting at five
   rep movsb            ;then move the next byte into the buffer
   pop ecx                ;or is it using the runtime stack as a base index then add
   dec ecx                ; four to get the next few bits or am i lost
   jmp while1
   
endloop:invoke printf,addr buffer
        ret
   

main endp
end main

runtime base index 1, row one
                              then add four

base = 1
read row = 4

base = 2
read row = 4

and so one

or dose move keep track of the last address it used and moves on to the next
address in the string then read four more bytes into the buffer regardless of ecx
= 5

dedndave

  • Member
  • *****
  • Posts: 8751
  • Still using Abacus 2.0
    • DednDave
Re: How can i make a multidimensional array and access each element
« Reply #7 on: June 13, 2012, 07:02:03 AM »
MOV ECX,4 is used as a count
it is then used by the next instruction, REP MOVSB, to move 4 bytes
so, in this case, it is the length of each row

hellfix

  • Guest
Re: How can i make a multidimensional array and access each element
« Reply #8 on: June 13, 2012, 08:16:38 AM »
since it moves four bytes how does it movsb move to the next row below lets abby to the next four bytes to fred how does it know to move to the next byte F for starting of fred s

MichaelW

  • Global Moderator
  • Member
  • *****
  • Posts: 1209
Re: How can i make a multidimensional array and access each element
« Reply #9 on: June 13, 2012, 01:30:23 PM »
It might be worth looking at how a Microsoft C compiler does it. Note that I could have done this with a 16-bit compiler, but the assembly code would have been longer and harder to understand.
Code: [Select]
//=============================================================================
#include <windows.h>
#include <conio.h>
#include <stdio.h>
//=============================================================================

int array[4][4];

int main( void )
{
    int i,j;

    for ( i=0; i < 4; i++ )           // line 13
        for ( j=0; j < 4; j++ )       // line 14
            array [i][j] = i<<4 | j;  // line 15

    for ( i=0; i < 4; i++ )
        for ( j=0; j < 4; j++ )
            printf("%02x ",array [i][j]);
     printf("\n");

    getch();
}
Code: [Select]
; Listing generated by Microsoft (R) Optimizing Compiler Version 13.10.3077

TITLE test.c
.386P
include listing.inc
if @Version gt 510
.model FLAT
else
_TEXT SEGMENT PARA USE32 PUBLIC 'CODE'
_TEXT ENDS
_DATA SEGMENT DWORD USE32 PUBLIC 'DATA'
_DATA ENDS
CONST SEGMENT DWORD USE32 PUBLIC 'CONST'
CONST ENDS
_BSS SEGMENT DWORD USE32 PUBLIC 'BSS'
_BSS ENDS
$$SYMBOLS SEGMENT BYTE USE32 'DEBSYM'
$$SYMBOLS ENDS
_TLS SEGMENT DWORD USE32 PUBLIC 'TLS'
_TLS ENDS
FLAT GROUP _DATA, CONST, _BSS
ASSUME CS: FLAT, DS: FLAT, SS: FLAT
endif

INCLUDELIB LIBC
INCLUDELIB OLDNAMES

_DATA SEGMENT
COMM _array:DWORD:010H
$SG74403 DB '%02x ', 00H
ORG $+2
$SG74404 DB 0aH, 00H
_DATA ENDS
PUBLIC _main
EXTRN _getch:NEAR
EXTRN _printf:NEAR
; Function compile flags: /Odt
_TEXT SEGMENT
_j$ = -8 ; size = 4
_i$ = -4 ; size = 4
_main PROC NEAR
; File c:\program files\microsoft visual c++ toolkit 2003\my\array\test.c
; Line 10
push ebp
mov ebp, esp
sub esp, 8
; Line 13
mov DWORD PTR _i$[ebp], 0
jmp SHORT $L74391
$L74392:
mov eax, DWORD PTR _i$[ebp]
add eax, 1
mov DWORD PTR _i$[ebp], eax
$L74391:
cmp DWORD PTR _i$[ebp], 4
jge SHORT $L74393
; Line 14
mov DWORD PTR _j$[ebp], 0
jmp SHORT $L74394
$L74395:
mov ecx, DWORD PTR _j$[ebp]
add ecx, 1
mov DWORD PTR _j$[ebp], ecx
$L74394:
cmp DWORD PTR _j$[ebp], 4
jge SHORT $L74396
; Line 15
mov edx, DWORD PTR _i$[ebp]
shl edx, 4
or edx, DWORD PTR _j$[ebp]
mov eax, DWORD PTR _i$[ebp]
shl eax, 4
mov ecx, DWORD PTR _j$[ebp]
mov DWORD PTR _array[eax+ecx*4], edx
jmp SHORT $L74395
$L74396:
jmp SHORT $L74392
$L74393:
; Line 17
mov DWORD PTR _i$[ebp], 0
jmp SHORT $L74397
$L74398:
mov edx, DWORD PTR _i$[ebp]
add edx, 1
mov DWORD PTR _i$[ebp], edx
$L74397:
cmp DWORD PTR _i$[ebp], 4
jge SHORT $L74399
; Line 18
mov DWORD PTR _j$[ebp], 0
jmp SHORT $L74400
$L74401:
mov eax, DWORD PTR _j$[ebp]
add eax, 1
mov DWORD PTR _j$[ebp], eax
$L74400:
cmp DWORD PTR _j$[ebp], 4
jge SHORT $L74402
; Line 19
mov ecx, DWORD PTR _i$[ebp]
shl ecx, 4
mov edx, DWORD PTR _j$[ebp]
mov eax, DWORD PTR _array[ecx+edx*4]
push eax
push OFFSET FLAT:$SG74403
call _printf
add esp, 8
jmp SHORT $L74401
$L74402:
jmp SHORT $L74398
$L74399:
; Line 20
push OFFSET FLAT:$SG74404
call _printf
add esp, 4
; Line 22
call _getch
; Line 23
xor eax, eax
mov esp, ebp
pop ebp
ret 0
_main ENDP
_TEXT ENDS
END


Well Microsoft, here’s another nice mess you’ve gotten us into.

Rockphorr

  • Member
  • **
  • Posts: 50
Re: How can i make a multidimensional array and access each element
« Reply #10 on: June 14, 2012, 05:31:41 AM »
use multydimentional elements by struc  --  (m,n,k ...)