Re: asm and nasm newbie
- From: Frank Kotler <spamtrap@xxxxxxxxxx>
- Date: Thu, 22 Sep 2005 16:57:31 +0000 (UTC)
Zhang Huan wrote:
hi all:
want to get parameter from stack, i use "mov di, [sp+4]". while
compiling error occurs "invalid effective address". what's wrong
It's not a valid addressing mode. :)
16-bit addressing modes are *quite* lame, compared to 32-bit addressing modes - only bx, si, di, and bp are useable (and there are further restrictions on combining them - bx and bp are "base" registers, si and di are "index" registers - you can only use one of each. [bx + si] is valid, [si + di] is not).
32-bit instructions are much more flexible. You can use *almost* any combination of registers you want - except that esp can't be an index register - [eax + esp * 4] is not valid - [esp + eax * 4] is. You can add an offset to any of these.
There's a section on "effective address encoding" in the Friendly Nasm Manual, but I doubt if a beginner will understand it... I don't!
You can use 32-bit instructions in 16-bit code. Nasm *does* understand this, and will add the 67h address size override prefix automatically (as Wolfgang explains). Nasm will even figure out that [eax * 5] can legitimately be coded [eax + eax * 4], and will do so "behind your back". (I wouldn't advise you to actually *use* this.)
If you expect that your 16-bit code will actually run on a 16-bit CPU, use "push bp", "mov bp, sp", etc. as Wolfgang explains, If not (and who's got a 16-bit CPU these days???), Nasm will let you "mix and match" 16- and 32-bit instructions, and will insert the 66h and 67h override bytes as appropriate. There's a small "penalty" in size and speed for doing so, but it's often a "win" anyway.
32-bit coding is a lot easier than 16-bit, in many ways. Interfacing with existing 32-bit OSen is... different. In *my* opinion, it's "harder" (Linux easier than Windows), but some people claim that Windows programming is "just as easy" as dos...
For an introduction to 32-bit coding, see Dr. Paul Carter's tut:
http://www.drpaulcarter.com/pcasm
He uses the C library for the OS-specific stuff, so it'll work with Windows or Linux (and other Unixen?) or djgpp (... but it uses the C library for OS-specific stuff...).
Move on from there when you feel "ready", I guess...
Best, Frank
.
- Follow-Ups:
- Re: asm and nasm newbie
- From: Zhang Huan
- Re: asm and nasm newbie
- References:
- asm and nasm newbie
- From: Zhang Huan
- asm and nasm newbie
- Prev by Date: Re: asm and nasm newbie
- Next by Date: Re: Masm 8
- Previous by thread: Re: asm and nasm newbie
- Next by thread: Re: asm and nasm newbie
- Index(es):
Relevant Pages
|