0

I am new to x86 assembly. I am trying to write the following instruction in x86 assembly with minimum number of instructions.

Multiply the contents of ESP by 4 and add 0x11233344, storing the result in EDI. Multiply the contents of EAX by 9 and add 0x55667788, storing the result in EBX. Add the two results together and store the result in ECX.

This is what I have so far:

mov edi,  esp           
lea edi,  [edi*4+0x11233344]
lea ebx,  [eax*9+0x55667788]
add ebx,  edi 
mov ecx,  ebx 

I try to check the instruction with an online assembler, it shows that lea ebx, [eax*9+0x55667788] is an invalid instruction. How should i fix this?

xyz123
  • 1
  • 1
  • 4
    The scale in a memory operand can only be one of 1, 2, 4, and 8. Find a different way to multiply `eax` with 9. For example, you could use `[eax+eax*8+0x55667788]`. – fuz Mar 16 '20 at 14:39
  • 1
    Use a compiler to compile a function that takes 2 register args to find a pattern of instructions that works for `return x * 4 + const1 + x*9 + const2`, then adapt for the regs you want to use. BTW, some assemblers (like NASM I think) will accept `eax*9` and split it into base = eax, index = eax, scale factor = 8. Like I said in answering [How to copy a register and do \`x\*4 + constant\` with the minimum number of instructions](https://stackoverflow.com/q/60701395), the scale factor is a 2-bit shift count. (Was that you again, or someone else taking the same class?) – Peter Cordes Mar 16 '20 at 19:48
  • Why did you modify EBX with `add` instead of using another LEA to add two registers and *then* copy to ECX? You already figured out how to use LEA for the earlier part, `lea ecx, [ebx + edi*1]` seems like the obvious next step and would better follow the instructions that nail down which register to use for what. – Peter Cordes Mar 16 '20 at 19:51

0 Answers0