0
add $1, (%eax, %ecx, 1)

I know that first it's multiplying ecx by 1 byte, but is it the memory address of ecx or the value of it? And when it adds to eax, is it adding the memory address to it or the value of ecx to eax? Then in the end, it's adding 1 to the memory address of eax? Thanks.

0

1 Answer 1

1

It increments the 32-bit value at address EAX+ECX by one. The (%eax, $ecx, 1) syntax denotes the scale-index-base portion of the operation's encoding, with EAX being base, ECX being index, 1 being scale (i. e. no scale at all). For addressing with a constant displacement 1, the syntax would be 1(%eax, $ecx, 1)

4
  • How does the assembler know that it's incrementing a 32 bit value as opposed to a 16 bit or 8 bit value? With Intel assembler, the operand size needs to be specified, such as "add dword ptr [eax,ecx],1" . In ATT assembler syntax, is "add" the same as "addl"?
    – rcgldr
    Commented Feb 19, 2014 at 19:41
  • The CPU in in 32-bit mode - register names suggest that. Without explicit "byte ptr" or "word ptr", the default is 32-bit operands, even in Intel syntax (at least as understood by MASM). AT&T syntax does expect the operand size postfix after the command (addl instead of add), but maybe some assemblers out there forgive that. Commented Feb 19, 2014 at 20:38
  • Using ML.EXE (MASM 6.11) or later version, I get "error A2070: invalid instruction operands" with instruction "mov [ebx],1" . Changing this to "mov dword ptr [ebx],1 fixes the error (could also use word ptr or byte ptr. If in 64 bit mode, then mov qword ptr [rbx],1 could be used.
    – rcgldr
    Commented Feb 20, 2014 at 1:59
  • @rcgldr: Allowing this ambiguity is a bug or quirk of GAS for some instructions, but not all: mov does forces you to specify an operand-size. There is no "default" operand-size in asm source, or at least there shouldn't be when neither operand-is a register. Good assemblers like NASM (and apparently MASM) will correctly reject the equivalent. However, see this comment about GAS allowing add $8, -8(%rbp) defaulting to 32-bit operand-size in 64-bit mode. (The default op size with no prefixes) Commented Oct 29, 2020 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.