Hi,

Am a self teaching Newbie Assembly programmer :) Still in the theoretical Stages.

My question is....

According to the book am reading

When you convert 254(decimal) to hexadecimal you get 0FEh.

And when you convert -2(decimal) to hexadecimal using two's complement you get OFEh.

When i do it manually i get FEh.

Is the zero before FEh always necessary or is it just another way of representing your code..?
Am fully aware that 0 in hexaecimal stands for 0000(base 2)

Thanks in advance......

Dani AI

Generated

Short answer: the leading 0 is a disambiguator, not part of the value. In MASM/TASM-style syntax, hex literals use the trailing h suffix. If the first hex digit would be A..F, the assembler might think you wrote a symbol name (label) instead of a number, so MASM requires you to start the literal with a decimal digit. That is why 0FEh assembles, while FEh would be parsed as an identifier. The value is the same either way. Microsoft’s MASM notes explicitly call this out: hexadecimal numbers must begin with 0–9 to distinguish them from symbols. Hexadecimal Numbers Must Begin With a Digit Between 0 and 9. For assemblers that support C-style prefixes (e.g., NASM), you can also write 0xFE; MASM itself uses the …h style and the leading-0 rule. How can I represent a hexadecimal value, such as FFFFFFBB, in x86 assembly programming?

On the signed value question, is right: -2 depends on operand size. As an 8-bit two’s-complement value it is FEh (1111 1110b); as 16-bit it is FFFEh; as 32-bit it is FFFFFFEh. Sign extension repeats the sign bit into the newly added high bits. On x86 you can control extension explicitly: MOVZX zero-extends a smaller operand into a larger register (useful when you mean 00FEh, not FFFEh), and MOVSX sign-extends (useful when you mean -2). See Intel’s instruction refs for details: MOVZX — Move With Zero-Extend and MOVSX — Move With Sign-Extension.

Practical tip tying it together: if you want the 8-bit pattern FE in a wider register without it becoming negative, zero-extend it; if you want it to represent -2, sign-extend it. That resolves the apparent contradiction between @Dante Wingates (syntax) and (value/semantics).

Recommended Answers

All 7 Replies

Hi,

Am a self teaching Newbie Assembly programmer :) Still in the theoretical Stages.

My question is....

According to the book am reading

When you convert 254(decimal) to hexadecimal you get 0FEh.

And when you convert -2(decimal) to hexadecimal using two's complement you get OFEh.

When i do it manually i get FEh.

Is the zero before FEh always necessary or is it just another way of representing your code..?
Am fully aware that 0 in hexaecimal stands for 0000(base 2)

Thanks in advance......

Well, the 0 of 0FEH is wrong if it should represent -2D. The most significant bit (msb) of an integer numbers always indicates whether the number is positive or negative. msb = 0 is for positive numbers, msb=1 stands for negative numbers, where negative whole numbers usually are in two's complement.

Let's code -2D in binary 2k and then in hex 16k:

2D = 10 --> extend it by some 0 to indicate true positive number: 0000 0010
now 1K: 1111 1101, 2k = 1k + 1 = 1111 1101 + 1 = 1111 1110B
-2D = 1111 1110B = FEH

If you add a 0, you get 0FEH, binary: 00001111 1110B what is a positive number, therefore wrong! If you want to extend a negative whole number, you must extend with the sign bit, that is for example FEH = FFFEH = FFFFFFFEH.

-- tesu

commented: Simple but informative +1

Now I see you are a assembly programmer. Here the leading 0 has a special meaning.

Consider this two instructions:

MOV AL, FEH
MOV AL, 0FEH

In first instruction the value of the variable FEH (it is an offset) is moved into reg. AL. Therefore such variable must exists in data segment, for example by definition: FEH DB 1

In second instruction the 8-bit constant FEH is moved into 8bit reg. AL.

Also consider these:

;; negative FEH should be loaded:
MOV AX, 0FEH    ;;; wrong, bcause AX is filled with: 000000FEH
MOV AX, 0FFFFFFFEH    ;;; correct, AX is filled with FFFFFFFEH

-- tesu

As far as I know the assembler can't tell if you're trying to declare a variable or referencing one because variables can start with any letter, including those used in base 16. If I understood your question right, you must put a zero before the hexadecimal value to say the assembler its a number... Normally becomes 0FEh... FE isnt the same as 0FE because the assembler will treat FE as a variable name and 0FE as a hexadecimal number...

Wow! Thanks You taught me a lot from those few lines.

1k and 2k stands for 0ne's and two's complement respectively right???

Is msb (most significant bit) same as high order bit (H.O bit) and is L.O bit also same as lsb (least significant bit)???

If you want to extend a negative whole number, you must extend with the sign bit, that is for example FEH = FFFEH = FFFFFFFEH.

i didn't get this last part most probably i will get it when i read further.
Thanks....

If I understood your question right, you must put a zero before the hexadecimal value to say the assembler its a number... Normally becomes 0FEh.

If you add a 0, you get 0FEH, binary: 00001111 1110B what is a positive number, therefore wrong! If you want to extend a negative whole number, you must extend with the sign bit

isn't this contradictory???

uuuuum Okay getting a little confused here let me check my books and ill get back to you.

Hi Madawar,

first i have to correct the following line:

>> MOV AX, 0FFFFFFFEH ;;; correct, AX is filled with FFFFFFFEH

is wrong because length of AX is 16bits. Therefore correct is:

MOV AX, 0FFFEH ;;; correct, AX is filled with FFFEH

1k is short for one's complement (in Bavaria;))
2k is two's complement

msb = h.o. bit, lsb = l.o. bit

As for sign extension: we have negative whole number FEH = 1111 1110H. If we want to extend it for some reason, extension must be done with sign bit, for example 11 1111 1110B. How to translate it back into hex? each hex figure consist of 4 bits, therefore further extension must be made: 11 11 1111 1110B = FFEH. For 80x86 all register are divisible by 4 so sign extension usually happens in portion of 4 bits that makes just a hex figure, FEH, FFFEH, FFFFFFFEH (for al, ax, eax).

Btw, for 80x86 there exists a special move instruction MOVSX for move with sign extension.

Dante's very good explanation didn't cover negative whole numbers too, so you can't hardly say there would be a contradiction.

-- tesu

Av understood now! thanks

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.