Page 1 of 1

Stupid Question Alert

Posted: Sun Jun 20, 2021 11:27 pm
by Darendor
Okay, so, I'm trying to learn some assembly language stuff here.

It's going less than swimmingly. :?

I am specifically interested in learning the EOR instruction. What is its purpose in life?

Anyone able to explain? I would be eternally hamburgers.

Re: Stupid Question Alert

Posted: Tue Aug 03, 2021 5:00 am
by Maven
Well I can tell you what I use it for most. First, if you EOR something with itself, it becomes 0. It's a quick efficient way to zero out a register. If you load 0 into a register it takes an extra immediate byte, whereas if you use a register, even if it's the same register, it's shorter in actual machine code.

Second, it's a quick way to toggle a bit. Especially if you use one byte for multiple bit flags. For example if you use the lower 3 bits of a byte to hold status flags for the numlock, caps lock, and scroll lock. Suppose you assign numlock as 4 (100b) caps lock as 2 (010b) and scroll lock as 1 (001b). If you detect that someone hit the caps lock key, you can toggle the 2 bit by EORing the status byte with 2.

You can SET the caps lock bit by ORing it with 2. You can RESET the caps lock bit by ANDing it with ~2. The EOR function makes it easy to TOGGLE the bit. You don't have to do a conditional with a jump, it's just a single instruction.

It can also be used for simple obfuscation. EOR is easily reversible. If you EOR a value with X, you can get the original value back by doing the EOR with X operation again. So if you have a data file you don't want people to be able to immediately recognize, just EOR each byte by something like 0xAA, and they won't be able to read it. You do the EOR operation again to get the original data back.

Re: Stupid Question Alert

Posted: Thu Aug 05, 2021 10:37 pm
by Darendor
So then, EOR A would turn the A register to zero, is that correct?

Re: Stupid Question Alert

Posted: Mon Aug 09, 2021 4:05 am
by Maven
Well, theoretically.
Except it looks like on the 6502 there is no mode that lets you do EOR with the accumulator. :(

Re: Stupid Question Alert

Posted: Fri Aug 13, 2021 6:19 am
by Darendor
Maven wrote: Mon Aug 09, 2021 4:05 am Well, theoretically.
Except it looks like on the 6502 there is no mode that lets you do EOR with the accumulator. :(
Well I know that EOR A is a valid instruction on the 6502 because I encounter it constantly while exploring the BTII code. :?

Re: Stupid Question Alert

Posted: Sun Aug 15, 2021 5:49 pm
by drifting
Darendor wrote: Fri Aug 13, 2021 6:19 am
Maven wrote: Mon Aug 09, 2021 4:05 am Well, theoretically.
Except it looks like on the 6502 there is no mode that lets you do EOR with the accumulator. :(
Well I know that EOR A is a valid instruction on the 6502 because I encounter it constantly while exploring the BTII code. :?
What is the opcode for EOR A in the BT2 code? According to https://www.masswerk.at/6502/6502_instruction_set.html, there is no instruction on the 6502 for EOR'ing with a register

Re: Stupid Question Alert

Posted: Mon Aug 16, 2021 4:50 pm
by Weber G
EOR or Exclusive-OR is a logical operation as OR and AND.

The C64 (6502) has the following EOR commands:

EOR #$nn (EOR A with a constant value) => op code $49
EOR $hhll (EOR A with the content of address hhll) => op code $4D
EOR $hhll,X (EOR A with the content of address hhll + X) => op code $5D
EOR $hhll,Y (EOR A with the content of address hhll + Y) => op code $59
EOR $ll (EOR A with the content of the zero page address ll) => op code $45
EOR $ll,X (EOR A with the content of th zero pare address ll + X) => op code $55
EOR ($ll,X) (EOR A with the content of the address which is stored in ll + X) => op code $41
EOR ($ll),Y (EOR A with the content of the address which is stored in ll + Y) => op code $51

So there are many possibilities to EOR something with A.

The truth table for EOR:
A B result
0 0 0
0 1 1
1 0 1
1 1 0

The EOR command always makes a bitwise comparison.

e.g.: $EA EOR with $F0:
$EA => 11101010
$F0 => 11110000
result: 00011010 => $1A

EOR is manily used for protection code or to create a checksum, because you can EOR back the EORed and then you get the same result (that's different to the logical operation OR and AND):

$EA => 11101010
$1A => 00011010
result: 11110000 => $F0

Hope this was understandable and helps a bit?

Re: Stupid Question Alert

Posted: Thu Aug 19, 2021 12:18 pm
by Darendor
drifting wrote: Sun Aug 15, 2021 5:49 pm
Darendor wrote: Fri Aug 13, 2021 6:19 am
Maven wrote: Mon Aug 09, 2021 4:05 am Well, theoretically.
Except it looks like on the 6502 there is no mode that lets you do EOR with the accumulator. :(
Well I know that EOR A is a valid instruction on the 6502 because I encounter it constantly while exploring the BTII code. :?
What is the opcode for EOR A in the BT2 code? According to https://www.masswerk.at/6502/6502_instruction_set.html, there is no instruction on the 6502 for EOR'ing with a register
Well...

I had thought I had seen it.

But then, me being wrong about something regarding ML is as unlikely as the sun being hot - not very.

:?