Numerical Systems, conversion of number
Binary, octal, decimal, hexadecimal numerical systems are the positional systems. A positional numerical system is one in which the value of numerals depends on their position in the number. The basis of positional numerical system is the count, after which is filled the next order of the number. Otherwise, the base of the numerical system is equal to the number of numerals, including zero, by which are written the numbers in this system.
Assembler allows the possibility of use of numbers in binary, octal, decimal or hexadecimal system. By default, the assembler considers all the numbers in the program to be decimal. You can explicitly indicate the base of the number by using tags (for MASM32 of version 11): b or y for binary numbers, o or q for octal numbers, d or t for decimal numbers, h for hexadecimal numbers. The tag is written at the end of the number in one word with it. If the number uses alphabetic characters (hexadecimal numbers), at the beginning is written a zero, according to assembler rules, the designations of numbers must begin with a numeral. Examples:.data var1 byte 00001111b ; 15 in binary representation var2 byte 00001111y ; 15 in binary representation var3 byte 17o ; 15 in octal representation var4 byte 17q ; 15 in octal representation var5 byte 15d ; 15 in decimal representation var6 byte 15t ; 15 in decimal representation var7 byte 0Fh ; 15 in hexadecimal representation
You can specify the type of numbers used in the program in the section of directives by the instruction like this:.RADIX base
in which the base is written by a decimal number. For example, according to the instruction:.RADIX 16
assembler will think that all numbers without tags in the program are hexadecimal numbers.
The existence of two variants of tags for binary and decimal numbers is due to considerations of compatibility of earlier versions of MASM32 with later versions. For hexadecimal numbers, there are not enough Arabic numerals, so the numerical string is supplemented with the letters:0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
The tags b and d coincide with the numerals of the hexadecimal number, which makes it impossible to use them with the directive .RADIX 16. The tag o is duplicated by the tag q because of the similarity of first with zero. For the above reasons, the tags y, q, t are preferred.
In a computer, all numbers are stored as sequences of zeros and ones. When we write some number into the text of the program, its conversion into the machine-readable representation is performed by assembler, and into the executable file this number will be written in a proper (binary) form. But we need to organize the output from the program in a format that the user understands. If we want to show a number to the user, we should do the following in the program: a) convert the number from a binary system to a decimal one; b) replace the resulting decimal number with its character, in other words, replace the number with the corresponding image, since the monitor displays just images, not numbers.
The conversion of the machine number into a given numerical system is performed by successive
division of this number and the receiving result on the base of the desired numerical system.
The remainder from the division is entered into the lower position, the quotient is again divided
on the base of the numerical system, the remainder is entered into the next position... This
continues until the result of the division will be zero. Suppose that you want to convert
the decimal number 250 to a hexadecimal representation:
The maximum number that can be written to a byte is 255. If according to the results of the program it is necessary to display on the screen the number in decimal format from a single-byte variable, you should divide this number by 10 no more than three times, because in the number 255 there are three decimal orders. In variables of type word (two bytes), the maximum value is 65 535, in the variables of the type double word (4 bytes) - 4 294 967 295. Accordingly, the numbers of word type in order to convert to the decimal format we divide by 10 no more than five times, of double word type we divide by 10 no more than ten times.
The codes of character-numbers are:
The fragment of the program which convert the byte variable into a decimal character form:
; . . . . . . . . . . .data divider byte 10 ; divider buffer_dec byte 3 dup (?) parametr byte 255 ; . . . . . . . . . . .code start: ; . . . . . . . . . . ; obtain the value of byte of "parameter" in the decimal character form mov AH, 0 ; zero the processor register AH mov AL, parametr ; copy the byte variable to the AL register DIV divider ; divide the content of the AX register by 10 mov buffer_dec, AH ; copy the remainder from the division into buffer_dec  mov AH, 0 ; zero the AH register DIV divider ; divide the content of the AX register by 10 mov buffer_dec, AH ; copy the remainder from the division into buffer_dec  mov buffer_dec, AL ; copy the quotient to buffer_dec  ADD buffer_dec, 48 ; add 48 - receive the character of numeral "2" ADD buffer_dec, 48 ; add 48 - receive the character of numeral "5" ADD buffer_dec, 48 ; add 48 - receive the character of numeral "5" ; . . . . . . . . . .
Send a comment
Table of Contents
|© Max Petrov||При использовании материалов ссылка на sadda.ru обязательна|