Im a little confused about these two instructions. addr get's the address of the label,so does offset right? So, the assembler first checks the source code for offset comands and get's the adresses before the function is invoked right? But is there no checking for addr commands. So if i declare some labels after the method invokation the addr won't work because of that? Why not then just use offsets and forget addr?
Im probbably missing something. :P
Posted on 2005-08-15 04:49:54 by blackswan
Okey I can use addr for local variables. So we get adress during runtime, and offset can't do that, because he get's the addres before assembling the code. I kind of understand it now. Maybe I will more later when more examples come.
Posted on 2005-08-15 04:55:47 by blackswan
no no. The cpu can compute an address both via "direct" address( via "offset") or with register-based addressing ( mov edx, )
jmp/jXX and "call" can use relative-addresses: "jmp $-16 ", these instructions usually take up less bytes in the ".code" section.
In MASM, "ADDR" serves another purpose - it either translates to a
push offset X ; where X is a global variable (we know its "direct" address)
or for register-based addressing:
lea eax,
push eax
So, "addr" is just a macro. With local variables, note that it overwrites EAX.
Ah, and "addr" can only be used with "invoke"
Posted on 2005-08-15 05:50:48 by Ultrano
Okey,thanks for explaining. But what's a lea command ? load eax register?
So everytime I use addr this macro of two lines of code get's executed. But where is the base pointer register pointning, and how do we know what to add to base pointer to get the address of our variable?
Posted on 2005-08-15 06:26:32 by blackswan
LEA is Load Effective Address and it is useful among other things for loading the address of a local variable into a register. When a variable is set in the .DATA section, its address can be set at assembly time by the assembler, with a local variable, it can only be set at run time.
Posted on 2005-08-15 08:11:01 by hutch--
LEA is Load Effective Address and it is useful among other things for loading the address of a local variable into a register. When a variable is set in the .DATA section, its address can be set at assembly time by the assembler, with a local variable, it can only be set at run time.
yes 8) thanks. Which good tutorials would you recommend for further reading.
p.s.
Why do people say that programming in assembler gives shorter code? I see that it's faster, but I dont see it shorter, more the opposite.
Posted on 2005-08-15 09:40:00 by blackswan
LEA is a funny complex instruction:
lea eax,
is equal to the C expression:
eax = ebp + ecx*4 + 43;
(all those are registers)
But also, if in "lea eax,X" "X" is a global variable (so we know its direct address, let's assume it's 0x410308 ) , the assembled code is
lea eax,0x410308
which is equal to
mov eax,0x410308
or also to
mov eax,offset X
Why do people say that programming in assembler gives shorter code?
They probably mean the resulting .exe size is smaller (less _compiled_ , binary code). In my experience C _source_ code is around 7-10 times shorter than asm code. Yet, there are so many situations when asm code is 10 times shorter than the same algo in C.
If the coder uses "macros," then he can save a lot of typing too! After having a suitable set of macros, typing in asm becomes a bit faster and easier than in C/C++ .
A nice macro example from my custom base includes is "foreach" :
foreach AllWindows, delete EDX
The code above, in C++ is roughly:
if(AllWindows->Lock()){
for(long i=0;i<AllWindows.NumElements;i++){
delete (CWindow*)AllWindows->Elements;
}
AllWindows->FlushDeletes();
AllWindows->FlushInserts();
AllWindows->Unlock();
}
Might not be the best example, but anyway - macros can save you LOTS of typing, headaches and debugging problems. Unfortunately not enough people know them well.
Posted on 2005-08-15 11:17:01 by Ultrano
So everytime I use addr this macro of two lines of code get's executed. But where is the base pointer register pointning, and how do we know what to add to base pointer to get the address of our variable?
The base pointer EBP is set up by the standard
calling convention. If you define your functions using PROC with arguments and LOCALs, the displacements will be calculated for you. Within the body of the PROC, EBP will be saved and reset on entry, and every RET will restore EBP.
As this is related to Win32ASM programming, you should discuss this in the Main forum.
Posted on 2005-08-15 13:31:27 by tenkey
blackswan,
> yes Cool thanks. Which good tutorials would you recommend for further reading.
I would recommend getting the Intel PIV manuals, they are complicated and large but they are the reference for x86 arachitecture, the complete instruction set and many of the major optimisation techniques. Iczelion's tutorials are worth having, Thomas Bleekers snippets are good value and if you like really low level examples, Test Departments examples are very good. Have a look a the links at www.website.masmforum.com as there is a lot of useful info there.
You definitely need an API reference for win32 programming.
> Why do people say that programming in assembler gives shorter code? I see that it's faster, but I dont see it shorter, more the opposite.
Depends what you are writing, if you have to write functionality from scratch without using built in language operators or libraries, assembler is fine but higher level languages have a lot of prebuilt code that makes development faster. With an assembler you use macros, API calls and libraries to get you speed up and you still have all of the low level power if you need it.
Regards,
hutch at movsd dot com
Posted on 2005-08-15 21:34:53 by hutch--