6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri Nov 15, 2024 3:18 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Wed Sep 03, 2003 7:53 pm 
Offline

Joined: Wed Sep 03, 2003 7:41 pm
Posts: 4
I'm trying to learn more about how the 6502 processor works but am stuck on how subtraction is done.

Here is my understanding of addition:

---------------------------------------------------------------------

[Addition with 2 bytes]

First number:
310 decimal / $136 / 0000 0001 0011 0110
$03A0 = 0011 0110 - low byte
$03A1 = 0000 0001 - hight byte

Second number:
525 deciaml / $20D / 0000 0010 0000 1101
$03B0 = 0000 1101 - low byte
$03B1 = 0000 0010 - byte

Program:
CLC ;Clear carry flag
LDA $03A0 ;Load low byte of first number into accumulator
ADC $03B0 ;Add low byte of second number to low byte of first number with carry
STA $03C0 ;Store result in memory
LDA $03A1 ;Load high byte of second number into accumulator
ADC $03B1 ;Add high byte of second number to high byte of first number with carry
STA $03C1 ;Store final result in memory

Result:
$03C0 = 0100 0011 - Low byte
$03C1 = 0000 0011 - High byte

[How it is done]
__111 1 Add with carry
_0011 0110 ($03A0 Location) - First number low byte
+0000 1101 ($03B0 Location) - Second number low byte
--------------
_0100 0011 ($03C0 Location) - Low byte result

_0000 0001 ($03A1 Location) - First number high byte
+0000 0010 ($03B1 Location) - Second number high byte
--------------
_0000 0011 ($03C1 Location) - High byte result

Result:
835 decimal / $343 / 0000 0011 0100 0011

---------------------------------------------------------------------

I know the carry flag is set before subraction but I can't see why. So can someone explain how subraction is done with the 6502. Perhaps using an example like the one above.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Sep 03, 2003 10:56 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 9:02 pm
Posts: 1747
Location: Sacramento, CA
John,

I'll do my best to explain it as I understand it. My examples will use a 4 bit number for simplicity.

In order to do a subtraction in hardware, its usually easier to take the two's compliment of the second number and then add the two numbers together. In that way, you only need a hardware adder to do both addition and subtraction.

Now, to take the two's compliment of a number, you invert the bits and then add 1.

Example:
0101 = 5
1010 = -6 (invert the bits, or one's compliment, or EOR $FF)
1011 = -5 (add 1)

Now, instead of adding the 1 seperately, why not just set the Carry and do an ADC command, saving an extra cycle!

Here are two examples:
Code:
Example 1:

   1101 : 13
-  0110 :  6   
---------------

      1 : Carry Set
   1101 : 13
   1001 : -7 (one's comp)
-----------
  10111 :  7 with the carry bit set (no borrow)


Example 2:
   1101 : 13
-  1110 : 14 
---------------

      1 : Carry Set
   1101 : 13
   0001 : -15 (one's comp)
-----------
  01111 :  -1  with Carry Clear (borrow)


That how I understand it. I hope its clear to you.

Daryl
http://65c02.tripod.com/


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Sep 04, 2003 7:31 am 
Offline

Joined: Wed Sep 03, 2003 7:41 pm
Posts: 4
Thanks for the explanation...Now I understand.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: