I have code that looks like:
mov dl, 05
mov dh, 05

I can combine these 2 commands into:
mov dx, 0505H

What I don't understand is why is it necessary for '0505' to be in hex. If I make the command:
mov dx, 0505
it does not work right. Actually, I think I understand why it does not work right in dec. but not why it does work in hex. Can someone explain this. I hope for details on this 1. I'm an idiot. Thank you.

Dani AI

Generated

Short answer: DH is the high byte of DX and DL is the low byte. When both byte registers hold the same small value, the 16-bit word they form is that high value times 256 plus the low value. That is why writing the word as a hex literal that encodes those two bytes matches the two separate byte moves. As pointed out, a bare numeric token without a hex marker is usually treated as decimal by the assembler, so the decimal literal does not equal the combined bytes.

A concise way to think about it:

dx = (dh << 8) | (dl & 0xFF);

Two hex digits represent one byte, so four hex digits represent the two bytes of a 16-bit word. Assemblers require a radix indicator (Intel-style assemblers often accept a trailing h; many others accept 0x), so always use the assembler's accepted notation when you mean hex.

Practical tips: check the assembler manual for literal syntax and any rules about leading zeros or letter-starting hex constants; ensure immediates fit the destination size (you cannot load a 16-bit immediate into an 8-bit register); use a debugger or assembler listing to view the actual numeric encoding if results look wrong. This is a common confusion and not a mistake in reasoning.

Recommended Answers

All 2 Replies

Hex is base16 (0-F) while Dec is base10 (0-9). If you do a 'mov dh,05' then the value of dx in decimals will not be 5*(10^2)=500 but 5*(16^2)=1280.

Thus, a 'mov dh, 05' and 'mov dl, 05' is in decimal 5*(16^2)+5, a 'mov dx, 0505h' is in decimal 5*(16^2)+5 as well but a mov dx, 0505' is in decimal 5*(10^2)+5.

Hope this makes sence.

Thank you. I think I have it straight in my head now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.