Skip to main content

Python library for writing assembly code through object abstractions

Project description

flexpasm

Python library for writing assembly code through object abstractions. For Linux FASM.
Explore the docs »

Getting Started · Basic Usage · Specifications · License


flexpasm is a library for assembly language metaprogramming (FASM LINUX) via OOP, object relations. It also includes several useful tools for working with assembly code

Python library for writing assembly code through object abstractions. For Linux FASM.

Check Other My Projects

  • SQLSymphony - simple and fast ORM in sqlite (and you can add other DBMS)
  • Burn-Build - simple and fast build system written in python for C/C++ and other projects. With multiprocessing, project creation and caches!
  • OptiArch - shell script for fast optimization of Arch Linux
  • libnumerixpp - a Powerful C++ Library for High-Performance Numerical Computing
  • pycolor-palette - display beautiful log messages, logging, debugging.
  • shegang - powerful command interpreter (shell) for linux written in C
  • aioconsole - simple python library for creating async CLI applications

Getting Started

flexpasm is available on PyPI. Simply install the package into your project environment with PIP:

pip install flexpasm

Once installed, you can start using the library in your Python projects. Check out the documentation for detailed usage examples and API reference.

Basic Usage

You can view examples in examples directory.

Using templates

from flexpasm.instructions.segments import Label
from flexpasm.mnemonics import JmpMnemonic
from flexpasm.program import ASMProgram
from flexpasm.settings import Settings
from flexpasm.templates import PrintStringTemplate


def main():
	settings = Settings(
		title="Example ASM Program with Templates",
		author="alexeev-prog",
		filename="example_templates.asm",
		mode="32",
	)
	asmprogram = ASMProgram(settings, __name__)

	pst = PrintStringTemplate("Hello, World!")
	pst2 = PrintStringTemplate("Hello, World!", var="msg2", entry="print_string2")
	start_lbl = Label("start")

	start_lbl.add_instruction(
		JmpMnemonic("print_string"), 1, comment="Jump to print strint template"
	)

	asmprogram.add_label(start_lbl)
	asmprogram.add_template(pst)
	asmprogram.add_template(pst2)

	asmprogram.save_code()


if __name__ == "__main__":
	main()

Launch this script and run the following commands:

$ fasm example_templates.asm example_templates
$ ld example_templates -o example_templates
$ ./example_templates

Hello, World!

ASM Code:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Author: alexeev-prog                                                                                               ;;
;; Example ASM Program with Templates                                                                                 ;;
;; Program generated by FLEXPASM (github.com/alexeev-pro/flexpasm)                                                    ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

format ELF executable;                          ; ELF EXECUTABLE
entry start                                     ; Set Start Entry


;; Segment readable executable in FASM is a directive for defining a section of code with readable and executable attributes.
segment readable executable

start:                                          ; Label start with 1 commands
	JMP print_string                                ; Unconditional jump to label print_string; Jump to print strint template

print_string:                                   ; Label print_string with 7 commands
	; Printing the string 'Hello, World!' to stdout
	MOV EAX, 4                                      ; Loading 4 value into EAX register.
	MOV ECX, msg                                    ; Loading msg value into ECX register.
	MOV EDX, msg_size                               ; Loading msg_size value into EDX register.
	INT 0x80                                        ; Call software interrupt 0x80: SYSCALL
	MOV EAX, 1                                      ; Loading 1 value into EAX register.
	XOR EBX, EBX                                    ; Exclusive OR operation EBX and EBX using XOR
	INT 0x80                                        ; Call software interrupt 0x80: SYSCALL

print_string2:                                  ; Label print_string2 with 7 commands
	; Printing the string 'Hello, World!' to stdout
	MOV EAX, 4                                      ; Loading 4 value into EAX register.
	MOV ECX, msg2                                   ; Loading msg2 value into ECX register.
	MOV EDX, msg2_size                              ; Loading msg2_size value into EDX register.
	INT 0x80                                        ; Call software interrupt 0x80: SYSCALL
	MOV EAX, 1                                      ; Loading 1 value into EAX register.
	XOR EBX, EBX                                    ; Exclusive OR operation EBX and EBX using XOR
	INT 0x80                                        ; Call software interrupt 0x80: SYSCALL


;; Segment readable writeable in FASM is a definition of a segment of program data codes, where the attributes readable (the contents of the segment can be read) and writeable (program commands can both read codes and change their values) are specified for it.
segment readable writeable

msg db 'Hello, World!', 0                       ; Var msg (string)
msg_size = $-msg                                ; Var msg (string) length

msg2 db 'Hello, World!', 0                      ; Var msg2 (string)
msg2_size = $-msg2                              ; Var msg2 (string) length

Simple

from flexpasm import ASMProgram
from flexpasm.constants import LinuxInterrupts
from flexpasm.instructions.registers import get_registers
from flexpasm.instructions.segments import Label
from flexpasm.mnemonics import IntMnemonic, MovMnemonic, XorMnemonic
from flexpasm.settings import Settings


def main():
    settings = Settings(
        title="Example ASM Program",
        author="alexeev-prog",
        filename="example.asm",
        mode="64",
    )
    asmprogram = ASMProgram(settings, __name__)
    regs = get_registers(settings.mode)

    start_lbl = Label("start")

    start_lbl.add_instruction(MovMnemonic(regs.AX, 4))
    start_lbl.add_instruction(MovMnemonic(regs.CX, "message"))
    start_lbl.add_instruction(MovMnemonic(regs.DX, "message_size"))
    start_lbl.add_instruction(IntMnemonic(LinuxInterrupts.SYSCALL))
    start_lbl.add_instruction(MovMnemonic(regs.AX, 1))
    start_lbl.add_instruction(XorMnemonic(regs.BX, regs.BX))
    start_lbl.add_instruction(IntMnemonic(LinuxInterrupts.SYSCALL))

    asmprogram.add_label(start_lbl)
    asmprogram.main_rws.add_string("message", "Hello, World!")

    asmprogram.save_code()
    # asmprogram.restore_backup() # Restore backup


if __name__ == "__main__":
    main()
$ fasm example.asm example
$ ld example -o example
$ ./example

Hello, World!

Generated ASM code:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Author: alexeev-prog                                                                                               ;;
;; Example ASM Program                                                                                                ;;
;; Program generated by FLEXPASM (github.com/alexeev-pro/flexpasm)                                                    ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

format ELF64 executable 3;                      ; ELF64 EXECUTABLE
entry start                                     ; Set Start Entry


;; Segment readable executable in FASM is a directive for defining a section of code with readable and executable attributes.
segment readable executable

start:                                          ; Label start with 7 commands
    MOV RAX, 4                                      ; Loading 4 value into RAX register.
    MOV RCX, message                                ; Loading message value into RCX register.
    MOV RDX, message_size                           ; Loading message_size value into RDX register.
    INT 128                                         ; Call software interrupt 128: SYSCALL
    MOV RAX, 1                                      ; Loading 1 value into RAX register.
    MOV RBX, RBX                                    ; Exclusive OR operation RBX and RBX using XOR
    INT 128                                         ; Call software interrupt 128: SYSCALL


;; Segment readable writeable in FASM is a definition of a segment of program data codes, where the attributes readable (the contents of the segment can be read) and writeable (program commands can both read codes and change their values) are specified for it.
segment readable writeable

message db 'Hello, World!', 0xA                 ; Var message (string)
message_size = $-message                        ; Var message (string) length

(back to top)

💬 Support

If you encounter any issues or have questions about pyEchoNext, please:

(back to top)

🤝 Contributing

We welcome contributions from the community! If you'd like to help improve pyEchoNext, please check out the contributing guidelines to get started.

(back to top)

Specifications

Registers

Code from library:

class Registers16(Enum):
    AX = Register16("AX")
    BX = Register16("BX")
    CX = Register16("CX")
    DX = Register16("DX")
    SP = Register16("SP")
    BP = Register16("BP")
    SI = Register16("SI")
    DI = Register16("DI")

    r10 = Register16("10")
    r8 = Register16("8")
    r9 = Register16("9")


class Registers32(Enum):
    AX = Register32("EAX")
    BX = Register32("EBX")
    CX = Register32("ECX")
    DX = Register32("EDX")
    SP = Register32("ESP")
    BP = Register32("EBP")
    SI = Register32("ESI")
    DI = Register32("EDI")

    r10 = Register32("e10")
    r8 = Register32("e8")
    r9 = Register32("e9")


class Registers64(Enum):
    AX = Register64("RAX")
    BX = Register64("RBX")
    CX = Register64("RCX")
    DX = Register64("RDX")
    SP = Register64("RSP")
    BP = Register64("RBP")
    SI = Register64("RSI")
    DI = Register64("RDI")

    r10 = Register64("r10")
    r8 = Register64("r8")
    r9 = Register64("r9")


def get_registers(mode: str) -> Union[Register16, Register32, Registers64]:
    if mode == "16":
        return Registers16
    elif mode == "32":
        return Registers32
    elif mode == "64":
        return Registers64

BaseMnemonic

BaseMnemonic is the base abstract assembler mnemonic class. Assembly language mnemonics are a fundamental part of any program.

Module: flexpasm.mnemonics

Supported Mnemonics:

  • MovMnemonic
  • IntMnemonic
  • XorMnemonic
  • AddMnemonic
  • SubMnemonic
  • JmpMnemonic
  • IncMnemonic
  • MulMnemonic
  • DecMnemonic
  • ShrMnemonic
  • RorMnemonic
  • RolMnemonic
  • AndMnemonic
  • OrMnemonic
  • NotMnemonic
  • DivMnemonic
  • JeMnemonic
  • JneMnemonic
  • JgMnemonic
  • JLMnemonic
  • JgeMnemonic
  • JleMnemonic
  • CallMnemonic
  • RetMnemonic
  • PushMnemonic
  • PopMnemonic
  • XchgMnemonic
  • SwapMnemonic
  • IretMnemonic
  • CmpMnemonic

(back to top)

License

Distributed under the MIT License. See LICENSE for more information.

(back to top)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flexpasm-0.3.2.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flexpasm-0.3.2-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

Details for the file flexpasm-0.3.2.tar.gz.

File metadata

  • Download URL: flexpasm-0.3.2.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.2 Windows/11

File hashes

Hashes for flexpasm-0.3.2.tar.gz
Algorithm Hash digest
SHA256 ee83fe90a92376dcf498a9a21038b6d3bd9208342ea3ce3202b37b2f17049559
MD5 09ce5b4ef85d840d5f0178afdd1fa8a7
BLAKE2b-256 5115c7110dfb14d4cc7c1b331a213e589b451e19c923bb0a1b2ddd1afea74480

See more details on using hashes here.

File details

Details for the file flexpasm-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: flexpasm-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.2 Windows/11

File hashes

Hashes for flexpasm-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e0e7bb086282c1c98acd9c63fe16b60bff840d01dcc1c1f78eeff8b5bb8e8eb2
MD5 3ba102696d627e9c92345d55e905de74
BLAKE2b-256 5f015dd3459c579f6cb47c0c33dff0b999000215c2052da09f21de51ea99a99d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page