1
votes

I have a number 1f stored at address 00, and I have another number 1e stored at address 01. Now I want to make 1f into 1f00 and 1e into 001e, then add them up to get 1f1e. How do I do this? I know the instructions asl and lsr deal with this, but I'm a 6502 beginner, so I would really appreciate it someone can show me how it's done efficiently(least amount of cycles).

2
Since 6502 is a 8 bit machine, you can't work with 16 bit numbers directly. You can just manipulate the two halves separately, no shifts needed.Jester
@Jester Do you mean that if I want to store something into 1f1e I need to use sta ($00),y?Meteorite
Yes, but remember that 6502 is little endian, so to get 1f1e you need to swap the two bytes. You can of course also use sta ($00, x) assuming x is zero.Jester
@Jester I'd promote that to an answer if I were you; it clearly answers the question and will be more prominent to people who find this question in the future. Failing that I guess somebody else could post it as community wiki.Tommy

2 Answers

2
votes

If you are working with 16-bit quantities, you need to make sure your variables are 16 bits, or 2 bytes wide.

You probably want to use the 6502 little-endian order, especially if your variable could be used as an address. That means low-byte, high-byte order.

I have a number 1f stored at address 00, and I have another number 1e stored at address 01.

You really want to have $001F stored at address $00-$01 ($00 will equal #$1F and $01 will equal #$00), and $001E stored at address $02-$03 ($02 will equal #$1E and $03 will equal #$00). Then you probably want another variable to hold the result, let's say $04-$05.


But, if you really have two 8-bit variables and you want to "convert" them to 16-bit variables ...

Assume (using labels for clarity):

$00 - your first 8-bit variable (label VAR_A_8)

$01 - your second 8-bit variable (label VAR_B_8)

$02-$03 - where you want to place your first converted 8-bit variable (label VAR_A_16)

$04-$05 - where you want to place your second converted 8-bit variable (label VAR_B_16)

Are they unsigned? If so, just LDA #$00, STA VAR_A_16, STA VAR_B_16, LDA $00, >STA VAR_A_16, LDA $01, STA >VAR_B_16.

The > is a common assembler syntax to mean "low byte of" (It might actually be < - I always got confused on that)

Are they signed? If the 8-bit values are negative (bit 7 set), you want to store $FF in the new bytes, otherwise you want to store $00.

One way:

               LDX #$00
               LDA VAR_A_8       ;Loading .A will set N flag in .P if bit 7 is set
               BPL VAR_A_NOT_NEG
               DEX               ;.X will now be $FF
VAR_A_NOT_NEG: STX <VAR_A_16     ;store .X in high byte
               STA >VAR_A_16     ;copy low byte
               LDX #$00
               LDA VAR_B_8
               BPL VAR_B_NOT_NEG
               DEX
VAR_B_NOT_NEG: STX <VAR_B_16
               STA >VAR_B_16
1
votes

The 6502 is an 8-bit machine; it constructs 16-bit quantities by reading the least significant byte from the lower address (i.e. it is little endian).

You therefore just need to put the 1e into an address before 1f — by switching them around versus your current layout, by moving 1f to address 02 or by some other means. No shifting or ORing is necessary; indeed there is no 16-bit register inside the 6502 that can be shifted or operated upon with bitwise logic.

If the two bytes are kept at 00 and 01 but reversed then you can e.g. zero out x and use the ($00, x) addressing mode to access 1f1e.