In: Computer Science
Starting with the following C++ program:
#include <iostream>
using namespace std;
void main ()
{
unsigned char c1;
unsigned char c2;
unsigned char c3;
unsigned char c4;
unsigned long i1 (0xaabbccee);
_asm
{
}
cout.flags (ios::hex);
cout << "results are " << (unsigned int) c1 << ", "
<< (unsigned int) c2 << ", "
<< (unsigned int) c3 << ", "
<< (unsigned int) c4 << endl;
}
Inside the block denoted by the _asm keyword, add code to move each byte of i1
into the unsigned chars c1 through c4 with the high order byte going into c1 and
the low order into c4.
#include <iostream>
using namespace std;
void main ()
{
unsigned char c1;
unsigned char c2;
unsigned char c3;
unsigned char c4;
unsigned long i1 (0xaabbccee);
unsigned long temp=i1;
//given number is hexadecimal, so num%16 gives
last one digit whereas num%256 gives last two digits.
c4=temp%256; //last two digits must be saved in c4
temp=temp/256; //we have to remove last two digits in temp to
proceed further
c3=temp%256; //3rd two digits must be saved in c3.
temp=temp/256; //we have to remove last two digits in temp to
proceed further
c2=temp%256; //2nd two digits must be saved in
c2.
temp=temp/256; //we have to remove last two digits in temp to
proceed further
c1=temp; //1st two digits must be saved in c1.
cout.flags (ios::hex);
cout << "results are " << (unsigned int) c1 << ", "
<< (unsigned int) c2 << ", "
<< (unsigned int) c3 << ", "
<< (unsigned int) c4 << endl;
}