Author Topic: STR$  (Read 12071 times)

Offline DavidA

  • Hero Member
  • *****
  • Posts: 1219
  • Country: gb
STR$
« on: November 30, 2014, 03:35:25 PM »
Here is a chance to scrape the rust of your memory cells.

For ex- GWBASIC programmers.

I have a small problem.

I created a little program that reads bit six of the printer ports status port.

All it does is pol the status of the port A=(&h379).

It does this eight times and creates a series of Strings (E$ to L$) and concatenates the eight strings to create one string called C$

So far so good.

So I finish up with something like C$=(11111111) if all the hits were + or c$=(00000000) if they were all naughts.

Now for the fun bit.

As I am wanting to finish up with the decimal value of this string (from 0 to 255) I have sliced out each digit and compared it with a table holding the values. 128, 64,32,16,8,4,2,1.

Here is the problem.

If I use use, say, E=VAL(mid$(c$,1,1)) to get the first digit then when I get a wrong answer.  I always get E=0.  Not e=128 as the table says.

Now, I have worked out what is happening,  but not why.

If I change the line to E=VAL(mid$(c$,2,1)) theoretically extracting the second bit,  which one would expect to be 64, then it works. It gives me 128.

I have to go up in stages of two. So the seventh bit becomes K=VAL(mid$(C$,14,1)). Up to the last part (bit 8) which needs to be read as L=VAL(mid$(c$,8,1)) or it gives back L=0 every time.

I suspect that it is something to do wit an 'invisible' sign bit in front of each bit. So I see 8 bits but there are actually 16.

What is the proper procedure for using MID$ ?

Dave.



Offline vtsteam

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 6466
  • Country: us
  • Republic of Vermont
Re: STR$
« Reply #1 on: November 30, 2014, 05:18:46 PM »
Dave,

hard to say without seeing the actual code. Did you first check that MID$() and VAL is working for you the way you guess it is by something like:

C$= "12345678"

FOR i = 1 to 8
PRINT MID$(C$,i,1)
PRINT VAL(MID$(C$,i,1))
NEXT

--I'm going to have to dig out my old laptop with win98 for this!!
« Last Edit: November 30, 2014, 05:47:06 PM by vtsteam »
I love it when a Plan B comes together!
Steve
https://www.youtube.com/watch?v=4sDubB0-REg

Offline RussellT

  • Hero Member
  • *****
  • Posts: 520
  • Country: gb
Re: STR$
« Reply #2 on: November 30, 2014, 05:21:34 PM »
I've not checked on this but is this a problem with bytes and integers - both can use a single byte but encoding is different?

Russell
Common sense is unfortunately not as common as its name suggests.

Offline vtsteam

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 6466
  • Country: us
  • Republic of Vermont
Re: STR$
« Reply #3 on: November 30, 2014, 05:27:15 PM »
Integers in the old interpreted MS basics were 2 bytes and ranged from -32K to 32K.
I love it when a Plan B comes together!
Steve
https://www.youtube.com/watch?v=4sDubB0-REg

Offline awemawson

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8966
  • Country: gb
  • East Sussex, UK
Re: STR$
« Reply #4 on: November 30, 2014, 05:48:00 PM »
In that basic version is there a 'Mask' function. Years back when I was doing bit manipulation of bytes I'd mask to the bit I wanted, logical 'and'. them and jump on none zero result, or alternatively add the byte to itself repeatedly testing the sign bit as this effectively does a serial left shift of bits into the sign position.
Andrew Mawson
East Sussex

Offline vtsteam

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 6466
  • Country: us
  • Republic of Vermont
Re: STR$
« Reply #5 on: November 30, 2014, 05:58:37 PM »
Don't remember a mask function, Andrew, but I got the old laptop out and ran QBasic, and can confirm that the output of VAL gives two characters for a positive number. A space (a placeholder for a sign) and the number itself.

We just have to trim off that space -- called in for dinner now, but we can do it with a function -- just have to look it up again after a bit of turkey in the interim!
I love it when a Plan B comes together!
Steve
https://www.youtube.com/watch?v=4sDubB0-REg

Offline vtsteam

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 6466
  • Country: us
  • Republic of Vermont
Re: STR$
« Reply #6 on: November 30, 2014, 07:19:26 PM »
This should do the whole job without a table
Where C$ contains a binary 8 bit string
And result X contains the decimal number:


Code: [Select]
X=0
FOR i = 8 to 1 step -1
E = VAL(MID$(C$,i,1))
X = X + (E*((2^(8-i)))
NEXT
I love it when a Plan B comes together!
Steve
https://www.youtube.com/watch?v=4sDubB0-REg

Offline DavidA

  • Hero Member
  • *****
  • Posts: 1219
  • Country: gb
Re: STR$
« Reply #7 on: November 30, 2014, 07:24:52 PM »
VT,

That looks as if it should do it.

I'll try it in the morning. I can run it as a subroutine at the end and just bypass the current method with the table.

I'm off to bed now.

Cheers.

Dave.

Offline vtsteam

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 6466
  • Country: us
  • Republic of Vermont
Re: STR$
« Reply #8 on: November 30, 2014, 07:56:34 PM »
If anyone is interested, you can still get good old QBASIC through links here and there are a bunch of tutorial resources and other fun stuff at:

http://www.petesqbsite.com/index.php
I love it when a Plan B comes together!
Steve
https://www.youtube.com/watch?v=4sDubB0-REg

Offline Joules

  • Hero Member
  • *****
  • Posts: 1271
  • Country: gb
Re: STR$
« Reply #9 on: December 01, 2014, 07:50:47 AM »
For anyone who might like playing with basic and has an iPad this is a nice application.  I use it quite a bit for developing ideas and general play to keep my nostalgia alive and kicking

No association, just impressed customer.

Uh oh, my bad turns out the software now just crashes in iOS 8 so link removed.  Hope they get it fixed soon.  😠
« Last Edit: December 01, 2014, 09:06:44 AM by Joules »
Honour your mentors, and pay it forward.

Offline DavidA

  • Hero Member
  • *****
  • Posts: 1219
  • Country: gb
Re: STR$
« Reply #10 on: December 04, 2014, 04:31:51 PM »
vt,

Turns out that things are much trickier that first thought.

That c$ I,e,  C$=(11111111)  seems to be a 16 byte string.

I ran print len(c$) and discovered this.

Sooooo. it is actually 0101010101010101

and it is not responding well to slicing.

You would think it quite straight forward to extract every second byte,  but oh no.
If I start at byte 8 I get 256  2^8 = 256
But  with    byte 7 I get the same. For some reason it delivers a pair of values. both equal to the even number'

You'll note I say bytes and not bits.  I suspect the whole problem is because c$ is a concatenation of eight other strings. each of these strings (e$ to l$) is either 0 or 1 depending on the state of the port.

I'm going to have to go back to the drawing board and devise a different method.

Woe is me.

Dave :doh:

Offline David Jupp

  • Sr. Member
  • ****
  • Posts: 308
  • Country: gb
  • Teesside - UK
Re: STR$
« Reply #11 on: December 05, 2014, 02:45:36 AM »
In a string, each character is represented by an ASCII code (2 bytes) - if you want to do arithmetic, you'll have to convert to a number first (either the whole string, or the snippets cut from it).  The CAST operator may be helpful.

Offline RussellT

  • Hero Member
  • *****
  • Posts: 520
  • Country: gb
Re: STR$
« Reply #12 on: December 05, 2014, 05:35:17 AM »
I haven't tried this because my computer with qbasic on is out in the garage but it seems to me that you'd be better off trying to do without strings.

For example.

Create a couple of variables eg poweroftwo and output and set them to one and  zero respectively.
Create a for/next loop to go around 8 times.
Double the poweroftwo variable each time you go around a loop to give powers of 2.
If the printer port bit is set add the power of two to the output variable - at the end of the loop the output variable should be the decimal value you are seeking.

I hope this helps.

Russell
Common sense is unfortunately not as common as its name suggests.

Offline gerritv

  • Full Member
  • ***
  • Posts: 115
  • Country: ca
  • St Catharines
    • My Hobbies
Re: STR$
« Reply #13 on: December 05, 2014, 08:32:22 AM »
If you use a shift to feed the bit value into the output variable you will then know for a fact that the output is a consecutive series of bits?

http://www.petesqbsite.com/phpBB3/viewtopic.php?f=1&t=1389

So, shift value left 1 bit, OR value of bit 6 into the low order bit, loop

Output is then an 8 bit value in 16 bits, no lookup table required.

Offline vtsteam

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 6466
  • Country: us
  • Republic of Vermont
Re: STR$
« Reply #14 on: December 05, 2014, 11:39:41 AM »
try

Code: [Select]
X=0
FOR i = 16 to 1 step -2
E = VAL(MID$(C$,i,1))
X = X + (E*(2^(8-(i/2))))
NEXT

let me know if that works.

if that always gives you 0 as an answer we just have to shift one.


ps. I see I had an error in my earlier code -- one too many left parenthesis -- but you probably already figured that out.  :palm:
I love it when a Plan B comes together!
Steve
https://www.youtube.com/watch?v=4sDubB0-REg

Offline DavidA

  • Hero Member
  • *****
  • Posts: 1219
  • Country: gb
Re: STR$
« Reply #15 on: December 05, 2014, 12:33:49 PM »
Thanks for all the help on this one.

I managed to approach the problem from a different direction.

Here is my solution.


20 X=1:Y=1
30 cls
40 T=0
50 M=0
55 Q=0
60 A=inp(&H379)
70 B=A AND 64
80 IF B=64 THEN C=1 ELSE C=0
90 D=C*2^(T)
92 Q=Q+D
100 PRINT;D,Q
110 FOR X=1 600000:NEXT X
120 IF T=7 THEN GOTO 150
130 T=T+1
140 GOTO 60


It avoids the business of strings altogether.
After every eight bits I have a byte value. I can then pass this to a file for storage and later retrieval.

Isn't programming fun.

Dave

VT, Yes,  I noticed the extra bracket.

Offline vtsteam

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 6466
  • Country: us
  • Republic of Vermont
Re: STR$
« Reply #16 on: December 05, 2014, 01:34:52 PM »
It is fun David, thanks for getting me back into it to solve a little puzzle -- even if it wasn't the same as your solution -- I got to think about it again. I actually ordered a used book about QBASIC (90's vintage) as a result of your question, and may fool around with the language again for kicks.

It would be fun to have a small puzzle in BASIC to solve every week!

I like that idea better than crossword puzzles.
I love it when a Plan B comes together!
Steve
https://www.youtube.com/watch?v=4sDubB0-REg

Offline DavidA

  • Hero Member
  • *****
  • Posts: 1219
  • Country: gb
Re: STR$
« Reply #17 on: December 05, 2014, 03:26:22 PM »
VT,

The beauty of brewing your own programs is that there is no single solution. I suppose that there are ideal solutions to every situation.  But there are many ways that one can come to the required conclusion.  Some far more elegant than others. The one that works for you is the best one.

And some programs come about from the strangest reasons. I'm slightly wary about explaining the reason behind the bit of code I presented. But here goes.

Many of you may have heard of the Rendlesham Incident.

A later development of this has an individual who was suppose to have been 'given' a message in the form of binary code.  If this seems to you to be highly unlikely,  then rest assured you are not alone.  Anyway, this chap claims to have been given this long series of binary naughts and ones.  And,  surprise,  it turned out to be a message.  When you divide up the continuous data stream into bytes they happen to be numbers that equate to ASCII code characters. No punctuation, line feeds etc,  just letters and numerals,  all upper case.

An odd way for aliens to communicate.  But that's his story.

Anyway,  I got to thinking 'how would I write a program to do the decoding. I wanted something where I could feed in a data stream and convert it into bytes and then search  for recognizable text.

That little program is the upstream end of the program that converts a continuous stream of binary into bytes.

And you thought I was sane ?

Dave. :D

Offline DavidA

  • Hero Member
  • *****
  • Posts: 1219
  • Country: gb
Re: STR$
« Reply #18 on: December 05, 2014, 03:37:44 PM »
For anyone who does like a binary distraction, here is something to chew on.

The situation.

We have a continuous stream of binary coming in.
We know we can easily slice it up into byte sized pieces.
And we are going to assume that the eventual output will be ASCII characters leading to a line of plain text (in English).

We don't know where in the continuous flow of bits the starting point is. Remember,  unlike standard teleprinter code,  there are no start bits,  stop bits etc.

The task is to write a short subroutine to find the start of a byte.

Gentlemen,  start your BASIC.

Dave.

Offline RussellT

  • Hero Member
  • *****
  • Posts: 520
  • Country: gb
Re: STR$
« Reply #19 on: December 05, 2014, 05:18:16 PM »
If you make the specification that it's plain text in English I think it's fairly straightforward.  If it's plain text it won't use any extended ascii codes so it will only have byte values between 0 and 127.  The most significant bit in every byte will be 0 and that could be used to find the end of the byte.

Russell
Common sense is unfortunately not as common as its name suggests.

Offline vtsteam

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 6466
  • Country: us
  • Republic of Vermont
Re: STR$
« Reply #20 on: December 05, 2014, 06:37:24 PM »
Good thinking Russell! I forgot that the first bits were zero for letters.

Man, so much forgotten since those really fun days when all this seemed new and I had my first computer, a Tandy Color Computer with 4K of memory and interpreted basic in ROM.

Those had a MC 6809 processor. Apples 6502 and the RS Model 1 a Z-80. Eventually I built a z-80 based computer from scratch (couldn't afford a Model 1) and learned Z-80 assembly and FORTH.
I love it when a Plan B comes together!
Steve
https://www.youtube.com/watch?v=4sDubB0-REg

Offline DavidA

  • Hero Member
  • *****
  • Posts: 1219
  • Country: gb
Re: STR$
« Reply #21 on: December 05, 2014, 07:25:43 PM »
Russell,

32 to 127 if I remember correctly.(spacebar to +/-)

The thing your program has to do is decide which of the zeros is the start of the character.

It's quite easy doing it using paper and pencil.

a bit when you have to write the program.

I have an good idea how I'd do it. it will be interesting to compare methods.

VT,
I still have a TRS-80 model 1.  Upper case only and 4K of memory.  I did my initial learning on that then moved on to an Amstrad 8256; using CP/M and Locomotive BASIC.  But went back to TRS-80 model 4 with LS DOS.

I still have them all.  and they still work.

Dave.

Offline vtsteam

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 6466
  • Country: us
  • Republic of Vermont
Re: STR$
« Reply #22 on: December 05, 2014, 08:26:25 PM »
YES LSDOS! I had that, too! Didn't it have a built in monitor and disassembler.

I built an LNW-80 from scratch, soldering in a couple hundred IC's etc. Programmed EPROMS with a wire wrapped board off the printer port, and erased it with a sunlamp. I customised the ROM a little -- otherwise it was straight TRS-80 Model 1 (extended) . Also figured out that if I changed the clock divider I could overclock from 4 Mhz to 5.33 Mhz though I never knew it would be called overclocking years later.

The LNW did color, though a Z-80 based computer, and I wrote a wire frame 3D projection mini CAD with FORTH and mixed-in assembler routines. This was just before the IBM PC came out and I was disappointed it was 8088 based. But really liked the bus and cards architecture (IBM's ultimate downfall due to ease of cloning and Microsoft selling to all comers) but they really opened up the computer world with the biggest name backing open architecture. We owe them a lot.

Now we're seeing a reversal of that revolution. Locked OS's and boot loaders, single boards, irreplacable batteries and components, cloud computing and apps. We're back to the mainframe and dumb terminal era. Just fancied up with another name: "cloud".  Chromebooks incapable without a connection. BASIC in ROM unheard of. No one programs PCs, when once evryone did. The personal computer is practically dead. The PC revolution, a thing of the past.

« Last Edit: December 05, 2014, 08:54:15 PM by vtsteam »
I love it when a Plan B comes together!
Steve
https://www.youtube.com/watch?v=4sDubB0-REg

lordedmond

  • Guest
Re: STR$
« Reply #23 on: December 06, 2014, 03:20:38 AM »
Looks like I need to get out the nascom and Fire up polly dos then load up polly basic
Now where are those 5 1/4 floppies to boot the machine up

Good luck with your quest ( or was that colasal cave) no it must be hunt the wumpus

Boy I have forgotten such a lot of $basic

Stuart

Offline awemawson

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8966
  • Country: gb
  • East Sussex, UK
Re: STR$
« Reply #24 on: December 06, 2014, 03:29:48 AM »
5 1/4 floppies - those are modern !!! My first home machine had 8" floppies - Shuggart 801's iirc !!!!
Andrew Mawson
East Sussex

Offline vtsteam

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 6466
  • Country: us
  • Republic of Vermont
Re: STR$
« Reply #25 on: December 06, 2014, 08:54:24 AM »
But of course the real mass storage device during the PC revolution was the casette tape recorder, and the monitor: a TV screen. Very crude by today's standards, but exciting to see the prompt come up back then. Your own computer! You could do anything -- all it took was writing a program. Or typing it in from a magazine.

I've been arguing locally, as a member of the school board, that a recent $10K grant we received should be spent on something like your $35 British Raspberry Pi's to keep and bring home for elementary school students. But the District administration IT department won't hear of it and insists on $275 Chromebooks kept at school. They call that "teaching technology."

If you want to teach technology, give a kid a computer that won't do much without programming and building it up into something. And let him/her take t home and mess around with it. If it breaks it's 35 bucks to replace.
I love it when a Plan B comes together!
Steve
https://www.youtube.com/watch?v=4sDubB0-REg

Offline awemawson

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8966
  • Country: gb
  • East Sussex, UK
Re: STR$
« Reply #26 on: December 06, 2014, 09:51:52 AM »
Thinking back, I used to record "RTTY" (Radio Teletype) signals picked up on my 'HRO' communications receiver on a reel to reel recorder, (using it as a backing store), then process them using a SC/MP development system into teleprinter 80-0-8 volt to drive my Creed 54N teleprinter. I remember recovering a whole load of telegram messages between (of all places!) Russia and Israel in the mid 1970's - got me very interested in electronics and computing as a hobby.

Bletchley Park would have been proud of me  :lol:
« Last Edit: December 06, 2014, 10:55:30 AM by awemawson »
Andrew Mawson
East Sussex

lordedmond

  • Guest
Re: STR$
« Reply #27 on: December 06, 2014, 11:16:45 AM »
Another comment from the past there Andrew TTY
I used a ASR Printer at 110 baud driven from a spare bit on the keyboard port

Stuart

Offline awemawson

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8966
  • Country: gb
  • East Sussex, UK
Re: STR$
« Reply #28 on: December 06, 2014, 12:12:40 PM »
110 baud came later when I also had an ASR33 - the telex stuff whizzed along at the dizzy speed of 50 baud (but some government stuff was 55 baud)
Andrew Mawson
East Sussex

Offline DavidA

  • Hero Member
  • *****
  • Posts: 1219
  • Country: gb
Re: STR$
« Reply #29 on: December 06, 2014, 12:56:26 PM »
My first contact with electronic mail came in 1980 when I was working in Baghdad.  Our office acquired it's own teleprinter. The freedom to just send and receive text when we wanted instead of having to drive over to the local telex office was overwhelming.  The same year I got to try a computer for the first time. But didn't get my own until about 1986.

Dave.