> I have programmed in ASM, mainly 32 bit, for some time, but using MASM syntax.
> Compared to NASM, it's relatively easy, since it has all extra directives such
> as ADDR, and OFFSET, and then it has macros.
Okay, *cough*, if you say so... Nasm users seem to get along without "OFFSET"... What does "ADDR" do?
> I am writing an operating system, just for the learning.
Okay, are we still in the 16-bit part, or 32-bit? (your example seems to be 16-bit, so I'll assume that...)
> And what I need to know, I will express in example code.
>
> Say we have a string:
>
> string db "String",0x0d,0x0a,0
>
> And I would want to do some kind of loop, comparing the text to 0, that is the
> end of the string, and if then exit on zero.
Okay... you need "strlen", or just to find the end of it? Something like...
xor si, si
find_len:
cmp byte [string + si], 0
jz got_len
inc si
jmp short find_len
got_len:
Now si has the length. Note that Nasm doesn't "remember" that we said "string" was "db", so we *do* need the "byte" size-specifier, but Nasm users get along without "PTR", too. Also, Nasm (without the "-O" switch) defaults to a "near" jump - the "short" tells Nasm that the target is within signed-byte range. (the code would just be longer without it - it would still work)
> In masm syntax, I would do something like:
>
> mov al,[string+cx] ; CX being the index for the loop.
>
> How would I go about doing that?
Use a different processor than x86? I'll eat my linker if that works in Masm syntax! Using 16-bit instructions, only bx, si, di, and bp can by used in an "effective address". "mov al, [string + ecx] would be okay... You can use 32-bit instructions in 16-bit code if you want to. It involves a 67h address size overwrite byte. Make sure that the upper word of ecx is clear. Nasm doesn't require a directive like ".386" to enable 32-bit instructions. ("cpu 386" would cause an error if an instruction not available until *after* 386 was used - not quite the same thing. By default, Nasm allows *any* instruction - programmer's responsibility to make sure it's appropriate for the intended target)
> My friend explained to me, very vaguely, something like "mov al,byte[string+1]",
> but that gives me the "invalid effective address".
That's strange... works for me. The "byte" isn't necessary here - using "al" determines the size of the instruction (true for Masm, too).
The major differences between Masm and Nasm syntax are covered in section 2 of the Nasm manual. The most confusing situation (IMO) is with something like:
mov ax, foo
If that's Nasm syntax, it means what Masm would write as "mov ax, OFFSET foo". If it's Masm syntax, it means what Nasm would write as "mov ax, [foo]"... IF "foo" is defined as "foo dw 42" or something. If "foo" is "foo equ 42", then it stays the same in Nasm!
In Nasm syntax, all memory references are in square brackets, and all of the effective address must be within the brackets - Masm: "es:[si]", Nasm: "[es:si]", Masm "foo[bx]", Nasm "[foo + bx]", etc... as well as Masm: "foo", Nasm, "[foo]". Masm will let you get away with "inc foo" - you often see "inc word ptr foo", but Masm "remembers" what size you said "foo" was. Nasm does not, so you must specify the size "inc word [foo]" when the size isn't specified by the operand.
Anything *not* in square brackets is an "immediate", whether it's a variable name, a label, a literal number, or an expression that evaluates to a number... unless, of course, it's a register :)
Is that enough confusion for the time being? :)
Best,
Frank