Simplifying+arithmetic+using+strings

Sometimes, rather than building a complicated formula for a piece of code, it can be simplified by using a string of values.

For instance, I was recently updating some Acorn code that read VDU variables so that it would run in BBC BASIC for Windows. The VDU variables are in a slightly different order on BBFW than they are on RISC OS systems:


 * n || Acorn VDU(n)     || m  || BBFW @vdu%!m     ||
 * 8 || text left side   || 24 || text left side   ||
 * 9 || text bottom line || 36 || text bottom line ||
 * 10 || text right side || 28 || text right side  ||
 * 11 || text top line   || 32 || text top line    ||

I didn't want to go through the whole program changing every reference, it was easier and safer to build a veneer function to convert Acorn VDU offsets into BBFW VDU offsets.

If you subtract two from the Acorn offset and then multiply by four you get the same range of BBFW offsets, but in a different order. The difficulty was trying to build a formula to get them in the right order.

Looking at the bottom two bits, it needs to convert 0, 1, 2, 3 to 0, 3, 1, 2. I found the easiest way to do that was as follows: code format="bb4w" m=24+4*VALMID$("0312",(n AND 3)+1,1) code This changes 8, 9, 10, 11 to 24, 36, 28, 32. Taking account of @vdu% values being in pixels, not characters, this allowed me to write the following veneer function: code format="bb4w" DEF FNvdu(n) IF (bbfw) THEN =@vdu%!(24+4*VAL MID$("0312",(n AND 3)+1,1)) DIV (8+8*(n AND 1)) ENDIF =VDU n code where bbfw is a variable in the program that indicates that it is running on BBC BASIC for Windows.

//Richard Russell, May 2009// The above routine works correctly only in MODEs in which a character cell is 8 pixels wide by 16 pixels high. The alternative code listed below works in all MODEs, and is (arguably) easier to understand:

code format="bb4w" DEF FNvdu(n) CASE bbfw AND n OF       WHEN 8: = @vdu.tl% DIV @vdu%!216 WHEN 9: = @vdu.tb% DIV @vdu%!220 WHEN 10: = @vdu.tr% DIV @vdu%!216 WHEN 11: = @vdu.tt% DIV @vdu%!220 ENDCASE = VDU n code This code relies on **bbfw** being a Boolean (i.e. having the value 0 or -1).