News:

Let's find out together what makes a PIC Tick!

Main Menu

Lost code extracting data from ASM

Started by TimB, Feb 23, 2023, 09:25 PM

Previous topic - Next topic

TimB


Hi all

Horrors of horrors I lost a bas file.

I have the asm which has all the lines of code in it. Is there a code stripper around

Example of what I need F1_001131 equ $ ; in [OPTOBOARD 13K22 I2C V1.BAS] Inc bVelAvePntr

The bit in bold is what I need. Is there any utility's to help? Basically remove any line not starting with F1 then remove all upto ]

Thanks

Tim



Gary Scott

#1
I ask chatGPT and here is one answer, and it recommends sed, and it supplies the expressions to make it happen.  https://gnuwin32.sourceforge.net/packages/sed.htm

IDA Pro and Ghidra may do what you want but both have one hell of a learning curve and probably not worth the effort unless you know one of them already

Gary Scott

#2
This Python script should also do what you want (not tested) if you prefer Python over sed. You will need python installed on your computer.

Make back up! before trying any procedure!!!!!!!!!!!

I ran Python with Powershell on Windows to run python

import re

# read in the input file
with open('input_file.txt', 'r') as f:
    input_lines = f.readlines()

# filter out lines that do not start with F1
filtered_lines = [line for line in input_lines if line.startswith('F1')]

# remove everything on each line up to and including the first ']' character
output_lines = []
for line in filtered_lines:
    output_lines.append(re.sub(r'^.*\]', '', line))

# write the output to a new file
with open('output_file.txt', 'w') as f:
    f.writelines(output_lines)

QuoteTo run this code, you'll need to save it to a Python file (e.g., my_script.py) and make sure you have a text file with the input data in the same directory. Then, follow these steps:

Open a command prompt or terminal window.
Navigate to the directory where your Python file and input data file are located using the cd command. For example, if they are both located in a directory called "my_project", you could navigate there with the command cd my_project.
Once you are in the correct directory, you can run the Python script by typing python my_script.py (replace "my_script.py" with the name of your Python file).
After running the script, you should see a new file in the same directory called "output_file.txt", which will contain the processed output data.
Note that you will need to have Python installed on your system in order to run this script. If you don't have Python installed, you can download it from the official website (https://www.python.org/downloads/).

JohnB

You could do the same thing using Regular Expressions.
JohnB

TimB

Quote from: Gary Scott on Feb 24, 2023, 12:33 AMI ask chatGPT and here is one answer, and it recommends sed, and it supplies the expressions to make it happen.  https://gnuwin32.sourceforge.net/packages/sed.htm

IDA Pro and Ghidra may do what you want but both have one hell of a learning curve and probably not worth the effort unless you know one of them already

LOL I asked Chat GPT and it wrote me a script as well

with open('input.txt', 'r') as input_file, open('output.txt', 'w') as output_file:
    for line in input_file:
        if line.startswith('F1'):
            output_file.write(line)

Only issue is I cannot make it run. No idea if it is real code or it pulled it from its.. I will ask Son as he writes python..


atomix

#5
use Notepad++

Menu -> Search -> Replace -> Press button "Replace All"

use the following regular expressions

  • ^(?!F1_).*$
  • ^(\r\n)+$
  • ^F1_[^\]]+\]


TimB


Thanks atomix

I'm not sure what I'm doing wrong. It just says no matches

searchreplace.png

Stephen Moss

Quote from: TimB on Feb 24, 2023, 07:27 AMLOL I asked Chat GPT and it wrote me a script as well

with open('input.txt', 'r') as input_file, open('output.txt', 'w') as output_file:
    for line in input_file:
        if line.startswith('F1'):
            output_file.write(line)

Only issue is I cannot make it run. No idea if it is real code or it pulled it from its.. I will ask Son as he writes python..
Looks like pseudocode to me and one that would not do the job anyway as it appears to be reading and then writing out the entire line.

In VB if ] is always the last character before the BASIC code then I would do something like...
FileOpen(1, FilenName, OpenMode.Input)        'Open Source File
FileOpen(2, FileName, OpenMode.Output)        'Open Destination file

Do Until EOF(1)                               'Loop until the end of the source File is reached
X = Lineinput(1)                              'Get Next Line of Data from the Source File
Y = InstrRev("]", X, 1, CompareMethod.Text)   'Get Location of ] staring from Right side of String
X = Strings.Right(X, Y-1)                     'Strip out everything left of ] to leave just the BASIC code
Print(2, X)                                   'Write BASIC Code to the new file
Loop

FileClose(1)                                  'Close Source File
FileClose(2)                                  'Close Destination File

It may not be 100% working code as it is a quick lash-up but it would not be far off, I know using stream readers/writes in the modern way but for something so simple the classic LineInput, Print & FileOpen/Close method would be sufficient.

JohnB

@TimB

I will send you a version of studio which will let you do this.  Studio relies heavily on Regular Expression and I have all the code in there to generate the file, it would be quicker than building a new app.  I couldn't find an easy way to do this using the RegEx in notepad++

This is the RegEx ^\s*(?:F\d{1}_\d{6}\sequ\s\$\s;\s[a-z\s[0-9.]+)](.)
JohnB

Pepe

#9
I created a program to generate the .bas file from the .asm missing still some things runs asm_bas xxx where xxx is the name of the asm file but without the extension and creates a file xxx_.bas
Look #26

atomix

You may have copied a space character while copying

TimB


Thanks everyone the support is fantastic. Some results

After removing the " " as was pointed out the Notepad++ worked a treat.

The Python code worked when run properly (it was only to leave the F1.... Lines)

Not tried the .exe yet as have to do that from a command line I think

In the mean time I also did it the hard way with note pad and loads of highlight + deletes
Personally I like the Notepad++ results as it left spaces and made it look easier to add the comments back in etc

What it did highlight though is my backups were not running properly. Google Back up on that file had not changed in months and would have just overwritten the files any case. I thought I had Windows backup running but it will not enable it unless I use of machine storage so non of the usual previous version stuff was working.
I'm investing now in a 3rd party backup to my G cloud account and going to see why backup in Positron IDE is not running.

Thanks again everyone

John Lawton

Good to hear you've managed to get your source code back.

I don't use Backup in Positron Studio, but I notice it only creates a .zip text file of the file names, and no actual source code is backed up... I don't know whether this is a known problem.

For my general company data backup, I store everything on a small file server which has an overnight 'snapshot' backup to another drive. As a result I have 30 days of file backup with that system which I can restore from if required, which I occasionally have to do. It has saved me a number of times.

keytapper

#13
Quote from: JohnB on Feb 24, 2023, 12:39 PMThis is the RegEx ^\s*(?:F\d{1}_\d{6}\sequ\s\$\s;\s[a-z\s[0-9.]+)](.)
This is the rightest. It takes in account whether there are more than a file included on the assembly file.
Ignorance comes with a cost

TimB

Quote from: John Lawton on Feb 24, 2023, 05:35 PMknow whether this is a known problem.

For my general company data backup, I store everything on a small file server which has an overnight 'snapshot' backup to another drive. As a result I have 30 days of file backup with that system which I can restore from if required, which I occasionally have to do. It has saved me a number of times.

I actually have a NAS hidden away but its a power hog and can warm a room. Since I pay G money for storage I might as well use it.

Pepe

#15
Mira el #26

rick.curl

Quote from: TimB on Feb 24, 2023, 05:13 PMI'm investing now in a 3rd party backup to my G cloud account and going to see why backup in Positron IDE is not running.
Hi Tim- I'm using Insync to keep files and libraries backed up and sync'ed across multiple computers.  It's affordable and the support is very good.

-Rick

Dompie

@Pepe When I download your asm_bas.zip I got a "serious" warning from Microsoft Defender.
Message: Trojan:Win32/Wacatac.B!ml

I haven't investigated further, I want to report it (since I almost never get notifications from Microsoft Defender)

Johan

Pepe

#18
look #21

Pepe

Try another antivirus, it can be a false positive, I have Windows Defender and does not report any virus