Board index » delphi » Re: Swapping 2 bytes - the story behind XOR

Re: Swapping 2 bytes - the story behind XOR


2008-06-17 06:21:12 PM
delphi135
"Aleksandr" <alsha at mail333 dot com>writes
Quote
I don't think XOR-code is more efficient.
It's only sample that it can work with VAR-parameters.
Believeing that I was the one introducing the XOR feature in this thread let
me tell from where i knew this:
In IBM system/360 (mainframe systems) we have an operation XC which actually
performs the XOR process on strings of up to 256 bytes.
This operation is frequently used for zeroing out fields (XC A;A) and also
for swapping fields without any need for temporary storage.
If I remember correct the OP on this thread had a subroutine using register
calling conventions (default in Delphi) which means that the routine:
procedure swap32(A, B: integer);
asm
XOR EAX,EDX
XOR EDX,EAX
XOR EAX,EDX
end;
will always work correct.
regards Sven
 
 

Re: Swapping 2 bytes - the story behind XOR

Sven,
| In IBM system/360 (mainframe systems) we have an operation XC...
Wow! You must be another "Ol'timer." <g>
--
Q
06/17/2008 10:02:45
XanaNews Version 1.17.5.7 [Q's Salutation mod]
 

Re: Swapping 2 bytes - the story behind XOR

"Sven Pran" :
Quote
In IBM system/360 (mainframe systems) we have an operation XC which
actually performs the XOR process on strings of up to 256 bytes.
Yes. And there was PL/I the best optimizer IMO.
--
regards,
Aleksandr
 

Re: Swapping 2 bytes - the story behind XOR

Hi
for real, double, extended what would be the good formulation?
thanks in advance
 

Re: Swapping 2 bytes - the story behind XOR

On Tue, 17 Jun 2008 12:21:12 +0200, "Sven Pran"
<XXXX@XXXXX.COM>writes:
Quote
procedure swap32(A, B: integer);
asm
XOR EAX,EDX
XOR EDX,EAX
XOR EAX,EDX
end;

will always work correct.
Yes. But it is completely useless because it only exchanges the
registers. And
xchg eax, edx
would be another code which does the same useless thing but more
effectively :)
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 home.netsurf.de/wolfgang.ehrhardt
(Free open source Crypto, AES, CRC, Hash for Pascal/Delphi)
 

Re: Swapping 2 bytes - the story behind XOR

"jfj" <XXXX@XXXXX.COM>wrote
Quote
for real, double, extended what would be the good formulation?
Jfj,
Do you mean for swapping between big and little endian?
In-place or to a different destination?
Rgds, JohnH
 

Re: Swapping 2 bytes - the story behind XOR

Wolfgang Ehrhardt writes:
Quote
Yes. But it is completely useless because it only exchanges the
registers. And

xchg eax, edx

would be another code which does the same useless thing but more
effectively :)
Did you read the part of the implied LOCK on XCHG in the other messages
of this thread?
--
Rudy Velthuis [TeamB] www.teamb.com
"Don't be so humble - you are not that great."
-- Golda Meir (1898-1978) to a visiting diplomat
 

Re: Swapping 2 bytes - the story behind XOR

"Rudy Velthuis [TeamB]" <XXXX@XXXXX.COM>writes
Quote
>xchg eax, edx
Did you read the part of the implied LOCK on XCHG in the other messages
of this thread?
Doesn't apply to the register-only variant.
- Per
 

Re: Swapping 2 bytes - the story behind XOR

"Aleksandr" <alsha at mail333 dot com>writes
Quote
"Sven Pran" :
>In IBM system/360 (mainframe systems) we have an operation XC which
>actually performs the XOR process on strings of up to 256 bytes.

Yes. And there was PL/I the best optimizer IMO.
Actually, the old (V2.3.0) PL/I compiler was/is pretty bad compared to
most PC stuff, and even the new versions leave a lot to be desired in
some occasions. Worse, they produce almost unreadable assembler listings
that make it pretty hard to see what's actually going on...
Robert (Using PL/I since 1985...)
--
Robert AH Prins
robert dot ah dot prins on gmail
 

Re: Swapping 2 bytes - the story behind XOR

Robert,
| Robert (Using PL/I since 1985...)
--
Q <quit using PL/1 in 1974>
06/18/2008 20:48:10
XanaNews Version 1.17.5.7 [Q's Salutation mod]
 

Re: Swapping 2 bytes - the story behind XOR

PL/I & Pl/1 are different compilers.
I think PL/I produces well readable and very optimized assembler listings.
--
regards,
Aleksandr (Using PL/I since 1980)
 

Re: Swapping 2 bytes - the story behind XOR

"Aleksandr" <alsha at mail333 dot com>writes
Quote
PL/I & Pl/1 are different compilers.
No, there is no language called PL/1.
Quote
I think PL/I produces well readable and very optimized assembler
listings.
About which PL/I compiler are we talking? As I wrote, the V2.3.0
assembler listings are readable (provided you use the 'lc(N)' compiler
option, with N>= 100), the assembler listing of the Enterprise PL/I
compiler, excusez-le-mot, stink, and unlike the V2.3.0 listings, there's
virtually no trace of any of the PL/I varaables to be found anymore.
As for optimisation, the V2.3.0 compiler is absolutely terrible in many
ways - I once reduced the CPU time of a program from 3 hours to 20
minutes by changing just a single statement. As this was with a former
client, I don't know how the V3R7M0 compiler would handle it. However,
the latter will generate complete {*word*99} code for a 'BY NAME' assignment,
where the destination is BASED.
Finally, the Enterprise Compiler cannot even handle all programs the
V2.3.0 compiler could compile, try this little gem (as it is likely to
wrap, I have added $$$ to the start and end of each line)
$$$%dcl z%z='put edit';proc options(main;q=''''put list(m;do
i=1,2;z(q)skip;do j=$$$
$$$1to 78c=substr(m(i),j;if c=q
z(c;z(c;end;z(q',';dcl(c,q)char,m(2)char(99)init($$$
$$$'%dcl z%z=''put edit'';proc options(main;q=''''''''put list(m;do
i=1,2;z(q)skip;do j=',$$$
$$$'1to 78c=substr(m(i),j;if c=q
z(c;z(c;end;z(q'','';dcl(c,q)char,m(2)char(99)init(',$$$
Robert
--
Robert AH Prins
robert dot ah dot prins on gmail
 

Re: Swapping 2 bytes - the story behind XOR

Quote
$$$%dcl z%z='put edit';proc options(main;q=''''put list(m;do
i=1,2;z(q)skip;do j=$$$
$$$1to 78c=substr(m(i),j;if c=q
z(c;z(c;end;z(q',';dcl(c,q)char,m(2)char(99)init($$$
$$$'%dcl z%z=''put edit'';proc options(main;q=''''''''put list(m;do
i=1,2;z(q)skip;do j=',$$$
$$$'1to 78c=substr(m(i),j;if c=q
z(c;z(c;end;z(q'','';dcl(c,q)char,m(2)char(99)init(',$$$

Robert
Could you, please, translate this to Delphi? Just for fun ;-)
Eriks
 

Re: Swapping 2 bytes - the story behind XOR

Robert,
| No, there is no language called PL/1.
Yes there is. it is the same language as PL/I. Both acronyms have been
routinely and interchangeably used for forty years. <g>(A quick
Google will show that they still refer to the same language.)
--
Q
06/19/2008 10:07:28
XanaNews Version 1.17.5.7 [Q's Salutation mod]
 

Re: Swapping 2 bytes - the story behind XOR

"Eriks Aleksans" <XXXX@XXXXX.COM>writes
Quote
>$$$%dcl z%z='put edit';proc options(main;q=''''put list(m;do
>i=1,2;z(q)skip;do j=$$$
>$$$1to 78c=substr(m(i),j;if c=q
>z(c;z(c;end;z(q',';dcl(c,q)char,m(2)char(99)init($$$
>$$$'%dcl z%z=''put edit'';proc options(main;q=''''''''put list(m;do
>i=1,2;z(q)skip;do j=',$$$
>$$$'1to 78c=substr(m(i),j;if c=q
>z(c;z(c;end;z(q'','';dcl(c,q)char,m(2)char(99)init(',$$$
>
>Robert

Could you, please, translate this to Delphi? Just for fun ;-)
Take your pick from any of these
www.nyx.net/~gthompso/self_pasc.txt
It's a quine, and what more, it is the smallest possible quine in PL/I
using the IBM V2.3.0 compiler (with some pretty non-standard compiler
options) The program throws a load of errors that would make any other
compiler give up, but miraculously, it also produces correctly working
code! It does not compile with the new and shiny Enterprise Compilers...
Robert
--
Robert AH Prins
robert dot ah dot prins on gmail