Page 1 of 1

[newbie] A<-A+X operation, how to

Posted: Thu Aug 21, 2008 3:13 am
by waskol
Well, it is not easy to explain, but I am stuck with this.

Suppose that a value was previously stored in the accumulator (A) another one in the X register, for instance

Code: Select all

LDA #$03
LDX #$02
...
Here is my dumb question : :oops:
What is the best method in order to add A and X ?

I well understood that there was no "implicit" way to do it with the ADC instruction, and that I am obliged to use a temporary memory location, like this :

Code: Select all

;A<-A+X
CLC
CLD
LDA #$03
LDX #$02
STX temp  ;store X in temp memory location
ADC temp ;Add temp to A (result in A)
Thus, the exact question should be more alike this one : what address should I choose, in the Oric memory, as a temporary variable ???
I imagine that it is not well indicated to choose a zero page address, or am I wrong ?

May be I could use the stack, but how ? For example,is this correct ?

Code: Select all

;A<-A+X
CLC
CLD
LDA #$03
LDX #$02

PHA      ;push A on the stack 
TXA      ;move X to A
TSX      ;move address of the stack in X
ADC $00,X  ;use X as index in order to add what is contained on the stack to A 
          ;(hum, is this allowed ???? lol )

;what follows does not really matters to me
STA result    ;result is a memory location
PLA             ;Pop A, let's be clean
Thanks for help.

(For info, I am trying to build a simple pascal cross compiler for Oric, with Crenshaw's tutorial : http://crenshaw.books.myprojects.kostigoff.net , do not except something terrific !)

Posted: Fri Aug 22, 2008 9:18 am
by Twilighte
it needn't be so complicated at all 8)
i usually assign a temp variable to zero page then..

Code: Select all

stx temp01
clc
adc temp01
no worries for cld, any routine that uses bcd (cld/sed) always clears bcd at end, or at least should do.

if u use xa for compiling, then..

Code: Select all

 .zero
*=$00
temp01    .dsb 1

 .text
*=$blah blah
under basic, zero page addresses 0 to B, BB-BC and F3 to F9 are not used and may be used by you (the programmer).
if a program is totally machine code and doesn't use basic, then you get all 256 addresses of zero page, page 2, etc. :D

Twi

Posted: Fri Sep 12, 2008 7:34 am
by waskol
Thanks twi, i did not see your answer before !