It is currently Sun Nov 19, 2017 9:03 am

All times are UTC - 7 hours



Forum rules


Related:



Post new topic Reply to topic  [ 55 posts ]  Go to page Previous  1, 2, 3, 4
Author Message
 Post subject: Re: spca65
PostPosted: Sat Jan 04, 2014 4:43 pm 
Offline
User avatar

Joined: Sun Jan 02, 2011 11:50 am
Posts: 522
blargg wrote:
And next, how to define a global symbol within a scope...The context is using scopes in a nice loop macro


By "loop macro" do you mean you are using a .repeat? You could try recursion instead, since the .repeat is basically a form of recursion. Maybe give a more defined example.


Top
 Profile  
 
 Post subject: Re: spca65
PostPosted: Sat Jan 04, 2014 4:51 pm 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19228
Location: NE Indiana, USA (NTSC)
I wouldn't be so sure that ca65's macro processor optimizes tail calls. How deep of recursion were you thinking?


Top
 Profile  
 
 Post subject: Re: spca65
PostPosted: Sat Jan 04, 2014 5:04 pm 
Offline
User avatar

Joined: Sun Jan 02, 2011 11:50 am
Posts: 522
I'm not sure what you mean with tail calls in this context, tepples.

Example comparison: A macro called hex as recursive and with .repeat (untested)

Usage:
Code:
hex 01 03 a0 ba 23 43 fe ee fc 01 03 a0 ba 23 43 fe ee fc


Recursive:
Code:
macro hex data
    .ifblank data
        .error "Please provide some data!"
    .endif
    .byte $.left(1,data)
    .if .tcount(data) > 1
        hex .mid(1, .tcount(data) - 1, data)
    .endif
.endmacro


With .repeat
Code:
macro hex data
    .ifblank data
        .error "Please provide some data!"
    .endif
    .repeat .tcount(data), I
        .byte $.mid(I, 1, data)
    .endrepeat
.endmacro


Both are technically recursive.


Top
 Profile  
 
 Post subject: Re: spca65
PostPosted: Sat Jan 04, 2014 5:12 pm 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19228
Location: NE Indiana, USA (NTSC)
Unless ca65's macro expander handles tail calls specially, your recursive example will eat up about 18 levels of call stack inside the macro expander when hex calls itself, one for each byte. The iterative example won't.


Top
 Profile  
 
 Post subject: Re: spca65
PostPosted: Sat Jan 04, 2014 5:40 pm 
Offline
User avatar

Joined: Sun Jan 02, 2011 11:50 am
Posts: 522
Yes, I'm sure the recursive example will use more memory vs .repeat, but it shouldn't matter much on a modern PC. The benefit being recursive example will allow you to have .local constants defined that are unique for each recursive expansion and hopefully avoid using any unnamed scopes (and making a global definition no problem).


Top
 Profile  
 
 Post subject: Re: spca65
PostPosted: Sat Jan 04, 2014 5:59 pm 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19228
Location: NE Indiana, USA (NTSC)
Movax12 wrote:
Yes, I'm sure the recursive example will use more memory vs .repeat, but it shouldn't matter much on a modern PC.

Unless you have to recompile ca65 for a greater recursion depth. A lot of interpreters* cap the call stack depth to prevent runaway recursion from making your machine unresponsive from swapping. Good luck getting a recursive macro to run 32768 deep, once for each byte in a bank.

When I try to assemble this, I get an error because ca65 is capping the macro call stack at 255 levels.
Code:
.macro recursefromhell i
  .if i > 0
    .local i1
    i1 = i
    .byte <i1
    recursefromhell (i1 - 1)
  .endif
.endmacro

; this works
recursefromhell 255

; this raises Error: Too many nested macro expansions
recursefromhell 256


Quote:
The benefit being recursive example will allow you to have .local constants defined that are unique for each recursive expansion and hopefully avoid using any unnamed scopes (and making a global definition no problem).

What does this necessarily provide over .local once and then .set within the .repeat loop?


* ca65 is an assembler, but its macro language is an interpreter within an assembler.


Top
 Profile  
 
 Post subject: Re: spca65
PostPosted: Sat Jan 04, 2014 6:17 pm 
Offline
User avatar

Joined: Mon Sep 27, 2004 8:33 am
Posts: 3715
Location: Central Texas, USA
Movax12 wrote:
blargg wrote:
And next, how to define a global symbol within a scope...The context is using scopes in a nice loop macro


By "loop macro" do you mean you are using a .repeat? You could try recursion instead, since the .repeat is basically a form of recursion. Maybe give a more defined example.

No, just the equivalent of a for loop with braces for scope, that allows nesting, and doesn't require you to name each loop. I'm writing a CPU instruction tester and want to make several nested for loops to go over arrays of bytes.
Code:
loop_bytes { 0, 1, $7f, $80, $ff }, in_a
loop_bytes { 0, 1, $7f, $80, $ff }, operand
cmp operand
; A  operand
; 00 00
; 00 01
; 00 7f
; 00 80
; 00 ff
; 01 00
; 01 01
; ...
loop_end
loop_end


Top
 Profile  
 
 Post subject: Re: spca65
PostPosted: Sat Jan 04, 2014 6:27 pm 
Offline
User avatar

Joined: Sun Jan 02, 2011 11:50 am
Posts: 522
tepples wrote:
What does this necessarily provide over .local once and then .set within the .repeat loop?


I found it helpful in the instances where the recursive expansion would end and the .local would retain the value from before the expansion call. Not sure if I could come up with a good example vs .repeat


Top
 Profile  
 
 Post subject: Re: spca65
PostPosted: Mon Feb 16, 2015 8:37 pm 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19228
Location: NE Indiana, USA (NTSC)
Back to topic:

What's the license on this set of macros?

(And after how many days since last post must I calculate days since last post?)


Top
 Profile  
 
 Post subject: Re: spca65
PostPosted: Sat Jul 18, 2015 1:32 pm 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19228
Location: NE Indiana, USA (NTSC)
The uses of DEFAULT_ABS inside macros need to be changed to ::DEFAULT_ABS so that they'll work even inside a .proc block.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 55 posts ]  Go to page Previous  1, 2, 3, 4

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 6 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:  
Powered by phpBB® Forum Software © phpBB Group