News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Python and MASM

Started by jj2007, November 30, 2023, 10:24:46 PM

Previous topic - Next topic

jj2007

There is a separate thread on using Python for calculating square roots started by Raymond, but I wanted to avoid hijacking his thread, therefore a new one here.

Python has a reputation for being the slow 21st Century BASIC. Probably true if we are talking about the interpreter, but there is also a bunch of libraries called CPython (correct me if I'm wrong).

I've started working with these libraries, see Assembly uses Python. There are over 1,600 functions to explore, see attachment, some of them very interesting and very fast (e.g.  TimSort*)).

First tests work fine but not perfectly. My attempt to use PyList_Sort fails with a crash, and I have no clue why it does so. Especially since assigning, reversing and displaying the items of the list works fine.

Anybody here who has more experience with Python?

Quoteint PyList_Sort(PyObject *list)¶
Sort the items of list in place. Return 0 on success, -1 on failure. This is equivalent to list.sort().

int PyList_Reverse(PyObject *list)
Reverse the items of list in place. Return 0 on success, -1 on failure. This is the equivalent of list.reverse().

What I understood is that there are two flavours of Python:
- CPython = python.exe with calls to Py_*, PyList_* etc functions; this is where we can talk to Python from Assembly;
- "real" Python, with list.sort() stuff that is probably not accessible to us.

How they correspond is not clear to me; as the equivalent above shows, it's sometimes mentioned in the docs, but so far I haven't found a dictionary that explains the equivalents systematically. There is a detailed manual for an older version (3.6, 2017) by ETH Zürich, but it's CPython only.

*) slashdot:
QuoteLong has the conviction been held that quicksort is faster than merge sort. Timsort (derived from merge sort and insertion sort) was introduced in 2002 and while slower than quicksort for random data, Timsort performs better on ordered data.

P.S.:
Print Str$("SomeDword as double: %Jf\n", PyLong_AsDouble(PyLong_FromUnsignedLong(SomeDword))v)
Output for SomeDword=12345678: SomeDword as double: 12345678.00000000000

Assembles fine with MASM, UAsm, JWasm and AsmC, so I assume it's pure x86 Assembly :biggrin:

GoneFishing

#1
Hi Jochen,

In fact I'm not Python guru... thus I can provide only  very basic advice on the subject.

First of all I've noticed that you took windows.inc as a dataset for testing.
Maybe it's worth trying to use smaller arrays and check them in python shell first:
lst=['string1','string2','string3','string4','string5','string6','string7','string8','string9','string10','string11','string12','string13','string14']
srt=[4,8,977,95,5,874,986,234,-8,9,-4,754,123,0]
ttt=[';;;asd','gfd','hfh:sh','re__we','tr@ew','rewrwqr','uutyy','###ouiiotit','oops#','wow^','<str>yeah','mmmm','break']

lst.reverse()
print(lst)

idx=lst.__len__()-1

for i in range(0,lst.__len__()):
    print(lst.__getitem__(idx-i))
   
srt.sort()
print(srt)

ttt.sort()
print(ttt)

Official Python package has help docs in CHM format ( with dedicated chapter on C Python API and Python embedding)

Python is meant to be embedded into your app as a scripting engine. Another easily embeddable language is Lua.
Id est you create engine inside your app and feed it with Python scripts and commands. Playing with separate Python C functions could be fun maybe but has no sense IMO.

<ADDITION>

Well, I've just tryed sorting of windows.inc file in Python shell and it worked fine.

import os
f=open(fname, 'r') - open file
t=f.read()               -  read it
f.close()                  - close it

lines=t.split('\n')  - split lines and write them into the list
print(type(lines)) 

lines.sort()           - now we can sort lines
print(lines)


jj2007

Great. So how can I access the sorted array from MASM?

Quote from: GoneFishing on December 01, 2023, 03:18:32 AMWell, I've just tryed sorting of windows.inc file in Python shell and it worked fine.

import os
f=open(fname, 'r') - open file
t=f.read()               -  read it
f.close()                  - close it

lines=t.split('\n')  - split lines and write them into the list
print(type(lines)) 

lines.sort()           - now we can sort lines
print(lines)

GoneFishing


jj2007

The problem with the Python shell is getting results that you can use in MASM. Not a new problem, btw: 8 years ago

That's why I want to work with python3.dll, not with the interpreter :cool:

TimoVJL

May the source be with you

jj2007