CBM BASIC 2.0 using STR$

Basic and Machine Language

Moderator: Moderators

Post Reply
Z0rb
Vic 20 Amateur
Posts: 41
Joined: Mon Apr 29, 2013 5:45 pm

CBM BASIC 2.0 using STR$

Post by Z0rb »

Ok this might be a silly question but I cannot seem to figure it out. I am writing a little program that will display memory addresses in HEX and am attempting to convert the decimal values of a PEEK into HEX, for the most part it works HOWEVER I get an annoying space between the first character and the second whenever I have an numeric value involved.

Here is a chunk of code I am using. It appears that whenever I use STR$ it adds a space before the number. Lines 1480 and 3700 I think are the sticking points. Is there any way to remove this padded character in my output? When working with such limited columns, I'd like to keep the output as tight as possible.

1300 print chr$(147)
1400 for i=0 to 10
1410 a = peek(addr+i)
1420 nn=int(a/16)
1430 gosub 3000
1440 h$=b$
1450 nn=a-(16*(int(a/16)))
1460 gosub 3000
1470 l$=b$
1480 print addr+i;":";h$;l$
1490 next i

3000 b$ = ""
3100 ifnn=10thenb$="a":return
3200 ifnn=11thenb$="b":return
3300 ifnn=12thenb$="c":return
3400 ifnn=13thenb$="d":return
3500 ifnn=14thenb$="e":return
3600 ifnn=15thenb$="f":return
3700 b$=str$(nn):return

Please help
Thanks
User avatar
Mayhem
High Bidder
Posts: 3027
Joined: Mon May 24, 2004 7:03 am
Website: http://www.mayhem64.co.uk
Location: London

Post by Mayhem »

You can compact the subroutine if anything down to this:

3000 IF NN<10 THEN B$=STR$(NN): RETURN
3001 B$=CHR$(55+NN): RETURN

Line 1450 can be written still as:

1450 NN = A - (16*NN)

Not sure about the STR$ issue though. If it adds a space in the function, then line 3000 could go to:

3000 IF NN<10 THEN B$=RIGHT$(STR$(NN),1): RETURN
Last edited by Mayhem on Tue May 14, 2013 9:54 am, edited 2 times in total.
Lie with passion and be forever damned...
Z0rb
Vic 20 Amateur
Posts: 41
Joined: Mon Apr 29, 2013 5:45 pm

Think I got it

Post by Z0rb »

just needed to isolate things a bit

3700 b$=right$(str$(nn),1):return

Thanks for the other tips
User avatar
Mike
Herr VC
Posts: 4839
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

You can use MID$(STR$(...),2) to strip off the first character of the numeric string. The blank character serves as placeholder for the minus sign, so negative numbers wouldn't disturb the layout.

And the conversion of numbers in the range 0 .. 255 into a two-digit hex string can be expressed somewhat more compact like thus:

Code: Select all

1 FORB=0TO255:GOSUB3:PRINTB,H$:NEXT:END
2 :
3 H$="":P=INT(B/16):GOSUB4:P=B-16*P
4 H$=H$+CHR$(48+P-7*(P>9)):RETURN
Z0rb
Vic 20 Amateur
Posts: 41
Joined: Mon Apr 29, 2013 5:45 pm

Post by Z0rb »

And the conversion of numbers in the range 0 .. 255 into a two-digit hex string can be expressed somewhat more compact like thus:
Wow nice chunk of code that works perfect. How can I bump that up for a 16 bit address in hex? :lol:
User avatar
Mike
Herr VC
Posts: 4839
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

Z0rbVIC wrote:How can I bump that up for a 16 bit address in hex? :lol:
I deliberately left that as exercise. :P
Z0rb
Vic 20 Amateur
Posts: 41
Joined: Mon Apr 29, 2013 5:45 pm

You got me

Post by Z0rb »

I deliberately left that as exercise.
Heh ok ok. I looked over the code and thought "Hmm, I bet I can make that work for a 16 bit address, oh wait, maybe I can be lazy and just ask someone to do it for me.
:lol:

I'll put on my thinking cap later tonight, I need to get back to my ABAP code so I can put bread on the table :D

Thanks for the tips
Leeeeee
soldering master
Posts: 396
Joined: Fri Apr 23, 2004 8:14 am

Post by Leeeeee »

How about ..

Code: Select all

3000 b$=mid$("0123456789ABCDEF",nn+1,1):return
Lee.
User avatar
buzbard
Vic 20 Devotee
Posts: 213
Joined: Sun Jul 03, 2005 9:10 am

Post by buzbard »

How about this: (highlight to get the answer) :)
3000 h$="":d=d/4096:forii=1to4:d%=d:h$=h$+chr$(48+d%-(d%>9)*7):d=16*(d-d%):next:return
Set D with decimal number: Gosub3000: H$ contains hex.
All on one line!
Ray..
Z0rb
Vic 20 Amateur
Posts: 41
Joined: Mon Apr 29, 2013 5:45 pm

Ok so I am lazy

Post by Z0rb »

buzbard wrote:How about this: (highlight to get the answer) :)
3000 h$="":d=d/4096:forii=1to4:d%=d:h$=h$+chr$(48+d%-(d%>9)*7):d=16*(d-d%):next:return
Set D with decimal number: Gosub3000: H$ contains hex.
All on one line!
Heh ok I'm lazy and went with this result. The reason being that I would like to get a small program together where I can plug in and view small machine language programs into the VIC memory. I'm reading through an old book called VIC-20 Machine Code and I am using an adaption of their program to enter in programs, but I added onto it to be able to view things in hex and also added a routine to view parts of the memory as well.

I am at the point where everything is viewed in hex with the exception of entering in the starting memory addresses, thanks to everyone in the thread I should have that issue cleared up soon.
User avatar
Witzo
Vic 20 Afficionado
Posts: 381
Joined: Thu Dec 01, 2011 9:14 am
Location: The Hague

Post by Witzo »

Mike wrote:You can use MID$(STR$(...),2) to strip off the first character of the numeric string. The blank character serves as placeholder for the minus sign, so negative numbers wouldn't disturb the layout.

And the conversion of numbers in the range 0 .. 255 into a two-digit hex string can be expressed somewhat more compact like thus:

Code: Select all

1 FORB=0TO255:GOSUB3:PRINTB,H$:NEXT:END
2 :
3 H$="":P=INT(B/16):GOSUB4:P=B-16*P
4 H$=H$+CHR$(48+P-7*(P>9)):RETURN
I cannot help but smile at Mike's understatements followed by magic :lol:
P>9; an evaluation to 0 or 1 inside an expression, right? Or was it -1 and 0?
User avatar
Mike
Herr VC
Posts: 4839
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

Witzo wrote:Or was it [...]?
In CBM BASIC, -1 for true, 0 for false. :)
User avatar
Witzo
Vic 20 Afficionado
Posts: 381
Joined: Thu Dec 01, 2011 9:14 am
Location: The Hague

Post by Witzo »

Mike wrote:
Witzo wrote:Or was it [...]?
In CBM BASIC, -1 for true, 0 for false. :)
Thanks; I keep forgetting because it seems to be 0/1, negative/0, 0/positive and other variations in other programming languages.
Post Reply