Click here to Skip to main content
15,885,665 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I put my question all wrong earlier, allow me to explain.
I am struggling to write an assember for x86 processors. The x86 instruction set has different opcodes for the same instructions based on the operand sizes. What I want to know is that given a statement 'mov eax, 123' how do I figure out the the operands(byte, word, dword, qword...)? I'm using C++ as my implementation language.
Hope this makes a little more sense.
Thanks anyway.
Posted
Updated 20-Jun-15 11:33am
v2
Comments
CPallini 20-Jun-15 12:50pm    
How (the fresh Hell) could be 0xDEADBEEF a byte? Has your computer 32 bit bytes?
0xF4 20-Jun-15 13:02pm    
It was just an example.... And i did not notice...
Frankie-C 20-Jun-15 13:18pm    
Assembler use also DWORDs, QWORDs, etc.
But the point is another: what is the sense of checking something declared in 'C'?
In you sample you declared a variable of type 'int' which size, in 'C', is 4 bytes, so you will always get 4.
If your question is how can detect dimension of variables declared in an assembly module using another language, the answer is: you can't.
If the module is compiled there is no way to get this information, if you have sources you have to declare manually that variable with correct type in the other language.
If you want to check if a value fits in a type Carlo gave you an example of how you can do.
0xF4 20-Jun-15 13:51pm    
Assemblers are written in C (at least some of 'em) and they do it... I guess...
Frankie-C 20-Jun-15 14:52pm    
Yes sometimes they are written in 'C', but this doesn't mean that an assembler transforms an assembler source in 'C'. Often (or always) a 'C' compiler transforms 'C' code to assembler :)
The assembler get a symbolic description of instruction and implements it in machine code. If in assembler you reserve a memory operand then use it the assembler does:
1. Remember the address where your variable should be and associate it to variable name.
2. When found the operation that uses the variable address put the machine code for the operation replacing the address with the one saved before.
The machine code emitted is appropriate to handle the operand size (BYTE, WORD, DWORD, etc..)

If you meant 'How could I establish if an int value would fit in a byte?' then the answer is yes, you may do it. A simple approach could be the round-trip:
C++
unsigned int a = 0xDEADBEEF; // see the nvr3 comment
bool fit_in_a_byte = (a == static_cast< unsigned char >(a));
 
Share this answer
 
v2
Comments
nv3 20-Jun-15 17:33pm    
Or better cast to signed char, as int is signed as well? My 5.
CPallini 22-Jun-15 3:04am    
Yes, that is a good observation (and my mistake). I would prefer work with both unsigned, however.
OK now it makes a little bit more sense.
To write an assembler is not such an easy task, at least to encode correct machine code.
In some cases even if a full specified instruction is present there could be more than one legal encoding.
Anyway going back to your question consider that in assembly there can be explicit or implicit operands and operands sizes.
In case of the instruction:
mov eax, 123

It is not fully qualified, but we can get the implicit operand size from the explicit operand. This instruction moves in a 32bits register, the explicit operand, a value. This value must be of same size, so it is a 32bits integer. In 'C' language it would be an 'int'.
In almost all instructions you can extrapolate the operands size from explicit operand. In cases where you can't the assembler use meta qualifiers to make it clear.
I.e.
movsx eax, byte ptr[var];

The instruction movsx load a register with a sign extended value of different size, in our specific case the destination is the 32bits register eax, but the operand is a byte.

Some assemblers allows unqualified types also where there could be more than one operand size, but in that case it must be clearly stated in in the manual to which operand size the assembler will default...
 
Share this answer
 
v6
Comments
Sergey Alexandrovich Kryukov 19-Jul-15 1:16am    
5ed.
—SA
Frankie-C 19-Jul-15 6:15am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900