Git Product home page Git Product logo

py2asm's Introduction

py2asm

This program will help to generate complex asembly code.

There are 3 level structures in py2asm:

1. Blocks

It is a high level block which can contain instructions. Each block should be used with python with statement.

Some block are listed below:

Program

It is the main block to create assembly code structure.

import py2asm as asm

with asm.Program(model="small", stack=0x100): # model and stack values are optional
    # Rest of the program goes here
    pass

Labeled Block

It adds an label to set of instructions.

import py2asm as asm

with asm.Program():
    with asm.LabeledBlock('LABEL_NAME'):
        # Some Code
        pass

While Block

Creates While loop.

import py2asm as asm

with asm.Program():
    # exp could be any Jump Type (available at asm.JumpTypes)
    # or it could be any conditional expression (<, >, ==, <=, >=)
    # see also: examples/whileloop.py 
    with asm.While(exp):
        # Some Code
        pass

While Block

Similar to While block but places condition to the end of block.

import py2asm as asm

with asm.Program():
    # exp could be any Jump Type (available at asm.JumpTypes)
    # or it could be any conditional expression (<, >, ==, <=, >=)
    # see also: examples/whileloop.py 
    with asm.DoWhile(exp):
        # Some Code
        pass

Procedure

Creates a procedure

import py2asm as asm

with asm.Program():
    # Automatically adds RET at the end
    with asm.Procedure('PROC_NAME'):
        # Some Code
        pass

2. Functions

Functions are just collection of instructions for most repeated tasks.

InputChar

Single character input

import py2asm as asm

with asm.Program():
    asm.InputChar(echo=True) # can be false

it should outputs following

MOV      AH, 1
INT      21h

PrintStr

Prints string that ends with '$'

import py2asm as asm

with asm.Program():
    msg = asm.Variable('msg', asm.VariableType.BYTE, "Hello World!$")
    asm.PrintStr(msg)

it should outputs following

LEA      DX, msg
MOV      AH, 09h
INT      21h

PrintChar

Prints single char

import py2asm as asm

with asm.Program():
    asm.PrintChar('C')

it should outputs following

MOV      DL, 'C'
MOV      AH, 02h
INT      21h

PrintNumChar

Prints single char

import py2asm as asm

with asm.Program():
    asm.PrintNumChar(7)

it should outputs following

MOV      DL, 07h
ADD      DL, '0'
MOV      AH, 02h
INT      21h

PrintNum

Print any unsigned number by including emu8086.inc

import py2asm as asm

with asm.Program():
    asm.PrintNum(1234)

it should outputs following

include emu8086.inc

org 100h

.model small
.stack 0100h

.data


DEFINE_PRINT_NUM_UNS
DEFINE_PRINT_NUM

.code
MOV      AX, 04D2h
CALL     PRINT_NUM

PrintStrBuiltin

Prints string by including emu8086.inc

import py2asm as asm

with asm.Program():
    asm.PrintStrBuiltin("Hello World!$")

it should outputs following

include emu8086.inc

PRINT    "Hello World!$"

3. Raw Instructions:

Any raw instruction can be provided by using asm.Raw

import py2asm as asm

with asm.Program():
    asm.Raw(
        'MOV AH, 10', # Instruction provided in string
        asm.Mov(asm.Register.AH, 10), # Using py2asm instructions
    )

A bit about Expressions

Expression can be written as operator pipe like this

import py2asm as asm

with asm.Program():
    x = asm.Variable('x', asm.VariableType.WORD, 10)
    y = asm.Variable('y', asm.VariableType.WORD, 20)
    
    # equivalent to ((y / 2) - 4) * y 
    y.div(2).sub(4).mul(y)

will generate

MOV      BX, 02h
MOV      AX, y
DIV      BX

SUB      AX, 04h

MOV      BX, AX
MOV      AX, y
MUL      BX

But following should not be done

import py2asm as asm

with asm.Program():
    x = asm.Variable('x', asm.VariableType.WORD, 10)
    y = asm.Variable('y', asm.VariableType.WORD, 20)
    
    # equivalent to (x * 5) + (y * 6) 
    x.mul(5).add(y.mul(6)) # this will nt work

    (x * 5) + (y * 6) # this will also not work

Instead you can use this

import py2asm as asm

with asm.Program():
    x = asm.Variable('x', asm.VariableType.WORD, 10)
    y = asm.Variable('y', asm.VariableType.WORD, 20)

    (x * 5).mov(asm.Register.CX) # saved the result to CX so that
                                      # further calculation won;t change value of AX
    x <<= (y * 6) + asm.Register.CX
    # <<= is used to store result of expression in x variable again
    # ie MOV x, AX
    asm.PrintNum(x)

This will produce following

include emu8086.inc

org 100h

.model small
.stack 0100h

.data
x                DW   0Ah
y                DW   14h

DEFINE_PRINT_NUM_UNS
DEFINE_PRINT_NUM

.code
MOV      BX, x
MOV      AL, 05h
MUL      BX

MOV      CX, AX

MOV      BX, y
MOV      AL, 06h
MUL      BX

ADD      AX, CX

MOV      x, AX
CALL     PRINT_NUM

py2asm's People

Contributors

malikshahzad228 avatar nmanumr avatar uadnan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.