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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.