"Default User" <de***********@yahoo.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Bryan Parkoff wrote: I have two variables: "char A" and "short B". I can be able to convert
from A to B using explicit case conversion with no problem like "B =
short
(A);". Right now, I have two variables: "char T[6]" and "short A". T
has
an array of six elements. I desire to capture first element and second
element as two bytes into word as short.
The problem is that "A" captures only one element instead of two
elements. I have looked at machine language and I discovered that C++
Compiler selects the wrong instruction which it uses MOV EAX, BYTE PTR
[T]
instead of MOV EAX, WORD PTR [T].
Is there a way how I can fix an error in my source code using
explicit
case conversion? I tried to use dynamic_cast<>, but it has the same
result.
Here is my example code below.
Bryan Parkoff
int main(void)
{
unsigned char T[6] = { "Bryan" }; // I chose unsigned char for string
instead of char.
unsigned short A;
A = unsigned short (*T); // Should capture "Br"
return 0;
}
Seems like you are thinking that what you have will start copying at
the pointer value into the short. It doesn't.
If you did want to do that, you could use memcpy().
memcpy (&A, T, 2);
However, you have to be sure that's the byte order you want and all
that.
It would be helpful if you explained exactly what you are trying to do,
as copying two characters into a short isn't all that typical of an
operation.
Brian,
Thank you for the information. I am sure that memcpy() works, but I
want to use two x86 instruction that uses WORD instead of BYTE, but C++
Compiler only assigns BYTE instead of WORD. It is the way how C++ Compiler
works. I don't know if there is no solution so I am forced to use __asm
function. I do not wish to use left shift to move first byte to the left
before use "or" to capture second byte so two bytes becomes word. SHIFT and
ROTATE are hurt on Intel Pentium 4 because of eating about 7 clock cycles
rather than one clock cycle.
Variable: T is defined in BYTE ARRAY, but I force to tell C++ Compiler
to capture two bytes from BYTE ARRAY. Please look at my C++ source code
with my comment below.
// Example 1
int main(void)
{
unsigned char T[6] = { "Bryan" }; // I chose unsigned char for string
instead of char.
unsigned short A; // C++ Compiler assigns "movzx EAX, BYTE PTR [T]"
because T is defined as BYTE ARRAY.
A = unsigned short (*T); // Should capture "Br"
return 0;
}
// Example 2
int main(void)
{
unsigned char T[6] = { "Bryan" }; // I chose unsigned char for string
instead of char.
unsigned short A; // C++ Compiler assigns "movzx EAX, BYTE PTR [T]"
because T is defined as BYTE ARRAY.
// A = unsigned short (*T); // Should capture "Br"
__asm
{
movzx EAX, WORD PTR [T] // Remove "movzx EAX, BYTE PTR [T]" from C++
Compiler
mov WORD PTR [A], AX
}
return 0;
}
Bryan Parkoff