News:

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

Main Menu

Array in Proc

Started by Force, July 22, 2014, 09:42:09 AM

Previous topic - Next topic

Force

Hello ...!

I am trying to add items into array

How can i make it in proc ?

.386
.model flat, stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
include \masm32\include\masm32.inc
includelib \masm32\lib\masm32.lib

addProc PROTO:DWORD,:DWORD

.data

        str0    db "item 1",0
        str1    db "item 2",0
        str2    db "item 3",0
        str3    db "item 4",0


ary dd 0

.code
start:
xor ebx,ebx

;invoke addProc,ary,addr str0

mov ary, offset [str0]
mov ebx,4
mov [ary+ebx], offset [str1]
add ebx,4
mov [ary+ebx], offset [str2]
add ebx,4
mov [ary+ebx], offset [str3]


invoke MessageBox, NULL,[ary+8], 0, MB_OK

exit:
invoke ExitProcess,NULL

addProc proc Array:DWORD,Buffer:LPSTR

; I m trying to add items here

ret

addProc endp


end start

dedndave

you have only allocated 1 dword for the array
should be something like
ary dd 4 dup(?)
that allocates 4 dwords   :biggrin:

now - you may not need to add them with code
it is possible to add them, by definition
ary dd str0,str1,str2,str3

str0    db "item 1",0
str1    db "item 2",0
str2    db "item 3",0
str3    db "item 4",0

in that case, the assembler knows "str2" is an address - not the contents
so, OFFSET is not needed

however, that may not suit your needs
there are some decisions to make - or, you're simply not giving us enough information
if the strings are of constant length, then the routine can determine the successive addresses
if not, the routine needs individual pointers
the other thing - how is the routine to know where in the array to place an element ?

beyond all that - let me give you a simple example of a PROC that can be INVOKE'd
        INCLUDE     \masm32\include\masm32rt.inc

MyProc PROTO :DWORD,:DWORD

        .CODE

main    PROC

    INVOKE  MyProc,eax,2
    INVOKE  ExitProcess,0

main    ENDP

MyProc  PROC  arg1:DWORD,arg2:DWORD

    mov     eax,arg1
    mov     edx,arg2
    ret

MyProc  ENDP

        END     main

jj2007

Ha - Dave was faster, as usual :lol:

include \masm32\include\masm32rt.inc

addProc PROTO:DWORD,:DWORD,:DWORD

.data

        str0    db "item 1",0
        str1    db "item 2",0
        str2    db "item 3",0
        str3    db "item 4",0


ary dd 100 dup(?) ; space for 100 items

.code
start:
; xor ebx,ebx
invoke addProc, addr ary, addr str0, 0
invoke addProc, addr ary, addr str2, 2
invoke addProc, addr ary, addr str3, 3
invoke addProc, addr ary, addr str1, 1

invoke MessageBox, NULL,[ary+2*4], 0, MB_OK

exit:
invoke ExitProcess,NULL

addProc proc Array:DWORD,Buffer:LPSTR, index:DWORD
  mov edx, Array
  mov eax, Buffer
  mov ecx, index
  mov [edx+4*ecx], eax
; I m trying to add items here
ret

addProc endp

end start

Force

Thank you dedndave and jj2007

i thought i should not allocate memory  for DWORD  so i was trying to make vector like in c++ and java

dedndave

here is one that adds items individually
it looks for the first 0 in the array, and places the pointer there
;###############################################################################################

        .XCREF
        .NoList
        INCLUDE    \Masm32\Include\Masm32rt.inc
        .List

;###############################################################################################

AddItem PROTO :LPSTR

;###############################################################################################

        .DATA

str0    db "item 1",0
str1    db "item 2",0
str2    db "item 3",0
str3    db "item 4",0

;***********************************************************************************************

        .DATA?

ary     dd 4 dup(?)

;###############################################################################################

        .CODE

;***********************************************************************************************

_main   PROC

        INVOKE  AddItem,offset str2
        print   uhex$(eax),32
        INVOKE  AddItem,offset str0
        print   uhex$(eax),32
        INVOKE  AddItem,offset str3
        print   uhex$(eax),32
        INVOKE  AddItem,offset str1
        print   uhex$(eax),32
        INVOKE  AddItem,offset str2
        print   uhex$(eax),13,10,13,10

;now, let's display the strings

        mov     esi,offset ary
        mov     ebx,4

loop00: lodsd
        print   eax,13,10
        dec     ebx
        jnz     loop00

        print   chr$(13,10)
        inkey
        INVOKE  ExitProcess,0

_main   ENDP

;***********************************************************************************************

AddItem PROC lpStr:LPSTR

;EAX returns 0 if array is full

        mov     edx,offset ary
        xor     eax,eax
        mov     ecx,offset ary+sizeof ary

Loop00: cmp     eax,[edx]
        jz      Found1

        add     edx,4
        cmp     edx,ecx
        jb      Loop00

        jmp short Exit00

Found1: mov     eax,lpStr
        mov     [edx],eax
        mov     al,1

Exit00: ret

AddItem ENDP

;###############################################################################################

        END     _main

Force

Dave i wrote it without allocate Dword

if there is no error should i allocate it ?


.386
.model flat, stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib



addItem PROTO:DWORD,:DWORD

.data

str0    db "item 0",0
str1    db "item 1",0
str2    db "item 2",0
str3    db "item 3",0
str4    db "item 4",0
str5    db "item 5",0
str6    db "item 6",0
str7    db "item 7",0
str8    db "item 8",0
str9    db "item 9",0



.data?

ary dd ?


.code
start:

invoke addItem, addr ary, addr str0
invoke addItem, addr ary, addr str1
invoke addItem, addr ary, addr str2
invoke addItem, addr ary, addr str3
invoke addItem, addr ary, addr str4
invoke addItem, addr ary, addr str5
invoke addItem, addr ary, addr str6
invoke addItem, addr ary, addr str7
invoke addItem, addr ary, addr str8
invoke addItem, addr ary, addr str9


xor ebx,ebx

invoke MessageBox, NULL,[ary+ebx], 0, MB_OK
add ebx,4
invoke MessageBox, NULL,[ary+ebx], 0, MB_OK
add ebx,4
invoke MessageBox, NULL,[ary+ebx], 0, MB_OK
add ebx,4
invoke MessageBox, NULL,[ary+ebx], 0, MB_OK
add ebx,4
invoke MessageBox, NULL,[ary+ebx], 0, MB_OK
add ebx,4
invoke MessageBox, NULL,[ary+ebx], 0, MB_OK
add ebx,4
invoke MessageBox, NULL,[ary+ebx], 0, MB_OK
add ebx,4
invoke MessageBox, NULL,[ary+ebx], 0, MB_OK
add ebx,4
invoke MessageBox, NULL,[ary+ebx], 0, MB_OK
add ebx,4
invoke MessageBox, NULL,[ary+ebx], 0, MB_OK


exit:
invoke ExitProcess,NULL

addItem proc Array:DWORD,Buffer:LPSTR

xor ecx,ecx
mov ebx,00h



mov edx, Array
mov eax, Buffer

detect:
cmp [edx+ecx],ebx
je adding
add ecx,4
jmp detect


adding:
mov [edx+ecx], eax

ret

addItem endp
end start

dedndave

definately   :biggrin:

otherwise, you will have a bug eventually
as soon as you put something in there - then wonder why it's being over-written   :redface:

Force