News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

How can i make a multidimensional array and access each element

Started by hellfix, June 12, 2012, 10:20:32 AM

Previous topic - Next topic

hellfix

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

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

Gunner

http://webster.cs.ucr.edu/AoA/Windows/HTML/Arraysa2.html#1000856
~Rob

dedndave

you can create a linear array, then use different registers to address different row or columns
        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

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

MyArray(0, 0)=1200
MyArray(0, 1)=1201
MyArray(1, 0)=1300


Pure MasmTM  :biggrin:

FORTRANS

Hi,

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


; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                        ; 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

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

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

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

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.

//=============================================================================
#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();
}


; 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

use multydimentional elements by struc  --  (m,n,k ...)