mnemonics package¶
Submodules¶
mnemonics.arithmetic module¶
Arithmetic mnemonics module for NGPASM.
This module provides arithmetic assembly instructions including addition, subtraction, multiplication, division, increment, and decrement.
- class mnemonics.arithmetic.AddMnemonic(*operands: Register | str | int, enable_comment: bool = True)[source]¶
Bases:
ArithmeticMnemonicADD instruction - adds source to destination.
This mnemonic performs addition operation: destination = destination + source. Requires exactly two operands.
Example
AddMnemonic(Register.EAX, Register.EBX) ; Adds EBX to EAX
- class mnemonics.arithmetic.ArithmeticCommentGenerator[source]¶
Bases:
CommentGeneratorSpecialized comment generator for arithmetic operations.
This generator creates context-aware comments for arithmetic instructions like ADD, SUB, MUL, DIV, INC, and DEC, using appropriate templates for each instruction type.
- _templates¶
Class variable mapping instruction names to comment templates. Templates use {dst} and {src} placeholders for operands.
- Type:
ClassVar[dict[str, str]]
- _abc_impl = <_abc._abc_data object>¶
- _templates: ClassVar[dict[str, str]] = {'add': 'Adding {src} to {dst}', 'dec': 'Decrementing {dst}', 'div': 'Dividing {dst} by {src}', 'inc': 'Incrementing {dst}', 'mul': 'Multiplying {dst} by {src}', 'sub': 'Subtracting {src} from {dst}'}¶
- generate(mnemonic_name: str, operands: list[Operand]) str[source]¶
Generate an arithmetic-specific comment for the instruction.
- Parameters:
mnemonic_name – Name of the arithmetic instruction.
operands – List of operands for the instruction.
- Returns:
A formatted comment string describing the arithmetic operation. Falls back to default comment generation if no template exists.
- class mnemonics.arithmetic.ArithmeticMnemonic(name: str, expected_operands: int, *operands: Register | str | int, enable_comment: bool = True)[source]¶
Bases:
BaseMnemonicBase class for arithmetic mnemonics with common behavior.
This class provides a foundation for all arithmetic instructions, automatically setting up the appropriate validator based on the expected number of operands and using arithmetic-specific comment generation.
- __init__(name: str, expected_operands: int, *operands: Register | str | int, enable_comment: bool = True) None[source]¶
Initialize an arithmetic mnemonic.
- Parameters:
name – The arithmetic instruction name (e.g., ‘add’, ‘sub’).
expected_operands – Number of operands required (1 or 2).
*operands – Variable number of instruction operands.
enable_comment – Whether to generate comments in output.
- Raises:
ValueError – If expected_operands is not 1/2, or if operand count mismatch.
- class mnemonics.arithmetic.DecMnemonic(*operands: Register | str | int, enable_comment: bool = True)[source]¶
Bases:
ArithmeticMnemonicDEC instruction - decrements register by 1.
This mnemonic performs decrement operation: operand = operand - 1. Requires exactly one operand.
Example
DecMnemonic(Register.EAX) ; Decrements EAX by 1
- class mnemonics.arithmetic.DivMnemonic(*operands: Register | str | int, enable_comment: bool = True)[source]¶
Bases:
ArithmeticMnemonicDIV instruction - divides destination by source.
This mnemonic performs division operation: destination = destination / source. Requires exactly two operands.
Example
DivMnemonic(Register.EAX, Register.EBX) ; Divides EAX by EBX
- class mnemonics.arithmetic.IncMnemonic(*operands: Register | str | int, enable_comment: bool = True)[source]¶
Bases:
ArithmeticMnemonicINC instruction - increments register by 1.
This mnemonic performs increment operation: operand = operand + 1. Requires exactly one operand.
Example
IncMnemonic(Register.EAX) ; Increments EAX by 1
- class mnemonics.arithmetic.MulMnemonic(*operands: Register | str | int, enable_comment: bool = True)[source]¶
Bases:
ArithmeticMnemonicMUL instruction - multiplies destination by source.
This mnemonic performs multiplication operation: destination = destination * source. Requires exactly two operands.
Example
MulMnemonic(Register.EAX, Register.EBX) ; Multiplies EAX by EBX
- class mnemonics.arithmetic.SubMnemonic(*operands: Register | str | int, enable_comment: bool = True)[source]¶
Bases:
ArithmeticMnemonicSUB instruction - subtracts source from destination.
This mnemonic performs subtraction operation: destination = destination - source. Requires exactly two operands.
Example
SubMnemonic(Register.EAX, Register.EBX) ; Subtracts EBX from EAX
mnemonics.base module¶
Base classes and utilities for assembly mnemonics.
This module provides the foundation for creating assembly language mnemonics with support for operand validation, comment generation, and instruction formatting.
- class mnemonics.base.BaseMnemonic(name: str, validator: OperandValidator, comment_generator: CommentGenerator = None, *operands: Register | str | int, enable_comment: bool = True)[source]¶
Bases:
objectBase class for assembly mnemonics following SOLID principles.
This class provides core functionality for constructing assembly instructions with flexible operand handling, validation strategies, and comment generation.
- name¶
Assembly instruction name (e.g., ‘mov’, ‘add’).
- operands¶
List of instruction operands wrapped as Operand objects.
- validator¶
Strategy for operand validation.
- comment_generator¶
Strategy for comment generation.
- enable_comment¶
Flag to control comment generation.
- custom_comment¶
Custom comment override.
- __init__(name: str, validator: OperandValidator, comment_generator: CommentGenerator = None, *operands: Register | str | int, enable_comment: bool = True) None[source]¶
Initialize a base mnemonic.
- Parameters:
name – Assembly instruction name.
validator – Strategy for validating operands.
comment_generator – Strategy for generating comments.
*operands – Variable number of instruction operands.
enable_comment – Whether to generate comments in output.
- Raises:
TypeError – If any operand has invalid type.
ValueError – If operands fail validation.
- _format_operands() str[source]¶
Format operands for instruction assembly.
- Returns:
Comma-separated string of operands.
- _validate_operand_types() None[source]¶
Validate operand types against allowed types.
Allowed types are Register, str, and int.
- Raises:
TypeError – If any operand has invalid type.
- property comment: str | None¶
Get the current comment for the instruction.
- Returns:
Current comment string or None if no custom comment is set.
- construct(indent: str = '') str[source]¶
Construct complete assembly instruction string.
This method generates the final assembly instruction with proper formatting, including indentation and optional comments.
- Parameters:
indent – Leading indentation for the instruction.
- Returns:
Formatted assembly instruction with optional comment.
- class mnemonics.base.CommentGenerator[source]¶
Bases:
ABCBase class for comment generation strategies.
- _abc_impl = <_abc._abc_data object>¶
- class mnemonics.base.DefaultCommentGenerator[source]¶
Bases:
CommentGeneratorDefault comment generation strategy.
- _abc_impl = <_abc._abc_data object>¶
- class mnemonics.base.OneOperandValidator(mnemonic_name: str)[source]¶
Bases:
OperandValidatorValidator for instructions requiring exactly 1 operand.
This validator ensures that instructions like INC, DEC, etc. receive the correct number of operands.
- mnemonic_name¶
Name of the mnemonic being validated.
- __init__(mnemonic_name: str) None[source]¶
Initialize one-operand validator.
- Parameters:
mnemonic_name – Name of the mnemonic for error messages.
- _abc_impl = <_abc._abc_data object>¶
- class mnemonics.base.Operand(value: Register | str | int)[source]¶
Bases:
objectRepresents a mnemonic operand.
This class wraps an operand value to provide consistent string representation and type handling across different operand types.
- value¶
The actual operand value which can be a Register, string, or integer.
- Type:
ngpasm.registers.Register | str | int
- __init__(value: Register | str | int) None¶
- value: Register | str | int¶
- class mnemonics.base.OperandValidator[source]¶
Bases:
ABCBase class for operand validation strategies.
- _abc_impl = <_abc._abc_data object>¶
- class mnemonics.base.TwoOperandValidator(mnemonic_name: str)[source]¶
Bases:
OperandValidatorValidator for instructions requiring exactly 2 operands.
This validator ensures that instructions like ADD, SUB, etc. receive the correct number of operands.
- mnemonic_name¶
Name of the mnemonic being validated.
- __init__(mnemonic_name: str) None[source]¶
Initialize two-operand validator.
- Parameters:
mnemonic_name – Name of the mnemonic for error messages.
- _abc_impl = <_abc._abc_data object>¶