Re: asm and nasm newbie



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

.



Relevant Pages

  • Comparing 64bit and 32bit CPU benchmark results
    ... 32bit applications when running Windows 64bit). ... The second and more interesting factor was that the CPU ... mov eax,dword ptr ...
    (comp.benchmarks)
  • Re: Win32 software interrupts?
    ... The following information is taken from the Windows Platform SDK. ... Retrieves the complete path in its long filename form for the ... mov si, seg SourcePath; see below ... mov si, offset SourcePath ...
    (comp.lang.asm.x86)
  • Re: Technical superiority of polling loops
    ... What's a bad design is abusing Sleep to try and synchronize two ... Windows is *not* a real-time OS. ... WaitForSingleObject will give you the response you want as quickly as ... mov(NULL, esi); ...
    (alt.lang.asm)
  • Re: Rapport from installation of Ubuntu 7.10 on amd64
    ... This isn't done in Windows nor in Linux ... And when that dont work, bittourment and games, and seeing other apps written by other programmers, may help reengange some energy for the main task. ... But I told you to write your assembler in ... mov ebx Source ...
    (alt.lang.asm)
  • Re: quick ASM program
    ... mov ax, 13h ... int 10h ... To do it in Windows (what you've got there is *not* a Windows program - ... Dos loads it to offset 100h in a segment of its choosing. ...
    (alt.lang.asm)