Click here to Skip to main content
Click here to Skip to main content

Interfacing VC++ and MASM x32 in both and x64: Some examples

, 1 Feb 2007
Rate this:
Please Sign up or sign in to vote.
An article on interfacing C++ with Assembler in X32 and X64.

Introduction

I have had, at many times, students and colleagues asking the question "how do I interface C++ with assembly language" specifically Visual Studio 2005 and the X64 platform (ML64.EXE). While assembly language is, for the most part, a language of the past, it does have its place in embedded systems, micro-controllers, etc. Moreover, it still remains a useful tool for teaching the hardware environment, addressing and some optimization techniques.

I put these applications together to demonstrate the calling conventions between C++ and assembly language in both the X32 and X64 platforms. Given the motivation to write this, I included code that might be considered useful, a Huffman encoder / decoder in both X32 and X64 assembly language using high-level C++ classes to encapsulate the calls to the assembly library functions. While we all know Huffman encoding / decoding (or could pick up a book and read about it), this article isn't about information theory. It is considered to be a set of tools that will assist those who are interested on how to interface between high-level languages and assembly language in addition to giving a little bit of education in writing optimized assembly language code, i.e. using the SIMD instruction set, avoiding processor stalls, loop unrolling, utilization of the machine word to access memory, hardware pre-fetching, when to cache, when not to cache, etc.

Using the Code

The code is self explanatory – I hope. There are a plethora of comments. It consists of three projects to the overall solution -- CHuffman.DLL, CTimer.DLL, TestHuffman.EXE. All projects have been developed for both the X32 and X64 platforms.

Chuffman.DLL contains the methods for the encoder and decoder – developed predominantly in assembly language.

CTimer.DLL is used for measuring the performance of the overall application and can be used to measure the performance at the function level if desired. Intel also markets an excellent profiler that integrates well with Visual Studio 2005 for function and instruction level profiling.

TestHuffman.EXE is a simple console application that allows testing of the code on the aforementioned platforms and does impart some important aspects regarding application performance such as aligned memory allocation, pre-allocation, etc

I am certain there are many ways to improve on this -- specifically in applying the use of the robust C++ optimizing compilers from Microsoft and / or Intel that can deliver unprecedented performance. In addition, there remain many tricks in assembly language that can achieve greater performance by reducing processor stalls, wait states, further loop optimization, etc. So feel free to experiment. I do hope this will be of benefit to at least a few.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

BadCoder444
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
QuestionThanks and a question Pinmemberbradr5-Aug-07 16:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 1 Feb 2007
Article Copyright 2007 by BadCoder444
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid