Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Asm code for splitting ubyte into low and high nibbles

Part of TutorialIntermediate

Description

Demonstrates using asm code with local variables

Example

/*
 *  Copyright (C) 2004 by Digital Mars, www.digitalmars.com
 *  Written by Lynn Allan
 *  This software is provided 'as-is' by a rusty asm-486 programmer.
 *  Released to public domain.
 *
 * Compile with: dmd test.d (uses dmd ver 0.102)
 */

import std.stdio;

void main ()
{
  ubyte x = 0x12;
  ubyte y = 0x9a;
  ubyte xLoNibble = (x & 0x0F);
  ubyte xHiNibble = (x >> 4);
  ubyte yLoNibble = (y & 0x0F);
  ubyte yHiNibble = (y >> 4);
  writefln("x: ", x, "  xLoNibble: ", xLoNibble, "  xHiNibble: ", xHiNibble);
  writefln("y: ", y, "  yLoNibble: ", yLoNibble, "  yHiNibble: ", yHiNibble);

  asm
  {
    movzx  BX,x;
    mov    AX,BX;
    and    BX,15;
    mov    xLoNibble,BL;
    shr    AX,4;
    mov    xHiNibble,AL;

    movzx  BX,y;
    mov    AX,BX;
    and    BX,15;
    mov    yLoNibble,BL;
    shr    AX,4;
    mov    yHiNibble,AL;
  }
  assert(xLoNibble == 2);
  assert(xHiNibble == 1);
  assert(yLoNibble == 10);
  assert(yHiNibble == 9);
  writefln("x: ", x, "  xLoNibble: ", xLoNibble, "  xHiNibble: ", xHiNibble);
  writefln("y: ", y, "  yLoNibble: ", yLoNibble, "  yHiNibble: ", yHiNibble);
}