# function adapted from assembly routine

• 11-18-2003
curlious
function adapted from assembly routine
Who said you can't do two things at once, or at least in the same day. With that in mind I started learning assembly.

I took this assembly language routine and converted it to c++ and just wanted to show it off for critiquing. Here is the asm code, then the c++.

note the assembly language routine comes from Jeff Duntemans book Assembly Language Step by Step
Code:

```Byte2Str:     mov DI, AX                        ; Duplicate byte in DI     and DI, 000FH                  ; Mask out high 12 bits of DI     mov BX, Digits                  ; Load offset of digits into BX     mov AH, BYTE [BX+DI]      ; Load digit from lookup table     mov [SI+1], AH                  ; store digit into string     xor AH, AH                        ; trick to zero out AH     mov DI, AX                        ; move byte into DI     shr DI,4                              ; Shift high nyble of bite to low     mov AH, BYTE [BX+DI]        ; Load digit from digits table into string     mov [SI], AH                      ; store digit into string     ret```
The C++ with some adaptation
Code:

```// Hex2.cpp : Defines the entry point for the console application. #include <iostream> using std::cin; using std::cout; using std::endl; #include <string> using std::string; #include <stdlib.h> string byte_to_hexstring(short int); int main(int argc, char* argv[]) {         short int number;                 // fetch number must be short int         cout << "Enter a number [0-255]: ";         cin >> number;     cout << endl;             // display hexstring equivalent         cout << "In hex : " << byte_to_hexstring(number);         cout << endl;                 cin.ignore(1);                 system("PAUSE");         return 0; } string byte_to_hexstring(short int decimal) {                 string digits="0123456789abcdef";         string hexstring="  ";         int temp_digit=0;         int mask=15;                                // bit mask to eliminate high order nybl         temp_digit=decimal & mask;      // eliminate high order hex digit;                 hexstring[1]=digits[temp_digit];// use digits as look up table to refrence char                 temp_digit=decimal >> 4;        //move high order nyble first hexdigit to low order position         hexstring[0]=digits[temp_digit];//first char of string equals highorder nyble of decimal         return hexstring; }```