Author Topic: quick question  (Read 15097 times)

Offline annonymous

  • Jr. Member
  • *
  • Posts: 13
quick question
« on: May 20, 2012, 04:49:32 PM »
What are the brackets used for in nasm? Example:
Code: [Select]
mov [bx], 99

Offline Cyrill Gorcunov

  • NASM Developer
  • Full Member
  • *****
  • Posts: 179
  • Country: 00
Re: quick question
« Reply #1 on: May 20, 2012, 05:51:38 PM »
Memory reference. Ie address of memory lays in bx.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: quick question
« Reply #2 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


Offline annonymous

  • Jr. Member
  • *
  • Posts: 13
Re: quick question
« Reply #3 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???

Offline Stephen

  • Jr. Member
  • *
  • Posts: 2
Re: quick question
« Reply #4 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.

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: quick question
« Reply #5 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

About Bryant Keller
bkeller@about.me