Click here to Skip to main content
15,886,362 members
Articles / Desktop Programming / MFC
Article

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

Rate me:
Please Sign up or sign in to vote.
2.59/5 (6 votes)
1 Feb 20072 min read 38.5K   831   17   1
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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThanks and a question Pin
bradr5-Aug-07 15:32
bradr5-Aug-07 15:32 

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

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