Thread View: borland.public.delphi.language.basm
10 messages
10 total messages
Started by John
Wed, 11 Jun 2008 14:32
Swapping 2 bytes
Author: John
Date: Wed, 11 Jun 2008 14:32
Date: Wed, 11 Jun 2008 14:32
20 lines
1210 bytes
1210 bytes
Hello,
this procedure exchange 2 32bit values :
procedure Swap32(var A, B: TRGBQUAD);
asm
// EAX = [A]
// EDX = [B]
MOV ECX,[EAX] // ECX := [A]
XCHG ECX,[EDX] // ECX <> [B];
MOV [EAX],ECX // [A] := ECX
end;
I mean here A become B and B become A.
How can I do the same for :
procedure Swap8(var A, B: Byte);
Thanks for your help
John
Re: Swapping 2 bytes
Author: Bob Gonder
Date: Wed, 11 Jun 2008 15:12
Date: Wed, 11 Jun 2008 15:12
9 lines
1096 bytes
1096 bytes
John wrote: > MOV ECX,[EAX] // ECX := [A] > XCHG ECX,[EDX] // ECX <> [B]; > MOV [EAX],ECX // [A] := ECX >How can I do the same for : >procedure Swap8(var A, B: Byte); Replace ecx with cl.
Re: Swapping 2 bytes
Author: Les Pawelczyk
Date: Wed, 11 Jun 2008 15:49
Date: Wed, 11 Jun 2008 15:49
39 lines
1852 bytes
1852 bytes
> this procedure exchange 2 32bit values : > procedure Swap32(var A, B: TRGBQUAD); > asm > // EAX = [A] > // EDX = [B] > MOV ECX,[EAX] // ECX := [A] > XCHG ECX,[EDX] // ECX <> [B]; > MOV [EAX],ECX // [A] := ECX > end; The instruction XCHG reg,[mem] is always automatically implementing locking protocol regardless of the presence of LOCK prefix. This makes it very slow. It is only good for thread and process synchronization. procedure Swap32(var A, B: LongInt); register; asm push [eax] mov ecx, [edx] pop [edx] mov [eax], ecx end; > How can I do the same for : > procedure Swap8(var A, B: Byte); Unfortunately you cannot do exactly the same for Byte because you cannot PUSH just one byte onto the stack, but there is a better way: procedure SwapByte(var A, B: Byte); register; asm mov cl, [eax] mov ch, [edx] mov [edx], cl mov [eax], ch end; Les.
Re: Swapping 2 bytes
Author: Sven Pran
Date: Wed, 11 Jun 2008 22:51
Date: Wed, 11 Jun 2008 22:51
53 lines
2412 bytes
2412 bytes
"Les Pawelczyk" <a@b.c> wrote in message news:484ff3fd@newsgroups.borland.com... >> this procedure exchange 2 32bit values : >> procedure Swap32(var A, B: TRGBQUAD); >> asm >> // EAX = [A] >> // EDX = [B] >> MOV ECX,[EAX] // ECX := [A] >> XCHG ECX,[EDX] // ECX <> [B]; >> MOV [EAX],ECX // [A] := ECX >> end; > > The instruction XCHG reg,[mem] is always automatically implementing > locking protocol regardless of the presence of LOCK prefix. This makes it > very slow. It is only good for thread and process synchronization. > > procedure Swap32(var A, B: LongInt); register; > asm > push [eax] > mov ecx, [edx] > pop [edx] > mov [eax], ecx > end; > > >> How can I do the same for : >> procedure Swap8(var A, B: Byte); > > Unfortunately you cannot do exactly the same for Byte because you cannot > PUSH just one byte onto the stack, but there is a better way: > > procedure SwapByte(var A, B: Byte); register; > asm > mov cl, [eax] > mov ch, [edx] > mov [edx], cl > mov [eax], ch > end; > > > > Les. > > XOR A,B XOR B,A XOR A,B is probably the fastest way to swap A and B regards Sven
Re: Swapping 2 bytes
Author: Per Larsen
Date: Wed, 11 Jun 2008 23:49
Date: Wed, 11 Jun 2008 23:49
13 lines
1254 bytes
1254 bytes
"Sven Pran" <no.direct@mail.please> wrote > XOR A,B > XOR B,A > XOR A,B > > is probably the fastest way to swap A and B The coolest, perhaps ;) - but not particularly relevant when the operands are not in registers. - Per
Re: Swapping 2 bytes
Author: John Herbster
Date: Thu, 12 Jun 2008 01:25
Date: Thu, 12 Jun 2008 01:25
7 lines
1117 bytes
1117 bytes
"Sven Pran" wrote > XOR A,B > XOR B,A > XOR A,B Sven, You beat me to it. Rgds, JohnH
Re: Swapping 2 bytes
Author: Rudy Velthuis [TeamB]
Date: Thu, 12 Jun 2008 11:26
Date: Thu, 12 Jun 2008 11:26
18 lines
2295 bytes
2295 bytes
John Herbster wrote: > > "Sven Pran" wrote > > XOR A,B > > XOR B,A > > XOR A,B > > Sven, You beat me to it. Rgds, JohnH Note that the original was "var A, B", i.e. you get passed pointers to A and B. In that case, the code Les posted is much better. -- Rudy Velthuis [TeamB] http://www.teamb.com "The company doesn't tell me what to say, and I don't tell them where to stick it." -- Unknown
Re: Swapping 2 bytes
Author: John
Date: Thu, 12 Jun 2008 12:57
Date: Thu, 12 Jun 2008 12:57
3 lines
987 bytes
987 bytes
Great ! Thanks all for your precious help !
Re: Swapping 2 bytes
Author: Wolfgang Ehrhardt
Date: Thu, 12 Jun 2008 16:39
Date: Thu, 12 Jun 2008 16:39
32 lines
1878 bytes
1878 bytes
On Thu, 12 Jun 2008 13:26:02 +0200, "Rudy Velthuis [TeamB]" <newsgroups@rvelthuis.de> wrote: >John Herbster wrote: > >> >> "Sven Pran" wrote >> > XOR A,B >> > XOR B,A >> > XOR A,B >> >> Sven, You beat me to it. Rgds, JohnH > >Note that the original was "var A, B", i.e. you get passed pointers to >A and B. In that case, the code Les posted is much better. > And it will not give wrong results as with the xor routine. If you call the xor routine with the same variable, ie swap32(a,a), it will leave you with a=0. This can happen for example in a sort routine where you want to swap two array elements with swap32(a[i], a[j]) and i=j! Therefore: Never use the xor swap technique with var parameters! Wolfgang -- In order to e-mail me a reply to this message, you will have to remove PLEASE.REMOVE from the address shown in the header or get it from http://home.netsurf.de/wolfgang.ehrhardt (Free open source Crypto, AES, CRC, Hash for Pascal/Delphi)
Re: Swapping 2 bytes
Author: Rudy Velthuis [TeamB]
Date: Thu, 12 Jun 2008 16:54
Date: Thu, 12 Jun 2008 16:54
13 lines
2227 bytes
2227 bytes
Wolfgang Ehrhardt wrote: > And it will not give wrong results as with the xor routine. If you > call the xor routine with the same variable, ie swap32(a,a), it will > leave you with a=0. True. -- Rudy Velthuis [TeamB] http://www.teamb.com "Comedy is nothing more than tragedy deferred." -- Pico Iyer, Time
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads