NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: annonymous on May 20, 2012, 04:49:32 PM

Title: quick question
Post by: annonymous on May 20, 2012, 04:49:32 PM
What are the brackets used for in nasm? Example:
Code: [Select]
mov [bx], 99
Title: Re: quick question
Post by: Cyrill Gorcunov on May 20, 2012, 05:51:38 PM
Memory reference. Ie address of memory lays in bx.
Title: Re: quick question
Post by: Frank Kotler on May 20, 2012, 05:59:41 PM
To generate the "operation size not specified" error. :)

Seriously, as Cyrill says, to move the value 99 (byte? word? dword?) into memory pointed to by bx, rather than into the bx register itself (which we know is a word).

Best,
Frank

Title: Re: quick question
Post by: annonymous on May 20, 2012, 06:10:01 PM
Ok I get why the brackets are used now but nit the actual use of them. Still kind of a little fuzzy. So it is the same concept as a c programming pointer? What exactly am I referencing? Why not just use direct access???
Title: Re: quick question
Post by: Stephen on May 20, 2012, 06:49:59 PM
If you wanted to fill a block of memory with a value you don't want a program line for location (ie clearing the screen or a buffer) So you do a loop and inc bx each time through the loop. That's just the first example to came to mind.
Title: Re: quick question
Post by: Bryant Keller on May 21, 2012, 05:07:38 AM
Ok I get why the brackets are used now but nit the actual use of them. Still kind of a little fuzzy. So it is the same concept as a c programming pointer? What exactly am I referencing? Why not just use direct access???

The brackets are just telling the assembler that the argument is a "MEM" type, like mem8, mem16, mem32, etc. Let's look at a different example.

Code: [Select]
mov eax, 0x100
This example will put the immediate value (0x100) into the eax register.

Code: [Select]
mov eax, [0x100]
This example puts the 32-bit value, which is stored in memory at the address 0x100, into eax.

Code: [Select]
mov ebx, 0x100
mov eax, [ebx]

And finally, this code places the immediate value (0x100) into the ebx register, and then uses the contents of the ebx register as the address to memory (a pointer) whose contents gets stored in eax.


If you can understand that basic concept, we can now include labels into the mix.

Code: [Select]
org 0x100
myVar: DD 0xDEADBEEF
; ... some other stuff ...
   mov eax, [myVar]
   mov eax, [0x100]

These two mov statements are exactly the same. Both set the value stored at memory location 0x100 into the eax register. The only difference is that the first tends to be more readable.

I hope I didn't confuse you too much. :P

HtH,
Bryant