Re: Iczelion's tutorials revisited.
- From: Frank Kotler <fbkotler@xxxxxxxxxxx>
- Date: Wed, 31 Aug 2005 01:11:16 -0400
JGCASEY wrote:
....
Its been six years since I last used assembler so I am most likely a little bit rusty. By "local" variables on the stack I assume something like this?
Pretty much, yeah...
push bp mov bp,sp
This is enough to cover parameters passed on the stack. For "local"/"automatic" variables, we also need to subtract something from (e)sp - so we can push/pop without overwriting our variables.
sub (e)sp, number of local variables * size of variable
Normally, "size" would be two (bytes) for 16-bit code, and 4 for 32-bit code. IOW, the amount to subtract is the number of *bytes* taken up by local variables, even though they're probably mostly dwords. It's okay to have byte or word variables on the stack. If you come out with an "odd" number, you want to realign the stack,,,
and (e)sp, -4
You might want a larger alignment than this - if you're using 64- or 128-bit variables, for example.
.. then you use [bp + displacement] to access the variables,
Yeah. [(e)bp + displacement] for the parameters, [(e)bp - displacement] for local variables, actually.
then exit with,
First, we want to restore (e)sp to its original value:
mov (e)sp, (e)bp
This has the effect of "freeing" the local variables (this is why C calls this "automatic" storage). Needless to say, we don't want to alter (e)bp in the interim!!! In 16-bit code, [sp] isn't a valid addressing mode, but in 32-bit code, we *can* do [esp + displacement], so we *can* access parameters and locals that way. The "displacement" of a particular variable will change as we push/pop, so it's a PITA to keep track of, but it frees up ebp to use as a "general purpose" register, which is sometimes worthwhile.
pop bp ret 4 ;remove 4 or whatever number from stack.
Whether it's "ret number_of_bytes" or just "ret" depends on the calling convention. The Windows API uses "stdcall" in which "callee cleans up stack" - the Windows functions end with "ret N". The C calling convention expects "caller cleans up stack" - the C functions end with just "ret". We need to know which we're dealing with!
Another issue is the "Intel ABI" (Application Binary Interface). Personally, I think it's a misleading name, since the hardware doesn't give a damn, but that's what it's called. Functions are allowed to trash certain registers, but are expected to return others the same way we found 'em. Registers that must be saved/restored if they're altered are ebx, esi, edi, ebp, and of course esp. The return value is normally in eax, maybe edx:eax - ecx is "scratch". Being an old dos-head, I'm used to using (e)cx as a "counter", and it annoys me that calling libc or the Windows API is allowed to trash it, but that's life...
In your *own* routines, you can do it any way you like (more "maintainable" if you're consistant), but if you're calling Windows you can expect that behavior, and if Windows is calling you (your WndProc, for example), then your routine has to obey these rules.
....
However I couldn't find the "teach yourself Window Assembler programming in 21 days" book :)
"Windows Assembly for Dummies" is right there on the bookshelf next to "Military Intelligence" and "Jumbo Shrimp" :)
Betov makes a good point that you don't really "learn" the entire Windows API. You need to learn how to navigate the documentation and *find* the API(s) you need. I suppose this gets easier with practice. I've spent *no* time with Windows documentation, so the clock hasn't even started ticking for me :)
....
So what kind of programs do you write these days?
"Hello World" for Linux, mostly - not literally "Hello World", but nothing too advanced. I suppose I'm "learning" the Linux API, a little bit at a time.
....
I have already got as far as "processing" an image using the QuickCam in dos and in a VC++ shell with a LogiTech webcam.
That should be a good start - at least you'll know what to do with the camera.
It is the fairly simple API stuff I have to get my head around.
There must be some kind of "Quick Start" document - "The Dozen Most Frequently Used APIs", or "21 APIs in 21 Days", or something. If there isn't, perhaps there should be. I *thought* that's more or less what Iczelion's tuts did...
The VC++ shell was written using the MFC Wizard by someone else not willing to show me how it was done.
That'd be "OOP", I guess - where "information hiding" is condidered a *good* thing (!). Perhaps *not* a good place to start if you want to learn to do it yourself...
When I saw the FASM example I thought cool, I wonder how hard that would be?
Well, it doesn't look too bad. Can you get it to "work", as far as it goes?
I think I mentioned that I sponged a USB camera, so that I could "follow along", maybe learn how to do it in Linux... When I went to plug it in, I find I haven't *got* a USB port on this machine! (thought I did...) So that's not going to be happening... unless I can figure out what happened to the parallel-port camera I *used* to have... I still find the project "interesting".
Best, Frank .
- Follow-Ups:
- Re: Iczelion's tutorials revisited.
- From: JGCASEY
- Re: Iczelion's tutorials revisited.
- References:
- Iczelion's tutorials revisited.
- From: hutch--
- Re: Iczelion's tutorials revisited.
- From: randyhyde@xxxxxxxxxxxxx
- Re: Iczelion's tutorials revisited.
- From: hutch--
- Re: Iczelion's tutorials revisited.
- From: randyhyde@xxxxxxxxxxxxx
- Re: Iczelion's tutorials revisited.
- From: f0dder
- Re: Iczelion's tutorials revisited.
- From: Guga
- Re: Iczelion's tutorials revisited.
- From: hutch--
- Re: Iczelion's tutorials revisited.
- From: Paul Dunn
- Re: Iczelion's tutorials revisited.
- From: f0dder
- Re: Iczelion's tutorials revisited.
- From: JGCASEY
- Re: Iczelion's tutorials revisited.
- From: Betov
- Re: Iczelion's tutorials revisited.
- From: JGCASEY
- Re: Iczelion's tutorials revisited.
- From: Betov
- Re: Iczelion's tutorials revisited.
- From: Frank Kotler
- Re: Iczelion's tutorials revisited.
- From: JGCASEY
- Iczelion's tutorials revisited.
- Prev by Date: Re: Polling loop good here???
- Next by Date: Re: HLA Adventure tidbit in October 2005 Linux Journal
- Previous by thread: Re: Iczelion's tutorials revisited.
- Next by thread: Re: Iczelion's tutorials revisited.
- Index(es):
Relevant Pages
|