X86 build issue

This is my build program, which is just a * x * y exchange function. So the first argument to main is the address x, which is in 8(%ebp)

, and the second address, y, is in 12(%ebp)

. The program swaps x and y. I need 7 lines for this. You can do this 6 rows and there is a condition that you can only use %eax

, %ecx

and %edx

3 registry. I think about it so much, but I can't seem to do it 6 lines. There has to be a way, right? It may not be very important, but if there is a way to get it in 6 lines, I want to know.

movl 8(%ebp), %eax
movl (%eax), %ecx
movl 12(%ebp), %edx
movl (%edx), %eax
movl %ecx, (%edx)
movl 8(%ebp), %ecx
movl %eax, (%ecx)

      

+2


a source to share


4 answers


Maybe you can use the xor swap trick:



http://en.wikipedia.org/wiki/Xor_swap

+1


a source


Which assembler are you using and which processor are you targeting?

If you are using MASM you can add an offset to the register like this:

mov eax, ebp - 12
mov ecx, ebp - 8
mov ebp - 12, ecx
mov ebp - 8, eax

      



Alternatively, you can use the xchg command and do it in 3 lines:

mov eax, ebp - 12
xchg ebp - 8, eax
xchg ebp - 12, eax

      

It seems so simple that maybe I am missing something?

+1


a source


The Motorola syntax isn't really mine, but here's a shot at it in 5 instructions:

movl 8(%ebp), %eax
movl (%eax), %ecx
movl 12(%ebp), %edx
xchg (%edx), %ecx
movl %ecx, (%eax)

      

See Pascal's comment for a shorter, possibly slower one. xchg %reg,(mem)

will likely be slower than address reloading due to implicit prefix lock

.

+1


a source


I understood! this is based on the xor substitution trick. but something else ^^; answer

movl    8(%ebp), %eax 
movl    (%eax), %ecx 
movl    12(%ebp), %edx 
xorl    (%edx), %ecx
xorl    %ecx, (%eax) 
xorl    %ecx, (%edx) 

      

as with using a single memory access. because in x86 source and destination both cannot access memory with instruction. only one can be used in each individual instruction. so i use it like that.

0


a source







All Articles