
X86-to-6502: translate x86 assembly into mos6502 assembly - ingve
https://github.com/lefticus/x86-to-6502
======
KSS42
Can you explain the example?

// test.asm main: movb $1, 53280 xorl %eax, %eax ret

And get this output:

main: ldy #$1 sty 53280 lda $00 rts

I can see "movb $1, 53280" getting mapped to: ldy #$1 sty 53280

but what is going on with the xorl and lda?

~~~
tux1968
xor'ing a register against itself is an idiomatic way to clear the register to
0. Could do the same thing in 6502, but loading with zero directly is
functionally equivalent.

~~~
KSS42
Thanks for the explanation.

In the x86 code, is there an advantage of using the xor instead of a load/mov?

Is it to avoid a fetch?

(I programmed 6502/6510 assembly as a kid, but have no experience with x86
assembly)

~~~
alblue
The encoding of the machine instruction is 2 bytes for xor eax,eax - if you
were doing a load/mov then you'd have to use a numeric cknstant which would be
4 bytes to represent zero and then more for the mov itself.

In addition when you have dependent loads of registers between instructions
you can end up with pipeline stalls that delay the instruction (even if it
shouldn't have any logical effect).

The xor pattern is so common that inside the processor (which translates isa
instructions to microcode) recognise it explicitly and so it's treated as a
special case. In fact no xor happens and the register is simply reprinted to a
fresh value containing zero.

~~~
13of40
I've always wondered why a CISC architecture like x86 didn't include a CLR
instruction...

~~~
Annatar
On some processor families, like for instance the MC68000 family of
processors, the clr instruction always wastes at least one clock cycle reading
the register being cleared before actually clearing it, which is why when you
look at the assembler code for that processor family, you will rarely see the
clr instruction being used:

[http://www.easy68k.com/paulrsm/doc/trick68k.htm](http://www.easy68k.com/paulrsm/doc/trick68k.htm)

common tricks to clear out a data register on the 68000 family in a performant
way include:

    
    
      moveq #0, d0 ; this works because zero can be expressed with only seven bits
      eor.l d0, d0
      sub.l d0, d0
    

and for the address register

    
    
      sub.l a0, a0
      eor.l a0, a0

