The Frazzled Programmer

Thursday, February 7, 2008

D Programming Tutorials: Inline Assembly

Inline Assembly

One of the really neat features of D is the ability to put assembly language directly inline with the rest of your code. For systems programmers, this could potentially save them a bit of time when optimizing their code. Below is a commented example of how inline assembly works in D:


// I based this sample off of a test case walter bright wrote. I added the 
// XOR operation and comments.
import std.stdio;
import std.string;
 
// use the version command to see if this is an x86 or x86/64 architecture
version(D_InlineAsm_X86)
{
version = runInineX86AssemblyTest;
}
else version(D_InlineAsm_X86_64)
{
version = runInineX86AssemblyTest;
}
else
{
static assert(0, "DSTRESS{XFAIL}: no inline x86 Assembly support");
}
 
// If x86 is the architecture, run the code in this block
version(runInineX86AssemblyTest)
{
ubyte b = 0xAA;
 
int main()
{
void* x = &b; // This is a pointer to b (an unsigned byte with the value 10101010)
void* y;  // This is a null void pointer
 
writeln("Addresses Before: x=" ~ toString(cast(long)x) ~ ", y=" ~ toString(cast(long)y));
writeln("Values Before: *x=" ~ toString(*cast(ubyte*)x) ~ ", *y=(null pointer exception)");
 
writeln(`
Using Assembly Language, XOR variable b (170) and value 0xFF (255).
10101010 xor 11111111 = 01010101 = 64 + 16 + 4 + 1 = 85
Then copy the address of b to the void pointer y.
`
);

static if(size_t.sizeof == 4) // 4 byte pointer (32 bit OS)
{
asm
{
mov EAX, offsetof b; // Move the address of b to the EAX register
xor [EAX], 255;   // Reverse the bits by doing an XOR on 0xFF.
mov y, EAX; // Copy the address in EAX to y
}
}
else static if(size_t.sizeof == 8) // 8 byte pointer (64 bit OS)
{
asm
{
mov RAX, offsetof b; // Move the address of b to the RAX register
xor [RAX], 255;   // Reverse the bits by doing an XOR on 0xFF.
mov y, RAX; // Copy the address in RAX to y
}
}
else
{
pragma(msg, "DSTRESS{ERROR}: unsupported pointer size");
static assert(0);
}
 
if(x != y)
{
assert(0);
}

writeln("Addresses After: x=" ~ toString(cast(long)x) ~ ", y=" ~ toString(cast(long)y));
writeln("Values After: *x=" ~ toString(*cast(ubyte*)x) ~ ", *y=" ~ toString(*cast(ubyte*)y));
 
getc(stdin);
return 0;
}
}

And here is the programs output:


Addresses Before:  x=4296832, y=0
Values Before:    *x=170, *y=(null pointer exception)

Using Assembly Language, XOR variable b (170) and value 0xFF (255). 
10101010  xor 11111111 = 01010101 = 64 + 16 + 4 + 1 = 85
Then copy the address of b to the void pointer y.

Addresses After:   x=4296832, y=4296832
Values After:     *x=85, *y=85

Tuesday, February 5, 2008

D To HTML Converter

I've just completed a D to HTML converter (written in D) as an educational exercise. It's an executable (run from the command line) that will traverse your directory structure and convert all ".d" files into ".d.html" files. Along with the html files comes an "index.html" file to navigate through all of your code in a browser.


Downloads: (version 0.2)

DToHtml.zip - Executable Only
DToHtmlWithSrc.zip - Executable and Source

Instructions:

  1. Place the d2html.exe file in the base directory that contains your D code.
  2. Run d2html.exe
  3. Open up the index.html file that is created in the same directory as the d2html executable.


Command Line Options:

----------------------------------------------------------------

Command line usage:

----------------------------------------------------------------



File or Recursive convertion Modes:

-file <filename>

-dir <directory> (default - current directory)



D Code or Program Output Modes:

-code (default - d code formatting)

-outfile (raw console program output)



Replacement Options: (Escape sequeces \r, \n, and \t work here)

-space <space string> (default "&nbsp;")

-tab <tab string> (default "&#009;")

-newline <newline string> (default "<br>")



Other Options:

-copyto <directory> (copy html files to target directory)

-? (console help - duh)



----------------------------------------------------------------

Examples:

----------------------------------------------------------------



REM This recusively converts all d programs to html in the current directory

d2html.exe



REM This recusively converts all d programs to html in the passed in directory

d2html.exe -dir "c:\pub\src\dprojects"



REM This converts the passed in file to html

d2html.exe -file "c:\pub\src\dprojects\sampleprogram.d"



REM This converts the passed in program output file to html

d2html.exe -outfile -file "c:\pub\src\dprojects\sampleprogram.output"



REM This recusively converts all d programs to html in the current directory, using the following character replacements for space, tab, and return chars.

d2html.exe -code -space " " -tab " " -newline "\r\n<br>"



REM This recusively converts all d programs to html in the passed in directory, then copies all of the html file tree to another directory

d2html.exe -dir "c:\pub\src\dprojects" -copyto "c:\pub\src\dprojects\HTML"



View Sample Code (Click here)