4

I thiiink I understand that

movzbl (%rdi, %rcx, 1) , %ecx

means "move zero-extended byte to long" and is saying to extend ecx into 32 bits, but I'm not entirely sure what the syntax (%rdi, %rcx, 1) refers to.

I've seen somewhere that that syntax refers to

(Base, Index, Scale)

but I can't find any resources that say what that means exactly. I'm guessing it means to move whatever info is at (%rdi, %rcx, 1) to %ecx so that

(long) %ecx = (%rdi, %rcx, 1)

but how do I figure out what location that is? is there some sort of arithmetic involved to find an address or something?

also isn't ecx implicitly 32 bits already? Why would it need to be extended into 32 bits?

edit for clarification:

I understand that the syntax (%rdi, %rcx, 1) means that I have to add those three things together, but I don't understand how that results in an answer.

What am I adding, the contents of the registers? The address of the register? If it's the address, how do I figure out what the address is and add it together?

All I'm finding online is telling me what the syntax means but not how to use it with examples.

movac
  • 1,576
  • 3
  • 21
  • 45
  • heyyy why the down vote =[ – movac Oct 31 '15 at 15:53
  • 2
    _no research effort_ = Tons of answers for this. See for example [here](http://stackoverflow.com/a/29420062/547981) or the [official manual](https://sourceware.org/binutils/docs-2.20/as/i386_002dMemory.html). And the byte fetched from memory is extended to 32 bits. – Jester Oct 31 '15 at 15:56
  • @Jester I already understand that part, I just can't figure out how to do the arithmetic. Like, how do I figure out the address %rdi + %rcx, is it just the contents of those registers added together = the address? I've looked online and can't find a good tutorial on how to do it. – movac Oct 31 '15 at 16:02
  • @Jester I understand what it /means/ from online resources already, I just don't know how to go about getting the correct result – movac Oct 31 '15 at 16:04
  • What else could you add? Of course you add the content of the registers. If you don't know this, then you should have read section _3.7.5 Specifying an Offset_ in the intel basic architecture manual. A register doesn't have an address, it's not part of the memory address space. – Jester Oct 31 '15 at 16:13
  • ok but in the code I have nothing has ever been passed into those registers before this point so aren't they empty? I'm not understanding how this would translate into C. Or if they are showing up at that point with nothing having previously been "mov"ed into them, does that imply that in C there was already something passed into them in args or something? – movac Oct 31 '15 at 16:19
  • 1
    Something must have been loaded at least into `rcx`. According to standard calling convention, `rdi` is used to pass first argument so that might be the case here. But this is a different question for which you haven't provided enough code. – Jester Oct 31 '15 at 16:21
  • @Jester Technically, segment registers are also involved, but you can ignore them unless you are doing really weird stuff. – fuz Oct 31 '15 at 20:23

1 Answers1

1

To quote the intel basic architecture manual:

3.7.5 Specifying an Offset The offset part of a memory address can be specified directly as a static value (called a displacement) or through an address computation made up of one or more of the following components:

  • Displacement -- An 8-, 16-, or 32-bit value.
  • Base -- The value in a general-purpose register.
  • Index -- The value in a general-purpose register.
  • Scale factor -- A value of 2, 4, or 8 that is multiplied by the index value.

The offset which results from adding these components is called an effective address.

Notice it says "the value in a general-purpose register". As registers are not part of the memory address space on x86, they don't even have an address, so the only thing you can possibly use is the value in them.

As for the movzbl: it instructs the cpu to fetch a byte from memory, and zero extend it to 32 bits.

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
Jester
  • 56,577
  • 4
  • 81
  • 125
  • For future reference, https://github.com/HJLebbink/asm-dude/wiki/MOVZX is the Intel mnemonic. – Peter Cordes Mar 16 '18 at 08:49
  • 2
    And since this is x86-64, writing the result to a 32-bit register implicitly zero-extends it to 64-bit. So it's an 8-bit zero-extending load into a 64-bit register. – Peter Cordes Oct 15 '20 at 22:19