CBM BASIC 2.0 using STR$
Moderator: Moderators
CBM BASIC 2.0 using STR$
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
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
- Mayhem
- High Bidder
- Posts: 3007
- Joined: Mon May 24, 2004 7:03 am
- Website: http://www.mayhem64.co.uk
- Location: London
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
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...
Think I got it
just needed to isolate things a bit
3700 b$=right$(str$(nn),1):return
Thanks for the other tips
3700 b$=right$(str$(nn),1):return
Thanks for the other tips
- Mike
- Herr VC
- Posts: 5130
- Joined: Wed Dec 01, 2004 1:57 pm
- Location: Munich, Germany
- Occupation: electrical engineer
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:
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
You got me
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.I deliberately left that as exercise.

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

Thanks for the tips
How about ..
Lee.
Code: Select all
3000 b$=mid$("0123456789ABCDEF",nn+1,1):return
Ok so I am lazy
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.buzbard wrote:How about this: (highlight to get the answer)
Set D with decimal number: Gosub3000: H$ contains hex.3000 h$="":d=d/4096:forii=1to4:d%=d:h$=h$+chr$(48+d%-(d%>9)*7):d=16*(d-d%):next:return
All on one line!
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.
I cannot help but smile at Mike's understatements followed by magicMike 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

P>9; an evaluation to 0 or 1 inside an expression, right? Or was it -1 and 0?