7 ms·
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 ro
by jgrahamc 11mo ago
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
- vanderZwan 11mo agoHah, 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/ld_r_n.html https://jnz.dk/z80/xor_r.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.
- jgrahamc 11mo agoYou'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!
- sfink 11mo agoWhat 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.
- Narishma 11mo ago> 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.
- sfink 11mo agoRight, 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.
- vanderZwan 11mo ago> 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?
- jgrahamc 10mo agoThe 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.
- jgrahamc 10mo agoWhat 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.
- wavemode 11mo ago> 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)
- jgrahamc 11mo agoHe was right though. We were memory and cycle constrained and I'd wasted both!
- xigoi 11mo agoThe real joke is that a CEO had actual technical knowledge instead of just being there for decoration.
- mkornaukhov 11mo agoSimilarly, the CEO couldn't resist the outstanding optimization of memory and execution speed!
- 6510 11mo ago[flagged]
- jgrahamc 11mo agoI am sad you don't believe this story. The CEO was very technical and this is exactly the sort of thing he would spot.
- bombcar 11mo agoPeople 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).
- 11mo ago
- anonzzzies 11mo ago3E 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.
- unnah 11mo agoUmm... how did you manage to learn those hex codes? You just read a lot of machine code and it started to make sense?
- jgrahamc 11mo agoI 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 https://blog.jgc.org/2013/04/how-i-coded-in-1985.html and I typed the whole program in through the keypad.
- stevekemp 11mo agoSame 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.
- kragen 11mo agoThe 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_2 https://dercuano.github.io/notes/8080-opcode-map.html#addtoc....
- stevefan1999 11mo ago> 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).
- OhMeadhbh 11mo agoI 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.
- kjs3 11mo agoI'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.
- PaulHoule 11mo agoI 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
- rzzzt 11mo agoThere are uC versions like the W65C134S: https://www.westerndesigncenter.com/wdc/w65c134s-chip.php https://www.westerndesigncenter.com/wdc/w65c134s-chip.php
- mmphosis 11mo agoTry 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
- brucehoult 11mo agoThat'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.
- snvzz 11mo agoYou might like the PC Engine, a game console based on the 65C02*. *Actually a custom chip also containing some peripherals.
- favorited 11mo ago"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-code#set-a-to-0 https://github.com/pret/pokecrystal/wiki/Optimizing-assembly...
- user3939382 11mo agoI’m building a new 6502 machine