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

Changes from Version 1 of SplittingUbyteWithAsmExample

Show
Ignore:
Author:
jcc7 (IP: 68.97.93.38)
Timestamp:
11/14/05 03:40:05 (18 years ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • SplittingUbyteWithAsmExample

    v0 v1  
     1= Asm code for splitting ubyte into low and high nibbles = 
     2 
     3''Part of'' TutorialIntermediate 
     4 
     5== Description == 
     6 
     7Demonstrates using asm code with local variables 
     8 
     9== Example == 
     10 
     11{{{ 
     12#!d 
     13/* 
     14 *  Copyright (C) 2004 by Digital Mars, www.digitalmars.com 
     15 *  Written by Lynn Allan 
     16 *  This software is provided 'as-is' by a rusty asm-486 programmer. 
     17 *  Released to public domain. 
     18 * 
     19 * Compile with: dmd test.d (uses dmd ver 0.102) 
     20 */ 
     21 
     22import std.stdio; 
     23 
     24void main () 
     25{ 
     26  ubyte x = 0x12; 
     27  ubyte y = 0x9a; 
     28  ubyte xLoNibble = (x & 0x0F); 
     29  ubyte xHiNibble = (x >> 4); 
     30  ubyte yLoNibble = (y & 0x0F); 
     31  ubyte yHiNibble = (y >> 4); 
     32  writefln("x: ", x, "  xLoNibble: ", xLoNibble, "  xHiNibble: ", xHiNibble); 
     33  writefln("y: ", y, "  yLoNibble: ", yLoNibble, "  yHiNibble: ", yHiNibble); 
     34 
     35  asm 
     36  { 
     37    movzx  BX,x; 
     38    mov    AX,BX; 
     39    and    BX,15; 
     40    mov    xLoNibble,BL; 
     41    shr    AX,4; 
     42    mov    xHiNibble,AL; 
     43 
     44    movzx  BX,y; 
     45    mov    AX,BX; 
     46    and    BX,15; 
     47    mov    yLoNibble,BL; 
     48    shr    AX,4; 
     49    mov    yHiNibble,AL; 
     50  } 
     51  assert(xLoNibble == 2); 
     52  assert(xHiNibble == 1); 
     53  assert(yLoNibble == 10); 
     54  assert(yHiNibble == 9); 
     55  writefln("x: ", x, "  xLoNibble: ", xLoNibble, "  xHiNibble: ", xHiNibble); 
     56  writefln("y: ", y, "  yLoNibble: ", yLoNibble, "  yHiNibble: ", yHiNibble); 
     57} 
     58}}} 
     59 
     60== Source == 
     61 
     62|| Link || http://www.dsource.org/tutorials/index.php?show_example=127 || 
     63|| Posted by || Anonymous || 
     64|| Date/Time || Mon Oct 4, 2004 10:47 am ||