News for hackers. Stuff that matters.
Re:Why xor eax, eax? by jgrahamc on Monday December 01, 2025 @09:29AM (#46107832)
In my 6502 hacking days, the presence of an exclusive OR was a sure-fire indicator you’d either found the encryption part of the code, or some kind of sprite routine.

Yeah, sadly the 6502 didn't allow you to do EOR A; while the Z80 did allow XOR A. If I remember correctly XOR A was AF and LD A, 0 was 3E 01[1]. So saved a whole byte! And I think the XOR was 3 clock cycles fast than the LD. So less space taken up by the instruction and faster.

I have a very distinct memory in my first job (writing x86 assembly) of the CEO walking up behind my desk and pointing out that I'd done MOV AX, 0 when I could have done XOR AX, AX.

[1] 3E 00

Re:Why xor eax, eax? by wavemode on Monday December 01, 2025 @09:51AM (#46108116)
> CEO walking up behind my desk and pointing out that I'd done MOV AX, 0 when I could have done XOR AX, AX

Now that's what I call micromanagement.

(sorry couldn't resist)

Re:Why xor eax, eax? by xigoi on Monday December 01, 2025 @09:58AM (#46108219)
The real joke is that a CEO had actual technical knowledge instead of just being there for decoration.
Re:Why xor eax, eax? by jgrahamc on Monday December 01, 2025 @09:53AM (#46108148)
He was right though. We were memory and cycle constrained and I'd wasted both!
Re:Why xor eax, eax? by mkornaukhov on Monday December 01, 2025 @10:51AM (#46108821)
Similarly, the CEO couldn't resist the outstanding optimization of memory and execution speed!
Re:Why xor eax, eax? by 6510 on Monday December 01, 2025 @01:12PM (#46110793)
No one believes this story.
Re:Why xor eax, eax? by jgrahamc on Monday December 01, 2025 @01:44PM (#46111264)
I am sad you don't believe this story. The CEO was very technical and this is exactly the sort of thing he would spot.
Re:Why xor eax, eax? by bombcar on Monday December 01, 2025 @02:31PM (#46111958)
People don't realize that in the era of dinosaurs where MASM ruled and assembly walked the earth, there basically WEREN'T CEOs who didn't know the details, because all the companies doing this stuff were pretty small at the time (and the CEO may have been writing it himself a few years before).
Re:Why xor eax, eax? by Analemma_ on Monday December 01, 2025 @03:18PM (#46112614)
There was a time when Bill Gates wrote code for Microsoft, and he was actually quite good at it.
Re:Why xor eax, eax? by nomel on Monday December 01, 2025 @04:39PM (#46113605)
Not sure why this was voted down. He was very technical, especially for the time: https://www.thecrimson.com/article/2025/6/7/bill-gates-reuni...
Re:Why xor eax, eax? by eru on Monday December 01, 2025 @09:28PM (#46116671)
He also wrote and published a paper on pancake sorting.
Re:Why xor eax, eax? by assimpleaspossi on Monday December 01, 2025 @07:49PM (#46115846)
In the era of dinosaurs, neither MASM nor Windows existed but we still did assembly or micro-coding (machine coding) or flipped switches.
Re:Why xor eax, eax? by bombcar on Tuesday December 02, 2025 @12:49AM (#46118041)
Pre-MASM Dinos probably weren't doing xor ax, ax.
Re:Why xor eax, eax? by 6510 on Monday December 01, 2025 @04:15PM (#46113324)
Similarly, if you told people in the 80's that it would be the opposite in the future no one would believe it either.

Not even the developers are very technical in the future!

Woah, really? And they still manage to write good software?

Of course not, if good software would be standing next to their bed at 4 am they would scream who are you what are you doing here? help! help! Someone, make it go away!

Re:Why xor eax, eax? by OrderlyTiamat on Monday December 01, 2025 @02:50PM (#46112207)
My first part time dev job as a student featured me walking in on our CEO who showed me he was recompiling his kernel to enable some features. I'm quite sure he was just doing that to impress the students, but at least he knew how to!
Re:Why xor eax, eax? by eru on Monday December 01, 2025 @11:30PM (#46117551)
Perhaps his secretary showed him?
Re:Why xor eax, eax? by eru on Monday December 01, 2025 @09:19PM (#46116613)
CEO doesn't need to mean some big boss. If you have a three person startup, the CEO might just be your co-founding buddy.
Re:Why xor eax, eax? by crest on Monday December 01, 2025 @04:15PM (#46113313)
I had to pad the code for alignment reasons. ;-)
Re:Why xor eax, eax? by ksherlock on Monday December 01, 2025 @05:01PM (#46113905)
I mean, he IS the Chief EORfficer
Re:Why xor eax, eax? by stevefan1999 on Monday December 01, 2025 @10:50AM (#46108811)
> In my 6502 hacking days, the presence of an exclusive OR was a sure-fire indicator you’d either found the encryption part of the code, or some kind of sprite routine.

Correct. Most ciphers of that era should be Feistel cipher in the likes of DES/3DES, or even RC4 uses XOR too. Later AES/Rijndael, CRC and ECC (Elliptic Curve Cryptography) also make heavy use of XOR but in finite field terms which is based on modular arithmetic over GF(2), that effectively reduces to XOR (while in theory should be mod 2).

Re:Why xor eax, eax? by OhMeadhbh on Monday December 01, 2025 @12:32PM (#46110248)
I was going to say "but RC4 and AES were published well after the 6502's heyday," but NESes were completely rocking it in '87 (and I'm told 65XX cores were used as the basis for several hard drive controllers of the era.) Alas, the closest I ever came to encryption on a (less than 32-bit system) was lucifer on an IBM channel controller in the forever-ago and debugging RC5 on an 8085.
Re:Why xor eax, eax? by kjs3 on Monday December 01, 2025 @01:21PM (#46110898)
I'm told 65XX cores were used as the basis for several hard drive controllers of the era

Western Design Center is still (apparently) making a profit at least in part licensing 6502 core IP for embedded stuff. There's probably a 6502 buried and unrecognized in all sorts of low-cost control applications laying around you.

RC5 on an 8085

Oof. Well played.

Re:Why xor eax, eax? by PaulHoule on Monday December 01, 2025 @02:09PM (#46111619)
I dunno. The 6502 has been a $2 part for a long time but needs RAM and some glue logic, for a similar price you can get an AVR-8 [1] or ESP-32 [2] and get some RAM and GPIO.

[1] faster, more registers than the IBM 360, << 64k RAM

[2] much faster, 32bit, >> 64k RAM

Re:Why xor eax, eax? by kjs3 on Monday December 01, 2025 @03:10PM (#46112505)
I dunno.

You don't know what, exactly? You can go to the web site and see what they are selling.

The 6502 has been a $2 part for a long time

I doubt that for an IP license at any volume such a thing would make sense.

but needs RAM and some glue logic

Sure? Embedded in whatever you're building.

for a similar price you can get...

Oh, sorry...my bad. You were doing it the HN way: "Don't actually read what was written for comprehension...just take your first knee jerk and tell them how you would obviously do it better.".

Re:Why xor eax, eax? by rzzzt on Monday December 01, 2025 @02:12PM (#46111668)
There are uC versions like the W65C134S: https://www.westerndesigncenter.com/wdc/w65c134s-chip.php
Re:Why xor eax, eax? by brucehoult on Monday December 01, 2025 @08:14PM (#46116059)
65C02s are $8 now. That didn't stop me buying one when I was stuck at home during COVID. And a 6809 too.

But forget AVR. Yeah, for a buck or so the ATTiny85 was my go-to small MCU five years ago, and the $5 328 for bigger tasks.

But for the last three years both can be replaced by a 48 MHz 32 bit RISC-V CH32V003 for $0.10 for the 8 pin package (like ATTiny85, and also no external components needed) and $0.20 for the 20 pin package with basically the same number of GPIOs as the 328. At 2k RAM and 16K flash it's the same RAM and a little less flash than the ATMega328 -- but not as much as you'd think as RISC-V handles 16 and 32 bit values and pointers sooo much better.

And now you have the CH32V002/4/5/6 with enhanced CPU and more RAM and/or flash -- up to 8K rAM and 62K flash on the 006 -- and still for around the $0.10-$0.20 price

https://www.lcsc.com/product-detail/C42431288.html

Re:Why xor eax, eax? by OhMeadhbh on Thursday December 04, 2025 @06:07PM (#46154482)
Hi Bruce! If you make it back to the states we'll have to drink a beer and wax poetic about the 6809. Do you know if anyone ever implement the embedded RISC-V profile in hardware? Not everything I do on small systems needs a 48MHz 32-bit. But if I could get away with a low I/O count, why not use the $0.10 part? Also pretty sure I saw 8051 based SoCs going for $2. I bet if you looked hard enough you could find something like a 6502 for about the same price.

There's probably no reason not to get some of the CH32VXXX's to play with. Every now and again I have an application that needs very low power and I'm happy to spring for an MSP430. But every time I buy an MSP430, TI EoLs the specific model I bought.

Re:Why xor eax, eax? by brucehoult on Saturday December 06, 2025 @04:06AM (#46171817)
Heeey, how's the Cruz treating you? If it still is.

I don't know why you'd ever want to pay a cent more for a 6502 or 8051 or AVR than for a RISC-V or ARM (e.g. Puya PY32F002A). Especially when the CH32V002/4/6 run on anything from 2V to 5V (plus a margin) which is pretty rare, and they don't need any external components.

I don't know whether the M6809 designers were the first to ever analyse a body of real software to find instruction and addressing mode frequencies and the distribution of immediates in order to optimise the encoding of a new ISA -- in a way that the 8086 people clearly didn't [1], but I think they were the first to publish about it, and I was fascinated by their BYTE articles at the time.

MSP430 is also a fun ISA. I just wish they were cheaper, and the cheap ones has more than 512 bytes of RAM. FRAM is funky. I also loooove the instruction encoding e.g. `add.w r10,r11` is `0x5B0A` where `5` is `add`, `B` is src register, `0` means reg to reg word size, `A` is dst register. Just beautiful. Far nicer for emulating on a 6502 or z80 than Arm or RISC-V too. The R2/R3 const generation is a bit whack though.

[1] e.g. on one hand deciding it was worth squeezing a 5 bit offset from any of 4 registers into a 2-byte instruction, while also providing 8 and 16 bit offsets with 3 and 4 byte instructions. They were also confident enough to relegate the 6800's SEC/CLC/SEI/CLI/SEV/CLV to two-byte instructions (with a mask so you could do multiple at once). But not confident enough to do the same with DAA, or SEX. They kept the M6800 encoding for DAA (and for as much else as possible e.g. keeping the opcodes for indexed addressing, but expanding from one option to dozens), but SEX was new to them and they could have experimented with it.

Re:Why xor eax, eax? by ASalazarMX on Monday December 01, 2025 @02:15PM (#46111719)
Reading cryptography was that advanced at that time, I'm even more surprised that the venerable Norton Utilities for MS-DOS required a password, that was simply XORed with some constant and embedded in the executables. If the reserved space was zeroes, it considered it a fresh install and demanded a new password.

If it had been properly encrypted my young cracker self would have had no opportunity.

Re:Why xor eax, eax? by Sesse__ on Tuesday December 02, 2025 @09:52AM (#46121778)
Well, running in CTR mode is really common now, and that ends up XORing the generated keystream into the plaintext… (CTR mode is essentially converting block ciphers into stream ciphers, if you want to see it that way.)
Re:Why xor eax, eax? by stevefan1999 on Tuesday December 02, 2025 @08:01PM (#46129030)
Self-correction: It is GF(2^8) and not GF(2), but GF(2^8) primitive operations (such as carryless multiplication) can be reduced into a bunch of table lookups and/or GF(2) operations, which is how to AES crypto accelerators are being done in hardware.
Re:Why xor eax, eax? by vanderZwan on Monday December 01, 2025 @09:38AM (#46107941)
Hah, we commented on the exact same paragraph within a minute of each other! My memory agrees with your memory, although I think that should be 3E 00. Let me look that up:

https://jnz.dk/z80/ld_r_n.html

https://jnz.dk/z80/xor_r.html

Yep, if I'm reading this right that's 3E 00, since the second byte is the immediate value.

One difference between XOR and LD is that LD A, 0 does not affect flags, which sometimes mattered.

Re:Why xor eax, eax? by jgrahamc on Monday December 01, 2025 @09:52AM (#46108139)
You're right. Of course, it's 3E 00. Not sure how I remembered 3E 01. My only excuse is that it was 40 years ago!
Re:Why xor eax, eax? by sfink on Monday December 01, 2025 @03:39PM (#46112881)
What is this "LD A, 0" syntax? Is it a z80 thing?

One of the random things burned into my memory for 6502 assembly is that LDA is $A9. I never separated the instruction from the register; it's not like they were general purpose. But that might be because I learned programming from the 2 books that came with my C64, a BASIC manual and a machine code reference manual, and that's how they did it.

I learned assembly programming by reading through the list of supported instructions. That, and typing in games from Compute's Gazette and manually disassembling the DATA instructions to understand how they worked. Oh, and the zero-page reference.

Good times.

Re:Why xor eax, eax? by Narishma on Monday December 01, 2025 @05:42PM (#46114417)
> One of the random things burned into my memory for 6502 assembly is that LDA is $A9. I never separated the instruction from the register; it's not like they were general purpose.

You had LDA and LDX and LDY as separate instructions while the Z80 assembler had a single LD instruction with different operands. It's the same thing really.

Re:Why xor eax, eax? by sfink on Monday December 01, 2025 @07:11PM (#46115474)
Right, though the LD? and ST? instructions were kind of exceptions. You could only do arithmetic and stack and bitwise ops (and, or, eor, shift, rotate) with A, never X nor Y. Increment and decrement were X/Y only. You couldn't even add two registers together without stashing one in memory.
Re:Why xor eax, eax? by vanderZwan on Tuesday December 02, 2025 @03:56AM (#46119104)
> What is this "LD A, 0" syntax? Is it a z80 thing?

Well, I never wrote any 6502 so I can't compare, but yes, you could load immediate values into any register except the flag register on the Z80. Was that not a thing on the 6502?

Re:Why xor eax, eax? by jgrahamc on Wednesday December 03, 2025 @02:15PM (#46138667)
The 6502 instruction set was really limited but there were three registers: A, X, Y and there were immediate load instructions for each: LDA #0, LDX #0, LDY #0.
Re:Why xor eax, eax? by jgrahamc on Wednesday December 03, 2025 @02:18PM (#46138710)
What is this "LD A, 0" syntax? Is it a z80 thing?

On the 6502 you had three instructions LDA, LDX, LDY where the register name is essentially part of the instruction name. On the Z80 you had a lot of "load" instruction so you had LD and then many different operands: loading 8-bit registers, loading 16-bit, writing to memory, reading from memory, reading/writing from memory using a register as an index. So, made more sense on Z80 to have "LD" whereas LDA/LDX/LDY worked fine on 6502.

Re:Why xor eax, eax? by anonzzzies on Monday December 01, 2025 @10:26AM (#46108538)
3E 00 : I was on MSX and never had an assembler when you so I only remember the Hex, never actually knew the instructions; I wrote programs/games by data 3E,00,CD,etc without comments saying LD A as I never knew those at the time.
Re:Why xor eax, eax? by unnah on Monday December 01, 2025 @11:37AM (#46109461)
Umm... how did you manage to learn those hex codes? You just read a lot of machine code and it started to make sense?
Re:Why xor eax, eax? by jgrahamc on Monday December 01, 2025 @11:40AM (#46109509)
I started out writing machine code without an assembler and so had to hand assemble a lot of stuff. After a while you end up just knowing the common codes and can write your program directly. This was also useful because it was possible to write or modify programs directly through an interface sometimes called a "front panel" where you could change individual bytes in memory.

Back in 1985 I did some hand-coding like this because I didn't have access to an assembler: https://blog.jgc.org/2013/04/how-i-coded-in-1985.html and I typed the whole program in through the keypad.

Re:Why xor eax, eax? by stevekemp on Monday December 01, 2025 @12:16PM (#46110022)
Same here. On/For the ZX Spectrum, looking up the hex-codes in the back of the orange book. At least it was spiral-bound to make it easier.

Later still I'd be patching binaries to ensure their serial-checks passed, on Intel.

Re:Why xor eax, eax? by kragen on Monday December 01, 2025 @11:55AM (#46109730)
The instruction sets were a lot simpler at the time. The 8080 instruction set listing is only a few pages, and some of that is instructions you rarely use like RRC and DAA. The operand fields are always in the same place. My own summary of the instruction set is at https://dercuano.github.io/notes/8080-opcode-map.html#addtoc....
Re:Why xor eax, eax? by amirhirsch on Monday December 01, 2025 @12:21PM (#46110091)
I implemented a PDP-11 in 2007-10 and I can still read PDP-11 Octal
Re:Why xor eax, eax? by af78 on Monday December 01, 2025 @01:22PM (#46110922)
I had a similar experience of writing machine code for Z80-based computers (Amstrad CPC) in the 90's, as a teenager. I didn't have an assembler so I manually converted mnemonics to hex. I still remember a few opcodes: CD for CALL, C9 for RET, 01 for LD BC, 21 for LD HL... Needless to say, the process was tedious and error-prone. Calculating relative jumps was a pain. So was keeping track of offsets and addresses of variables and jump targets. I tended to insert nops to avoid having to recalculate everything in case I needed to modify some code... I can't say I miss these times.

I'm quite sure none of my friends knew any CPU opcode; however, people usually remembered a few phone numbers.

Re:Why xor eax, eax? by senderista on Monday December 01, 2025 @02:11PM (#46111648)
It wasn't unusual in the 80s to type in machine code listings to a PC; I remember doing this as an 8-year-old from magazines, but I didn't understand any of the stuff I was typing in.
Re:Why xor eax, eax? by anonzzzies on Monday December 01, 2025 @03:59PM (#46113143)
Typing from mags, getting interested in how the magic works by learning to use a hex monitor and trying out things. I was a kid so time enough.
Re:Why xor eax, eax? by mmphosis on Monday December 01, 2025 @03:26PM (#46112719)
Try to keep the value 0 in the Y register.

  echo tya|asm|mondump -r|6502
                                A=AA X=00 Y=00 S=00 P=22 PC=0300  0
  0300- 98        TYA           A=00 X=00 Y=00 S=00 P=22 PC=0301  2
Re:Why xor eax, eax? by brucehoult on Monday December 01, 2025 @08:18PM (#46116086)
That's 1 byte smaller than `LDA #0`, but not faster. And you don't have enough registers to waste them -- being able to do `STZ` and the `(zp)` addressing mode without having to keep 0 in Z or Y were small but soooo convenient things in the 65C02.
Re:Why xor eax, eax? by snvzz on Tuesday December 02, 2025 @01:38AM (#46118304)
You might like the PC Engine, a game console based on the 65C02*.

*Actually a custom chip also containing some peripherals.

Re:Why xor eax, eax? by favorited on Monday December 01, 2025 @04:23PM (#46113427)
"Prefer `xor a` instead of `ld a, 0`" is basically the first optimization that you learn when doing SM83 assembly.

https://github.com/pret/pokecrystal/wiki/Optimizing-assembly...

Re:Why xor eax, eax? by user3939382 on Monday December 01, 2025 @08:58PM (#46116438)
I’m building a new 6502 machine