News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Array question

Started by uosunsetter, March 17, 2012, 05:17:41 PM

Previous topic - Next topic

uosunsetter

Hi.

Im very new to assembly. Right now im reading all tutorials and examples that i find. However, i had a trouble with one of them.

In this topic http://www.masm32.com/board/index.php?PHPSESSID=930ec2ace365fba1127e88b7ca12b1af&topic=13710.0 the advice was:

mov     edi,4*IndexNumber  ;IndexNumber = 0,1,2,etc

But i wanted to iterate through the array by using ecx not by IndexNumber and i failed. I also checked other examples they seemed quite diffrent and complicated then this :( A detailed information would be great! But. a small example is also okay :)

Thanks :)

qWord

.data
Array dd 20 dup (?) ; array with 20 elements
.code


mov esi,OFFSET Array
xor ecx,ecx
; access the arrays using scale-index-base-Addressing (SIB)
.while ecx < LENGTHOF Array

; syntax variations for SIB addressing
mov eax,DWORD ptr [esi+ecx*4]
mov eax,DWORD ptr [esi][ecx*4]

; access the array using it's label
mov eax,Array[ecx*4]

inc ecx ; update index
.endw

mov esi,OFFSET Array
xor ecx,ecx
; using one register/pointer to access the array
.while ecx < LENGTHOF Array

mov eax,DWORD ptr [esi]

add esi,4 ; update pointer to next element

inc ecx
.endw


FPU in a trice: SmplMath
It's that simple!

jj2007

Quote from: uosunsetter on March 17, 2012, 05:17:41 PMand i failed

We are brilliant in assembler but ignorant in telepathy. Post your full code and explain what "I failed" means (e.g. "it gave me error A1234 in line 123" etc).

Welcome to the Forum :thumbu

uosunsetter

qword thanks alo for example and quick reply! I'm trying it right now :)

Quote from: jj2007 on March 17, 2012, 05:34:58 PM
Quote from: uosunsetter on March 17, 2012, 05:17:41 PMand i failed

We are brilliant in assembler but ignorant in telepathy. Post your full code and explain what "I failed" means (e.g. "it gave me error A1234 in line 123" etc).

Welcome to the Forum :thumbu

Well you are right but i was too shy to post my code 'cause it was a mess :D Next time, I will be posting anyway :P

jj2007

Quote from: uosunsetter on March 17, 2012, 05:42:38 PM
i was too shy to post my code 'cause it was a mess

New members posting messy code will be eaten alive :green2

baltoro

UOSUNSETTER,   
You should see the stuff that I post,...BLOAT, BLOAT, and more ridiculous BLOAT,...
...Just loaded with pointless sub-routines that check for obscure errors that will never occur in this galaxy,...
Generally, the guys helping on Forums here are geniuses. Posting your code is always best,...everything is then clear.

Quote from: JOCHEN...New members posting messy code will be eaten alive,... :green
...Yeah,...Jochen is a 12 ton Sumo wrestler,...vicious, but,...hilarious,...
Baltoro

uosunsetter

AHAHAHAH  :bg :bg :bg :bg :bg :bg

I wasnt expected that much of fun :D

Anyway the example worked great for me and explained exactly what I wanted!

Btw, I also sometimes become obssesive with error handling and speed. I think this is why I am here :P (For instance i guess OFFSET and LENGTHOF is MASM only and something like macro and something inside me wondering if they affect the performance, is there a faster way doing this? :D My past with .NET made me think of every function etc.. this way :P )

New newby questions coming soon with full codes with me taking the risk of eaten alive :P

dedndave

this is the Campus sub-forum
they aren't allowed to pick on you   :P

uosunsetter

Me again :P I made some searching but couldnt find the answers to my following questions:

OFFSET, SIZEOF, LENGTHOF.... are they hard-coded macros?

Why should I use OFFSET instead of lea? Because it is one time operation? and related to that for performance?

Honestly my question is what do OFFSET, SIZEOF, LENGTHOF do actually? What are their alternatives as pure assembly?

Lastly, are they the fastest? Or they just fit every situation and makes things easier but not that fast?

jj2007

Quote from: uosunsetter on March 18, 2012, 12:33:44 AM
OFFSET, SIZEOF, LENGTHOF.... are they hard-coded macros?

Why should I use OFFSET instead of lea? Because it is one time operation? and related to that for performance?

Honestly my question is what do OFFSET, SIZEOF, LENGTHOF do actually? What are their alternatives as pure assembly?

Lastly, are they the fastest? Or they just fit every situation and makes things easier but not that fast?

They are pure assembly, and mov eax, offset buffer is the fastest way to get a pointer to a buffer. Same for sizeof and lengthof.

Don't worry for speed. The routines you find in this forum, be it Masm32 lib (len, MemCopy, ... see \masm32\help\masmlib.chm) or MasmBasic (Len, Bmove, ..., see \masm32\MasmBasic\MbGuide.rtf) cannot be beaten with even "purer" assembler. Check the Laboratory for evidence - here are a quite a number of members who enjoy demonstrating that optimising C compilers are sloooow :bdg

dedndave

SIZEOF and LENGTHOF are assembler operators that do not generate code, per se

SIZEOF - yields the number of bytes in a data define
LENGTHOF - yields the number of elements in a data define

consider the following data...
dwArray dd 5 dup(?)
we have defined 5 dwords, which consumes 20 bytes

        mov     ecx,sizeof dwArray
the assembler replaces "sizeof dwArray" with the number of bytes in dwArray
so, it generates code like this
        mov     ecx,20

        mov     ecx,lengthof dwArray
the assembler replaces "lengthof dwArray" with the number of elements in dwArray
        mov     ecx,5

OFFSET and ADDR are a bit more complicated   :P

        mov     edx,offset dwArray
the assembler replaces "offset dwArray" with the address of dwArray
it might look something like this...
        mov     edx,00407270

offset works fine for global data like that defined in the .DATA or .DATA? sections
however, if you define LOCAL data on the stack, the assembler does not know the address at assembly-time
in these cases, you want to use LEA to get the address
the ADDR operator is a shortcut
the assembler generates the appropriate code for the situation
if it is global data, the assembler uses MOV
if it is local data, the assembler uses LEA

it is important to understand that, if ADDR is used in an INVOKE, it uses the EAX register
        LOCAL   dwData:DWORD

        INVOKE  SomeFunction,addr dwData

the assembler generates code that looks something like this...
        lea     eax,dwData
        push    eax
        call    SomeFunction


however, if you have some value in EAX, problems may arise
        INVOKE  SomeFunction,eax,addr dwData
the assembler will generate an "out of registers" error because you are attempting to use EAX twice

on the other hand,
        INVOKE  SomeFunction,addr dwData,eax
should be ok, because the value in EAX is pushed before the assembler uses LEA EAX,dwData
        push    eax
        lea     eax,dwData
        push    eax
        call    SomeFunction

uosunsetter

Omg...!!!

Again I am given very good and accurate information. You are really helping me learn I mean really!

Thanks again and again...