3

I tought I understood brackets in x86 assembly. In this example, the register ax should contain X, because brackets represents the current address of LABEL.

mov ax, [LABEL]

LABEL:
db "X", 0

But I dont understand the following two assembly lines:

mov al, [ebx]

Why do I need brackets? Is it because ebx is a 32 bits register and ax a 16 bits? Whats the difference with:

mov al, ebx

Or this one, I don't understand why I need brackets...

mov [edx], ax
perror
  • 7,071
  • 16
  • 58
  • 85
Pier-Alexandre Bouchard
  • 5,135
  • 5
  • 37
  • 72

2 Answers2

11

The bracket notation is used to let you access the "value pointed to" by the register or label.

mov ax, [LABEL]

LABEL:
db "X", 0

You are loading ax with the value from the memory labeled by LABEL. In this case, you are copying the 'X' (0x58 ASCII) into the ax register, along with the 0 into the high byte of ax. So ax = 0x0058, with ah = 0, al = 0x58.

LABEL is attached to the address where "X" is located.


This is not a valid operation:

mov al, ebx

And this:

mov [edx], ax

You are moving the value of ax into the first two bytes of "the value pointed to by edx", since ax is a 16 bit register and edx is just holding the memory address where it should be written to.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Nathan
  • 4,017
  • 2
  • 25
  • 20
  • 2
    Note, in some assemblers (actually, most i've seen other than NASM and Yasm), `mov ax, LABEL` actually reads the value at `LABEL`. In those assemblers, you need something like `mov ax, offset LABEL` or the like to get the value *of* `LABEL`. – cHao Apr 22 '13 at 21:43
  • "You are actually setting al with the first byte of the ebx value." vs "You are setting the first two bytes of the "value appointed by edx" to the value of ax.". You are talking about the first byte and the first two bytes. How do you know the difference? 16 bits vs 32 bits? – Pier-Alexandre Bouchard Apr 22 '13 at 21:53
  • 1
    Take a look at this question: http://stackoverflow.com/questions/228200/x86-assembly-registers-why-do-they-work-the-way-they-do AX is a 16 bit register, AL is a 8 bit register and EDX is a 32 bit register. – Nathan Apr 22 '13 at 21:54
  • Yeah, but you said that we are setting the first TWO bytes of the value pointed by edx, to the value of register ax. Is it because ax is a 16 bits (2 bytes) register and so the higher bytes of edx would be set to 0x00? – Pier-Alexandre Bouchard Apr 22 '13 at 22:05
  • You can't actually mov a 32 bit register into a 16 bit register. I'm gonna make a edit to clear this. – Nathan Apr 22 '13 at 22:23
  • mov al, [ebx] is valid? So we store the value (8 bits) of the address of the 32 bits register in al or we store the value of the content of ebx in the al register? Excuse me, something is not clear for me, with asm pointers.. – Pier-Alexandre Bouchard Apr 22 '13 at 22:33
  • mov al, [ebx] is valid, since you are actually saying "copy the value that ebx points to into ax", in this case, 2 bytes will be copied starting at the address appointed to by `ebx`. `mov al, ebx`, though, is not. – Nathan Apr 22 '13 at 22:34
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28691/discussion-between-nathan-and-pier-alexandre-bouchard) – Nathan Apr 22 '13 at 22:40
  • 1
    @cHao What you said is true of MASM/TASM and not of NASM/FASM/YASM. – Alexey Frunze Apr 23 '13 at 03:39
3

If we are using the 32 bit register "EDX" as an adressregister using brackets, then the value inside of EDX will be used as an address that points to a ram location. Beginning from this location we can read or write one byte, two bytes, or four bytes.

Examples for writing one byte, two bytes and four bytes:

location DB ?, ?, ?, ?
mov edx, offset location
mov eax, 04030201h

writing one byte:

mov [edx], al
result:
location DB 01, ?, ?, ?

writing an other one byte:

mov [edx], ah
result:
location DB 02, ?, ?, ?

writing two bytes:

mov [edx], ax
result:
location DB 01, 02, ?, ?

writing four bytes:

mov [edx], eax
result:
location DB 01, 02, 03, 04

Dirk