carlsson Posted July 30, 2020 Share Posted July 30, 2020 (edited) I thought I had grokked most of how VARPTR works, but apparently I'm still lacking. The manual isn't super clear, or maybe I missed some tutorial examples. Oscar's great book doesn't seem to touch it neither, perhaps a topic for an upcoming volume 2 with advanced tricks. Basically, I have the following code that works as expected: lvl=1 #curlvl = levels(lvl-1) FOR i=1 TO PEEK(#curlvl):PRINT AT PEEK(#curlvl+i)*20+7,"* *":NEXT dummy: GOTO dummy levels: DATA VARPTR levell1(0), VARPTR level2(0), VARPTR level3(0) level1: DATA 3,2,5,8 level2: DATA 4,2,4,6,8 level3: DATA 5,1,3,5,7,9 Which syntax should I use if I want to replace PEEK with variable references? I tried various variants of #curlvl(0) but it caused the compiler to choke. I also tried (#curlvl)(0) etc. What irks me is that I have a feeling I've managed this before, in part or full. Most probably I've skipped some obvious syntax, and while PEEK is perfectly fine to use at the moment, I imagined it could be done in a prettier way. Edited July 30, 2020 by carlsson 2 Quote Link to comment Share on other sites More sharing options...
+cmadruga Posted July 30, 2020 Share Posted July 30, 2020 I'd be interested in knowing as well, as another disciple of VARPTR. That volume 2 would be an instant buy if it ever comes out. Quote Link to comment Share on other sites More sharing options...
Zendocon Posted July 30, 2020 Share Posted July 30, 2020 With the newer versions of IntyBASIC, a second volume will probably be in order at some point. Obviously it will have more sample programs to work with. But I'd like to make some suggestions from reading the first volume. - See if an Index can be included. I created a Reference file myself for those times I need to look something up in the book. - I had to look elsewhere to figure out how USR and CALL worked. Once I knew, I put them both to good use. - Yes, more talk about VARPTR. - Maybe a little more discussion about JLP's flash capabilities, especially with regard to Wear Leveling. The sample FLASH program only wrote to the first row in a single sector. - There was a thread @intvnut created years ago about reading the ECS keyboard and Music Synthesizer. It would be good to have sample code for that. I know I'm forgetting something, but I'll remember at some point. 1 Quote Link to comment Share on other sites More sharing options...
carlsson Posted July 30, 2020 Author Share Posted July 30, 2020 I saw that Oscar in a previous thread used PEEK too, so perhaps that currently is the only way to do what I'm trying to do above. In that case, I'd be satisfied for the moment, just thought there might be some clever syntax I had missed out on. Quote Link to comment Share on other sites More sharing options...
Zendocon Posted July 30, 2020 Share Posted July 30, 2020 Actually, when I wrote my first iteration of Hunt The Wumpus for the 2018 contest, I used something like this: Maps: DATA 1, 2, 3 DATA 4, 5, 6 ... Each line of data contained the room numbers for the three exits from the current room. When fetching the adjacent rooms, I had something like this: Maps(RoomID*3) Maps(RoomID*3+1) Maps(RoomID*3+2) As you can guess, RoomID was the number of the room, range 0-19. I used RoomID+1 to display the current room number. All I know is it worked. Quote Link to comment Share on other sites More sharing options...
intvnut Posted July 30, 2020 Share Posted July 30, 2020 9 hours ago, carlsson said: levels: DATA VARPTR levell1(0), VARPTR level2(0), VARPTR level3(0) level1: DATA 3,2,5,8 level2: DATA 4,2,4,6,8 level3: DATA 5,1,3,5,7,9 You could probably do something like this: levels: DATA VARPTR level1(0) - VARPTR levels(0), VARPTR level2(0) - VARPTR levels(0), VARPTR level3(0) - VARPTR levels(0) level1: DATA 3,2,5,8 level2: DATA 4,2,4,6,8 level3: DATA 5,1,3,5,7,9 And then do this: #curlvl = levels(lvl-1) FOR i=1 TO levels(#curlvl):PRINT AT levels(#curlvl+i)*20+7,"* *":NEXT I'm not sure it buys you anything, and it may generate worse code. I suppose you could try it and see. Quote Link to comment Share on other sites More sharing options...
carlsson Posted July 30, 2020 Author Share Posted July 30, 2020 (edited) It seems the compiler ignores the subtraction: levels: DATA VARPTR level1(0) - VARPTR levels(0), VARPTR level2(0) - VARPTR levels(0), VARPTR level3(0) - VARPTR levels(0) generates the same code as I already had: label_LEVELS: DECLE label_LEVEL1 DECLE label_LEVEL2 DECLE label_LEVEL3 I understand that you're suggesting I would store the relative index from the beginning of the array, similar to how the first digit in each level says how many data points it has. It would be nice and dynamic, but currently I don't know how to compile this properly. As for efficiency, the compiler indeed generates more code with your suggestion. Original: MVII #1,R0 MVO R0,var_I T2: MVI var_&CURLVL,R1 ADD var_I,R1 MVI@ R1,R0 ; From here it is identical MULT R0,R4,20 ADDI #519,R0 MVO R0,_screen MOVR R0,R4 MVII #80,R0 XOR _color,R0 MVO@ R0,R4 XORI #80,R0 MVO@ R0,R4 MVO@ R0,R4 MVO@ R0,R4 MVO@ R0,R4 XORI #80,R0 MVO@ R0,R4 MVO R4,_screen MVI var_I,R0 INCR R0 MVO R0,var_I ; Different code MVI var_&CURLVL,R1 CMP@ R1,R0 BLE T2 Alternative indexing: MVII #1,R0 MVO R0,var_I T3: MVII #label_LEVELS,R1 MVI var_&CURLVL,R2 ADD var_I,R2 ADDR R2,R1 MVI@ R1,R0 ; Identical code MULT R0,R4,20 ADDI #519,R0 MVO R0,_screen MOVR R0,R4 MVII #80,R0 XOR _color,R0 MVO@ R0,R4 XORI #80,R0 MVO@ R0,R4 MVO@ R0,R4 MVO@ R0,R4 MVO@ R0,R4 XORI #80,R0 MVO@ R0,R4 MVO R4,_screen MVI var_I,R0 INCR R0 MVO R0,var_I ; Different code MVII #label_LEVELS,R3 ADD var_&CURLVL,R3 CMP@ R3,R0 BLE T3 Edited July 30, 2020 by carlsson Quote Link to comment Share on other sites More sharing options...
carlsson Posted July 30, 2020 Author Share Posted July 30, 2020 1 hour ago, Zendocon said: Maps(RoomID*3) Maps(RoomID*3+1) Maps(RoomID*3+2) Yes, if each level had a fixed number of data points it would be much easier. Another way around it is to define each level with maximum number of data points, and use dummy zeroes or something for unused entries, and in the loop skip those that are unused. But the general use to have an array of pointers to data, and then be able to dereference an indexed pointer and further index into its data array can have additional use. Quote Link to comment Share on other sites More sharing options...
carlsson Posted July 30, 2020 Author Share Posted July 30, 2020 (edited) Ok, I think I found an answer that does what I want without PEEK: lvl=1 #curlvl=levels(lvl-1) - VARPTR levels(0) FOR i=1 TO levels(#curlvl):PRINT AT levels(#curlvl+i)*20+7,"* *":NEXT dummy: GOTO dummy levels: DATA VARPTR lvl1(0), VARPTR lvl2(0), VARPTR lvl3(0) lvl1: DATA 3,2,5,8 lvl2: DATA 4,2,4,6,8 lvl3: DATA 5,1,3,5,7,9 This solution is five ASM instructions longer than my first solution with PEEK, but perhaps easier to understand and not as kludgy. Edited July 30, 2020 by carlsson 1 Quote Link to comment Share on other sites More sharing options...
+nanochess Posted July 30, 2020 Share Posted July 30, 2020 I'm afraid to say that you now are an expert in the use of VARPTR. These are the only ways to use it. I'll have to look into the intvnut's suggestion for VARPTR minus VARPTR expression for DATA, as probably there is something wrong in compilation. 2 Quote Link to comment Share on other sites More sharing options...
carlsson Posted July 31, 2020 Author Share Posted July 31, 2020 Ah. It reminds me about the story where a girl who was a computer newbie called Mr. Hotline thrice and quickly realized that within a week of self studies, she would be qualified to enter the staff at the hotline herself. 2 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.