Follow

6502, ai 

I was playing with a 6502 genetic programmer and as a test I asked it to make me a program that does Collatz iteration on A register (x -> x/2 if x is even, 3x+1 if x is odd), and it came up with this cutie:

```
* = 8000
8000 85 00 STA $00
8002 6A ROR A
8003 90 03 BCC $8008
8005 65 00 ADC $00
8007 0A ASL A
8008 60 RTS
8009 .END
```

explanation re: 6502, ai 

- put the value of A in memory (at address $00)
- rotate A right (ie divide by 2 and if doesn't divide evenly set the carry flag)
- if carry flag set, jump to the end (we're done)
- add the value from memory (original value of A) to A, with +1 due to carry (which is set); A is now original_A + A/2-1/2 (because we're rounding down) + 1 = original_A + A/2 + 1/2
- shift A left (ie multiply by 2); A is now 3 * original_A + 1
- return

Show thread

re: explanation re: 6502, ai 

correction: if carry flag NOT set, jump to the end

Show thread

re: 6502, ai 

I'm running an experiment to see if it can evolve an 8-bit * 8-bit = 16-bit multiplier. It seems to have evolved a solution that gets the least significant byte right at least. I'll run it for a while (I could code multithreading but I have other things to do atm).

Show thread

re: 6502, ai 

Looks like its (partial) solution is "let me add X number Y times"

Show thread

re: 6502, ai 

pastebin.com/3EGbMnMU if you're interested in the galaxy brain solution (arguments in $00 and $01, result in $02,$03 little-endian)

Show thread

re: 6502, ai 

@kiilas are you using an emulator (or a 65C02) that ties undefined opcodes to NOP, or a real 6502? because some of the undefined opcodes scattered around there will have very different effects on a real 6502... including a couple of instances of $02, which will kill the processor:

l8000:
. ldx 1,y
. sax $61B
. adc ($71,x)
. eor $D0CA
. sbc $285,y
. adc ($71,x)
. eor $D0CA
. asl $285,y
. lsr $8A,x
. dec $ABAE,x
. and $9C
. lsr a
. tas $BF58.y
. nop ; $DA
. arr #$71
l8023:
. lax ($1B,x)
. isc $4A6A,y
. adc ($19),y
. kil
. nop
. slo $6ACB,y
. beq l804A
. stx $D101
. eor $A059,y
. bit $C1
. dcp ($36),y
. ora ($36,x)
. slo ($90,x)
. nop
l8040:
. sbc #$F4
. lda #$5C
. eor $F459,y
. arr #3
. db $1C ; → nop $19CD,x; kil
l804A:
. cmp $219
. rol $01,x
. eor #$45
. pha
. rts

(opcodes as suggested by problemkaputt.de/pagezero.htm# )

re: 6502, ai 

@millihertz I'm using this emulation library github.com/redcode/6502.

I know the invalid opcodes will cause undefined behaviour/crash, but for the sake of simplicity (I don't intend to use the generated code anywhere... yet :blobtongue:​ ) I'm treating them as NOPs (or whatever the emulator treats them as). I suppose I can treat it like I currently treat PC ending at 0000 ie give a ginormous penalty to the program, and maybe I'll do that next.

re: 6502, ai 

@millihertz looks like the problem solved itself after running it overnight (the programs are rewarded for shortest execution time)!

* = 8000
8000 A6 01 LDX $01
8002 18 CLC
8003 65 00 ADC $00
8005 CA DEX
8006 D0 FA BNE $8002
8008 85 02 STA $02
800A 60 RTS

Sign in to participate in the conversation
Computer Fairies

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!