1) if it is only one parameter it gets stored in accumulator (A) if byte and A/X if word

2) no need to push the stack the value back if only returns one parameter ( not sure?)

3) if more than one parameter its is necessary to pop the stack for x-1 where x ins the number of parameters. and later reconstruct the stack(not sure how to do this?)

here is my first try:

Code: Select all

```
.export _atan2
_atan2:
ldy #3
lda (sp),y
sta <SCRX ;x1
dey
lda (sp),y
sta <SRC ;x2
dey
lda (sp),y ;y1
sta <SCRY
dey
lda (sp),y ;y2
sta <DST
lda <SCRX
sbc <SRC
bcs *+4
eor #$ff
tax
rol octant
lda <SCRY
sbc <DST
bcs *+4
eor #$ff
tay
rol octant
lda log2_tab,x
sbc log2_tab,y
bcc *+4
eor #$ff
tax
lda octant
rol
and #%111
tay
lda atan_tab,x
eor octant_adjust,y
jsr incsp2 ;my tentative to restore sp, only 2 because it is 4 bytes? one for each byte i guess :P
rts
```

and this is how i try to use on the C side:

Code: Select all

```
byte __fastcall__ atan2(byte x1, byte x2, byte y1, byte y2);
(...)
byte angle;
angle = atan2(10,30,20,40); //just some mock to test everything doesn't explode
```

sorry for the n00B questions but if some one can direct to some kind of guide on this i appreciate