News:

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

Main Menu

I can't understand what indirect addressing is supposed to do

Started by RedSkeleton007, July 15, 2015, 09:15:02 AM

Previous topic - Next topic

RedSkeleton007

I confess that even now, I don't have a full understanding of how pointers work (even in C++). But the following example from my book really takes the cake:

ESI contains the offset of byteVal. The MOV instruction uses the indirect operand as the source, the offset is dereferenced, and a byte is moved to AL:

.data
byteVal BYTE 10h
.code
mov esi,OFFSET byteVal
mov al,[esi] ;AL = 10h

How and where does the offset get dereferenced? Why did we move a byte into AL? WTF ARE WE SUPPOSED TO ACCOMPLISH HERE?! :dazzled:

MichaelW

??

The syntax MOV AL, [ESI] causes the assembler to encode a MOV instruction, where the destination operand is AL and the source operand is an indirect memory operand, that when executed "dereferences" the pointer in ESI, copying the byte value, in memory at the address specified by ESI, to AL.

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

rrr314159

Red,

You learn by doing not by study. Programming is an activity, not a body of knowledge. Therefore download masm32 package and start working thru the examples. Throw the book away, or if you know someone whom you don't want to be a programmer - give it to him / her
I am NaN ;)

Tedd

Quote from: RedSkeleton007 on July 15, 2015, 09:15:02 AM
ESI contains the offset of byteVal. The MOV instruction uses the indirect operand as the source, the offset is dereferenced, and a byte is moved to AL:

.data
byteVal BYTE 10h
.code
mov esi,OFFSET byteVal
mov al,[esi] ;AL = 10h

How and where does the offset get dereferenced? Why did we move a byte into AL? WTF ARE WE SUPPOSED TO ACCOMPLISH HERE?! :dazzled:
That particular example has very little practical value - the point is to demonstrate the concept only, i.e. ESI is given a memory location (a 'pointer') and then you can get the thing at the memory location that ESI points to.

"MOV EAX,ESI" means: copy the value of ESI into EAX.
"MOV EAX,[ESI]" means: copy the value at the memory location that ESI points to (not the value of ESI itself) into EAX.

This is useful any time you want to access memory locations without explicitly knowing what they will be ahead of time, e.g. a function that prints strings doesn't know what string you want to print until you give it the pointer to that string, and the next time it could be an entirely different string.

Consider this code (not likely to work, as-is):

.data
    someData db 34,58,97,44,57,89,63,47,35,0
.code
    mov esi,OFFSET someData
  next:
    mov al,[esi]
    cmp al,0
    je fin
    call print_al       ;let's just pretend you already have a function that does this
    inc esi
    jmp next
  fin:




Given the number and nature of your questions, I don't know if your book is the right one for you, or simply that you're wanting to understand every minute detail. The best thing to do is write bits of code and play around with instructions to see what happens. Learn how to use the debugger and how to step through instructions, then watch what happens to registers and flags.
Worry less about the theory - it's okay to not 'get' everything in the beginning, some things will just click later when other bits falls into place.
Potato2

hutch--

Red,

There is a difference between a book that is designed to get freshmen through a semester of introductory assembler and the minimum necessary to write Intel ABI compliant code that works reliably on all versions of 32 bit Windows. This forum and the MASM32 SDK that it supports is designed for reliable production code that can be used in commercial software, not just to get freshmen over the line to pass their exams. For getting familiar with modern assembler coding you have the MASM32 SDK, an extensive body of example code and a reasonable number of people who are highly experienced at writing this type of code.

Indirection is something you need to understand, an ADDRESS can be loaded into a register, store that ADDRESS into a pointer variable and you have the first level of indirection, literally the variable is an indirect address of the original data loaded into a register. Now as is common in complex software you can have multiple levels of indirection such as an array of pointers where each pointer in an array is an indirect reference to something else. this allows things as complex as an ADDRESS of an ADDRESS of an ADDRESS.

This style of technique allows you to pass access to a massive range of data with a single pointer and all you need to know is how to pull it apart at the receiving end.