tags:

views:

119

answers:

2

Hi, I have 2 simple, but maybe tricky questions. Let´s say I have assembler instruction: MOV EAX,[ebx+6*7] - what I am curious is, does this instruction really actually translates into opcode as it stands,so computation of code in brackets is encoded into opcode, or is this just pseudo intruction for compiler, not CPU, so that compiler before computes the value in brackets using add mul and so, store outcome in some reg and than uses MOV EAX,reg with computed value? Just to be clear, I know the output will be the same. I am interested in execution.

Second is about LEA instruction. I know what it does, but I am more interested wheather its real instruction, so compiles does not further change it, just make it into opcode as it stands, or just pseudo code for compiler to, again, first compute adress and than store it.

+2  A: 

1) Yes
2) Yes

ie. they both translate exactly to opcode encodings.

Bear in mind, though, that the under-lieing architecture of modern X86 CPUs is VERY different to what the assembler leads you to expect. So just because it becomes a single instruction doesn't mean it will get processed as a single instruction on the processor.

Goz
+6  A: 

The assembler (not compiler, although it's very similar) will work out the 6*7 bit as 42 and then the instruction is:

MOV EAX,[ebx+42]

This is what the processor can execute, by adding the contents of the ebx register and the constant 42, then using that address to load up eax. There is no instruction that encodes 6 and 7 as different entities, to be multiplied later.

LEA is as real an instruction as any other. Again, the assembler can handle constant-folding (working out fixed values) to get the assembler statement into a usable form for the processor.

You only have to think about the instruction:

mov ax,1+1+1+1+1+1+1

This won't be encoded as some (magical) sequence of bytes eb 01 01 01 01 01 01 01, it will be encoded exactly the same as any of the following:

mov ax,7
mov ax,49-42
mov ax,42/6
paxdiablo
49-2= 47 in my book
Marco van de Voort