Author Topic: How does it do it?  (Read 3093 times)

0 Members and 1 Guest are viewing this topic.

Offline akj

  • CPC464
  • **
  • Posts: 11
  • Liked: 0
  • Likes Given: 0
How does it do it?
« on: 19:36, 29 July 09 »
Here's a little teaser for your CPC brain cells.

The short routine below (which could be part of a longer program) will display on the screen the highest line number of the program currently in memory.  Can you work out how it does it?

Code: [Select]
10 REM Last
30 last%=0
40 CALL &BFFE-35, &C970,&2371,&166,&DD00,&6EDD,&ED18,&4623,&4E23,&23EB,&19EB,&A28
,&B37A,&2B56,&235E,&EB00,&1,&170,&1100,@last%
50 PRINT last%
Anthony Jordan

Offline Executioner

  • Supporter
  • 6128 Plus
  • *
  • Posts: 783
  • Country: au
  • WinAPE Developer
    • WinAPE
  • Liked: 392
  • Likes Given: 60
Re: How does it do it?
« Reply #1 on: 06:33, 30 July 09 »
Can you work out how it does it?

Yes, it places the parameters on the stack which are opcodes for the routine below, then executes the code on the stack:

Code: [Select]
.at_last
dw #01ff
db 0

.start
ld de,#0170
ld bc,#0000

.loop1
ex de,hl
ld e,(hl)
inc hl
ld d,(hl)
dec hl
ld a,d
or e
jr z,exit
ex de,hl
add hl,de
ex de,hl
inc hl
inc hl
ld c,(hl)
inc hl
ld b,(hl)
jr loop

.exit
ld l,(ix+#00)
ld h,(ix+#01)
ld (hl),c
inc hl
ld (hl),b
ret

Offline akj

  • CPC464
  • **
  • Posts: 11
  • Liked: 0
  • Likes Given: 0
Re: How does it do it?
« Reply #2 on: 12:04, 30 July 09 »
@Executioner:

Excellent!

Your answer is perfectly correct.Personally, I quite like this call-stack method of embedding short assembler routines within a basic program and I use it fairly often.
Anthony Jordan

Offline Gryzor

  • Administrator
  • 6128 Plus
  • *****
  • Posts: 16.891
  • Country: gr
  • CPC-Wiki maintainer
    • CPCWiki
  • Liked: 4014
  • Likes Given: 7230
Re: How does it do it?
« Reply #3 on: 21:17, 12 October 09 »
So, another question: what is it useful for?

Offline akj

  • CPC464
  • **
  • Posts: 11
  • Liked: 0
  • Likes Given: 0
Re: How does it do it?
« Reply #4 on: 00:30, 13 October 09 »
@Gryzor,

Who says it has to be useful?

Actually it is a very compact way of invoking pre-written machine code routines in a Basic program.  The execution is very fast compared with Read/Poke methods and needs no assembler on the end-user CPC.

For example, to display the version of your CPC's Basic Rom (0=CPC464  1=CPC664  2=CPC6128) just include these 3 lines:

Code: [Select]
ver%=0
CALL &BFFE-17, &C900, &3623, &7201, &66DD, &006E, &DDEB, &B915, &CD00, &0E00, @VER%
PRINT VER%

The hex values are basically just the machine code in reverse byte order.

For more details see my article in WACCI June 1994 Issue 79 page 17.

Anthony Jordan

Offline Gryzor

  • Administrator
  • 6128 Plus
  • *****
  • Posts: 16.891
  • Country: gr
  • CPC-Wiki maintainer
    • CPCWiki
  • Liked: 4014
  • Likes Given: 7230
Re: How does it do it?
« Reply #5 on: 08:51, 13 October 09 »
I didn't say it *has* to be useful, just asking :D

Offline arnoldemu

  • Supporter
  • 6128 Plus
  • *
  • Posts: 5.336
  • Country: gb
    • Unofficial Amstrad WWW Resource
  • Liked: 2280
  • Likes Given: 3478
Re: How does it do it?
« Reply #6 on: 11:59, 13 October 09 »
@Gryzor,

Who says it has to be useful?

Actually it is a very compact way of invoking pre-written machine code routines in a Basic program.  The execution is very fast compared with Read/Poke methods and needs no assembler on the end-user CPC.

For example, to display the version of your CPC's Basic Rom (0=CPC464  1=CPC664  2=CPC6128) just include these 3 lines:

Code: [Select]
ver%=0
CALL &BFFE-17, &C900, &3623, &7201, &66DD, &006E, &DDEB, &B915, &CD00, &0E00, @VER%
PRINT VER%

The hex values are basically just the machine code in reverse byte order.

For more details see my article in WACCI June 1994 Issue 79 page 17.
This is useful, but you *are* limited by the number of parameters you can pass. I think it may be 32 max.. have to check the ROM ;)

Please add this info to the sticky topic in the programming section (basic tips/tricks) because this definitely qualifies here.

http://cpcwiki.eu/forum/index.php/topic,203.0.html
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

Offline Gryzor

  • Administrator
  • 6128 Plus
  • *****
  • Posts: 16.891
  • Country: gr
  • CPC-Wiki maintainer
    • CPCWiki
  • Liked: 4014
  • Likes Given: 7230
Re: How does it do it?
« Reply #7 on: 09:08, 22 October 09 »
Sure, but what info, exactly? :)

Offline arnoldemu

  • Supporter
  • 6128 Plus
  • *
  • Posts: 5.336
  • Country: gb
    • Unofficial Amstrad WWW Resource
  • Liked: 2280
  • Likes Given: 3478
Re: How does it do it?
« Reply #8 on: 12:44, 22 October 09 »
Sure, but what info, exactly? :)
post already updated and I added some new info too. I'm thinking this needs to go into it's own wiki article soon.
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

Offline Gryzor

  • Administrator
  • 6128 Plus
  • *****
  • Posts: 16.891
  • Country: gr
  • CPC-Wiki maintainer
    • CPCWiki
  • Liked: 4014
  • Likes Given: 7230
Re: How does it do it?
« Reply #9 on: 10:18, 23 October 09 »
Most probably, yes. I think the forum should not be a replacement for knowledge that could go on the wiki. Discussion of the code, sure, but the end result should probably be on the wiki itself.