Stupid Question Alert

Anything you like and probably not related to Bard's Tale
Post Reply
User avatar
Darendor
Posts: 1502
Joined: Wed Jan 14, 2009 1:53 am
Location: Red Deer, Alberta, Canada

Stupid Question Alert

Post 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.
Maven
Posts: 137
Joined: Sat Apr 16, 2011 9:39 pm

Re: Stupid Question Alert

Post 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.
User avatar
Darendor
Posts: 1502
Joined: Wed Jan 14, 2009 1:53 am
Location: Red Deer, Alberta, Canada

Re: Stupid Question Alert

Post by Darendor »

So then, EOR A would turn the A register to zero, is that correct?
Maven
Posts: 137
Joined: Sat Apr 16, 2011 9:39 pm

Re: Stupid Question Alert

Post by Maven »

Well, theoretically.
Except it looks like on the 6502 there is no mode that lets you do EOR with the accumulator. :(
User avatar
Darendor
Posts: 1502
Joined: Wed Jan 14, 2009 1:53 am
Location: Red Deer, Alberta, Canada

Re: Stupid Question Alert

Post 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. :?
drifting
Posts: 152
Joined: Wed Dec 07, 2011 10:21 pm

Re: Stupid Question Alert

Post 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
Weber G
Posts: 125
Joined: Tue Dec 15, 2020 9:58 am

Re: Stupid Question Alert

Post 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?
User avatar
Darendor
Posts: 1502
Joined: Wed Jan 14, 2009 1:53 am
Location: Red Deer, Alberta, Canada

Re: Stupid Question Alert

Post 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.

:?
Post Reply