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?
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%
Quote from: akj on 17:36, 29 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:
.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
@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.
So, another question: what is it useful for?
@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:
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.
I didn't say it *has* to be useful, just asking :D
Quote from: akj on 22:30, 12 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:
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
Sure, but what info, exactly? :)
Quote from: Gryzor on 07:08, 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.
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.