Converting from hexadecimal to decimal in assembler. Converting decimal numbers to hexadecimal - Assembler. Reserving and initializing variables in memory

1. Basic number systems. Data declarations in assembler

Goal of the work: familiarizing students with number systems - binary, octal, hexadecimal; representation of data in computer memory, memory allocation directives.

Theoretical part

The smallest unit of information that can be stored in a computer is bit (eng. bit - bi nary digi t ), i.e. 0 or 1. A bit is an atom of information; it cannot be divided. Bits are grouped in groups of 8 to form a byte. The information manipulated by a computer is a string of binary numbers. From 8 bits, 256 combinations can be formed. These combinations are used to encode large and small letters, numbers, and special characters.

To measure information on a computer, the following quantities are used:

1 Kilobyte = 1 KB = 2 10 bytes = 1024 bytes;

1 Megabyte = 1 MB = 2 20 bytes = 1024 KB;

1 Gigabyte = 1 GB = 2 30 bytes = 1024 MB.

Number systems

A number system is a set of rules and numbers for representing numbers. For any positional number system, the number of digits to represent is equal to the base of the number system, e.g. binary system the base is the number 2, therefore, to represent numbers you need two digits 0 and 1, for the hexadecimal number system these are 0, 1, 2, ..., 9, A, B, C, D, E, F, where the letters correspond to the values ​​10, 11, 12, 13, 14 and 15 respectively.

To distinguish between number systems, a letter is placed at the end of the number: B for binary, Q for octal, D for decimal, and H for hexadecimal. For a decimal number, D is not required.

If a number is written in the b-ary number system in the form

Nr(b) = C n C n-1 C n-2 … C 2 C 1 C 0 , D 1 D 2 D 3 … ,

then in the decimal number system its value can be represented as the sum of digits multiplied by the base of the number system to a power equal to the position number of the digit in the number (numbering starts from 0, from right to left):

Nr(10) = C n *b n +C n-1 *b n-1 +…+C 2 *b 2 +C 1 *b 1 +C 0 *b 0 +D 1 *b -1 +D 2 * b –2 +D 3 *b –3 +...

For example:

Let two binary numbers 11b, 1100011b be given. Let's convert these numbers to the decimal number system:

11b =1*2 1 +1*2 0 =3;

11100011b = 1*2 7 +1*2 6 +1*2 5 +0*2 4 +0*2 3 +0*2 2 +1*2 1 +1*2 0 = 227.

Let's look at examples of converting an octal number to the decimal number system:

11q = 1*8 1 +1*8 0 = 9;

210q =2*8 2 +1*8 1 +0*8 0 =136.

Example of converting hexadecimal numbers to decimal:

11h = 1*16 1 +1*16 0 =17;

CA0h= C*16 2 +A*16 1 +0*16 0 = 3232

To convert numbers from the decimal system to binary or hexadecimal, integer division is used. The number is divided by the base of the number system until an indivisible remainder is obtained. The quotient obtained from division is again divided and the process ends when the quotient also becomes indivisible. The remainders obtained during division are written in reverse order. The diagram shows the conversion of the number 25 to the binary number system, as a result of which we obtain the number 11001b, as well as the conversion of the number 418 to the hexadecimal number system, as a result of which we obtain the number 1A2h, given that the number ten is A.

To convert numbers from the hexadecimal system to the binary system and vice versa, the following fact is used: each hexadecimal digit corresponds to a four-bit binary number and vice versa, as presented in the table. Therefore, when converting from the hexadecimal system to the binary system, it is necessary to write down its binary code for each hexadecimal digit, and when converting back, the binary number is divided from right to left into groups of four digits and the hexadecimal correspondence is written for them.

Table of correspondence between hexadecimal digits and binary numbers.

For example, let's convert the number 1FDh into binary representation:

1FDh = 0001-1111-1101b = 111111101b

Let's convert the binary number 1110100101b into hexadecimal representation: 0011-1010-0101b = 3A5.

Representation of integers in computer memory

The binary number system is used to represent information on a computer. To store integers, a strictly fixed number of bits is used: 8, 16, 32, 64. In n binary positions you can write a signed integer in the range from -2 n-1 to 2 n-1 -1. Positions are numbered from 0 to n-1 from right to left. For example, the number 67 in eight binary positions would be represented as 01000011b. Unsigned numbers can be represented in the range 0 to 2 n -1.

An integer can be stored in two's complement or two's complement form. To represent the sign of a number, a bit called the sign bit is used. It is in position n-1 and is the most significant bit of the number. For positive numbers this bit is zero, for negative numbers it is one.

Direct code used to store positive or unsigned numbers.

Additional code used for storage negative numbers. To obtain the representation of a number in complementary code, first the direct code of the modulus of the number is found, then its inverse code. The reverse code is obtained by inverting each digit in the binary representation of the number: 0 is converted to 1, and 1 to 0. At the last step, 1 is added to the reverse code.

For example, to represent the number -65 we have:

01000001b direct number code +65

10111110b return code

10111111b additional number code -65

Two's complement code is used to replace the operation of subtracting integers with an addition operation with the number represented in the two's complement code. In this case, the processor does not need to perform the operation of subtracting integers.

Data types

BYTE. This data type occupies 1 byte (8 bits). Using this type, you can encrypt a signed integer in the range from -128 to +127 or an unsigned integer in the range from 0 to 255, any ASCII character that is also encoded as an integer. Definition Directive – D.B.(Define Byte).

WORD. This data type occupies 2 bytes (16 bits). To a variable of this type You can place an integer in the range -32768 to +32767 or 0 to 65535, two ASCII characters, or a relative memory address of type near. In this case, writing to memory is done as follows: the low part of the number is located at the low address, and the high part is located at the high address. This is true for other data types as well. For example, if the hexadecimal integer number 1234h is located at address 1000h, then the low-order part 34h will be located at address 1000h, and 12h will be located at address 1001h. Definition Directive – DW(Define word).

DWORD– 4 bytes (2 words) can hold a 32-bit signed or unsigned integer, a floating point number, a 32-bit memory address, or 4 ASCII characters. When storing an address, the segment address is located in the high two bytes and the offset is in the low two bytes of memory. Definition Directive – DD(Define Double word).

QWORD– 8 bytes. Can be a signed or unsigned integer, a number, or a double-precision floating-point number. Definition Directive – DQ(Define Quad).

TEN-BYTES– 10 bytes. Used to store data in main memory or coprocessor. Can be a packed BCD number, an extended integer number, or an extended floating point number. Definition Directive - D.T.(Define Ten bytes).

The general syntax for defining data is:

< Name> < type> < listvalues>

< Name> < type> < number>dup (expression),

Where Name– identifier, type– one of the memory allocation directives discussed above, list of values– a list that can contain character or numeric constants. It may also contain the symbol ? , if the value is undefined, or address - a variable name or label, a string of ASCII characters enclosed in quotes or apostrophes. Directive dup specifies the repetition of the values ​​defined by the expression specified <число> once. Expression can be a constant, constants, arithmetic operators, a list of values, or a symbol ? , if the value is undefined.

For example,

var_a db 2 dup (0, 3 dup (1)) ; equivalent to var_a db 0,1,1,1,0,1,1,1 var_b db 1, 2, 3, ?, ? adr_a dw var_a adr_b3 dd var_b+3

Let's determine the size of memory allocated for each of the following variables:

m1 db 4, 5, 1, 6; 4*1=4 bytes m2 db “xzyqw” ; 5*1=5 bytes m3 dw 12 dup(?) ; 12*2=24 bytes m4 dd 345h , 234h ; 2*4=8 bytes

m1 db 4, 5, 1, 6 ; 4*1=4 bytes m2 db “xzyqw” ; 5*1=5 bytes m3 dw 12 dup(?) ; 12*2=24 bytes m4 dd 345h, 234h ; 2*4=8 bytes

The total number of bytes allocated by these directives is 41 bytes. Variable m1 is located at relative address 00h, m2 – 04h, m3 – 09h, and m4 – 021h.

Individual tasks:

1. Convert decimal numbers to binary, hexadecimal and octal number systems:

1)42;31;113 5 )46;35;119 9 ) 49;30;103 13 )29;37;97
2 )45;81;89 6)66;25;110 10 )19;53;101 14 )21;87;98
3 )12;38;118 7 )17;63; 96 11)34;50;107 1 5) 28;45;130
4 )11;43;67 8 )13;69;88 1 2 )14;70;99 16)15;72;100

2. Convert hexadecimal numbers to binary:

1)A45;12;56B 5)7C;72EB;31DB 9)34A;6AB;9AD 13)2B9;6F1;81B
2)1EF3;5AB;46F 6)3EB;4D8;A61 10)5AB;79F;AB8 14)7CD;2A1;B53
3)A56;5E9;CDE 7)6A3;9D0;8BE 11)9A;4DE;EF7 15)10B;87F;CD9
4)3B8;DE1;BAE 8)BC;7F9;78A 12)AB;8E4;C17 16)38E;9C7;B89

3. Convert binary numbers to octal and hexadecimal number systems:

1) 00101011; 00100110;
01110011
5 ) 11110010; 01101010;
11111100;
9 ) 10000101; 11100010;
11001011
13 ) 00011101; 11111001;
00111101
2 ) 01100001; 01101110;
11110011
6) 00110110; 00111011;
10001100
10 ) 00011101; 01010110;
10110010
14 ) 00011100; 01001100;
01101110
3) 11100100; 01011100; 11000001 7 ) 11010010; 01001100; 11000111 11) 11100010; 10100001; 10001110 1 5 ) 10101001; 11010101; 111001100
4 ) 00001111; 10100101; 10010001 8 ) 11100000 11111000; 01000011 1 2 ) 10100101; 01101100; 11100001 16) 11100111; 01100101; 10110010;

4. Present the following numbers in complementary code:

1)-42;-31;-96 5)-46;-35;-94 9) -49;-30;-103 13)-29;-37;-97
2)-52;-41;-93 6)-66;-25;-85 10)-19;-53 ; -101 14)-21;-87;-98
3)-12;-38;-93 7)-17;-63;-99 11)-34;-50;-94 15)-28;-45;-95
4)-11;-43;-67 8)-13;-69;-88 12)-14;-70;-99 16)-15;-72;-89

5. The following definitions of variables are given:

1) a db 45,16,76,-6
bdb "abcd"
cdw 15 dup(0),3,3
d dd 345h
2) add 2.24
b db “aaa”,-8.23h,11101b
c db 6 dup(0), 45, 6
d dw -7.4Dh.8 dup(0)
3) a db “Salut”,10,13
b db -16,-20,13h,2 dup(0)
c dw 62.34,-15
d dd 456C9h,4567
4) a dd 92.45h,90,-54,-67
b db 10 dup(‘$’),10.13
c db “amdto”,10,13,’$’
d dw 5 dup(?),7,-80h
5) a db “lucrarea_1”,10,13
b db 2 dup(0)
c dw 38,-15,78,41,12
d dd 678EFh,3489,456
6) a db 12.24,”sss”
b db “ab”,-8.23h
c dd 6 dup(0),45
d dw -7.5 dup(0)
7) a db 35.53
b db 10 dup(‘ ’),10,13,“$”
c dw 5 dup(0)
d dd 555h
8) a db 34,6,3,-8,-2
b db “Hello”,‘$’
c dw 6 dup(0),‘$’,10,13
d dw -68.46h,7 dup(0)
9) a db 45.16
b db 5 dup(?),10,13,“$”
c dw 55 dup(0)
d dd 34567h
10) a db 76,87,92,45h
b db 20 dup(‘$’),10.13
c db “qwert”
d dw 10 dup(0)
11) add 78,34,67
b db “Resultat”,‘$’
c db 16 dup(0),‘$’,10,13
12) a db 73,74,75,77,78,-67
b db 15 dup(‘?’),10,13
cdd 777h
13) a db 24.76,-56
b db “abc”,11101b
c dd 45.4 dup(?)
d dw 4 dup(0),8,3
14) a db “testul_nr_2”,13,10
b db -18,-22,18h,2 dup(0)
c dw 81,-16,44,18
d dd 568ABh
15) add 87.45h,-9
b db 10 dup(?)
c db “test_1$”
d dw 4 dup(0),2,7
16) a db “Matematica”,10,13
b db 10,20h,2 dup(0)
c dw 60,30,-10,-20,-50
d dd 56789Bh

a) determine how many bytes are allocated by these directives;
b) determine the addresses where each of the variables is located.


Binary, octal, decimal, and hexadecimal number systems are positional. A positional number system is one in which the value of a digit depends on its position in the number; the positions of the digits in the number are called orders or digits. The basis of the positional number system is the count, upon reaching which the next order of the number is filled. Otherwise, the base of a number system is equal to the number of digits, including zero, that numbers are written in this system.

The assembler allows the use of numbers in binary, octal, decimal or hexadecimal. By default, the assembler treats all numbers appearing in the program as decimal. You can explicitly indicate the base of a number using tags (for MASM32 version 11): b or y - for binary numbers; o or q - for octal; d or t - for decimal numbers; h - for hexadecimal numbers. The tag is written at the end of the number, together with the number. If alphabetic characters (hexadecimal numbers) are used in a number, a zero is written at the beginning - according to assembler rules, number designations must begin with a number. For example:

.data var1 byte 00001111b ; 15 in binary representation var2 byte 00001111y ; 15 in binary representation var3 byte 17o ; 15 in octal notation var4 byte 17q ; 15 in octal notation var5 byte 15d ; 15 in decimal notation var6 byte 15t ; 15 in decimal notation var7 byte 0Fh ; 15 in hexadecimal

You can set the type of numbers used in the program in the directives section with instructions like

.RADIX (base)

in which the number base is indicated as a decimal number. For example, according to the instructions

.RADIX 16

The existence of two variants of tags for binary and decimal numbers is due to compatibility considerations between earlier versions of MASM32, which did not have the ability to write numbers in hexadecimal format, with later versions. For hexadecimal numbers, Arabic numerals are not enough, so the number series is supplemented with letters:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

It is easy to see that the tags from earlier versions of MASM32 - b and d - are the same as hexadecimal digits, which makes them impossible to use with the .RADIX 16 directive. The o tag is duplicated by the q tag due to the similarity of the first one to zero. For these reasons, the preferred tags are y, q, t.

In a computer, all numbers are stored as sequences of zeros and ones. When we write any number into the program text, its translation into a machine-readable representation is carried out by the assembler, and in executable file this number will be written in the proper (binary) form. But we ourselves must organize the output of a number from the program in a format that is already understandable to the user, which means that if we need to show the user a number, then we must do the following in the program: a) convert the number from the binary system to the decimal system - from a machine representation to a human one; b) replace the resulting decimal number with its symbol, that is, with the corresponding picture, since the monitor displays pictures, not numbers.

The translation of a machine number into a given number system is carried out by sequentially dividing this number and the resulting result by the base of the desired number system. The remainder of the division is entered into the least significant digit, the quotient is again divided by the base of the number system, the remainder is entered into the next digit - and so on until the division reaches zero. Let's say you want to convert the decimal number 250 to hexadecimal:
250 / 16 = 15 , remainder = A (10),
15 / 16 = 0, remainder = F (15),
thus 250 (decimal) = FA (hexadecimal).

The maximum number that can be written to a byte is 255. If, based on the results of the program, it is necessary to display a number in decimal format from a one-byte variable, this number should be divided by 10 no more than three times - in 255 three decimal orders. IN type variables word (two bytes) the maximum value is 65,535, in variables of type double word (four bytes) - 4,294,967,295. Accordingly, word numbers for conversion to decimal format are divided by 10 no more than five times, double word -not more than ten times.

The character codes are: "0" - 48; "1" - 49; "2" - 50; "3" - 51; "4" - 52; "5" - 53; "6" - 54; "7" - 55; "8" - 56; "9" - 57. Obviously, to convert the value of a number into symbolic form, it is enough to add 48 to it.

A program fragment that converts a byte variable into decimal symbolic form:

.data divider byte 10 ; divisor buffer_dec byte 3 dup (?) parametr byte 255 ... .code start: ... ; get the values ​​of the parametr byte in decimal symbolic form mov AH, 0 ; reset AH mov AL, parameter; copy the byte variable to AL DIV divider ; divide AX by 10 mov buffer_dec, AH ; copy the remainder of the division into buffer_dec mov AH, 0 ; reset AH DIV divider ; divide AX by 10 mov buffer_dec, AH ; copy the remainder of the division into buffer_dec mov buffer_dec, AL ; copy the quotient to buffer_dec ADD buffer_dec, 48 ; add 48 - we get the number symbol "2" ADD buffer_dec, 48 ; add 48 - we get the number symbol "5" ADD buffer_dec, 48 ; add 48 - we get the number symbol "5"

Add a comment

Hexadecimal number system(also known as hexadecimal code) is a positional number system with an integer base of 16. The term hex (pronounced hex, short for English hexadecimal) is also sometimes used in the literature. The digits of this number system are usually used in Arabic numerals 0-9, as well as the first characters of the Latin alphabet A-F. The letters correspond to the following decimal values:

  • * A -10;
  • *B—11;
  • *C—12;
  • * D -13;
  • * E - 14;
  • * F - 15.

Thus, ten Arabic numerals, coupled with six Latin letters, make up the sixteen digits of the system.

By the way, on our website you can convert any text into decimal, hexadecimal, binary code using the Online Code Calculator.

Application. Hex code widely used in low-level programming as well as in various computer reference documents. The popularity of the system is justified by architectural solutions modern computers: They have a byte (consisting of eight bits) as the minimum unit of information - and the value of a byte is conveniently written using two hexadecimal digits. The byte value can range from #00 to #FF (0 to 255 in decimal notation) - in other words, using hexadecimal code, you can write any state of the byte, while there are no “extra” digits not used in the recording.

Encoded Unicode Four hexadecimal digits are used to record the character number. The RGB color notation (Red, Green, Blue) also often uses hexadecimal code (for example, #FF0000 is a bright red color notation).

A method for writing hexadecimal code.

Mathematical way of writing. In mathematical notation, the base of the system is written in decimal form as a subscript to the right of the number. The decimal notation of the number 3032 can be written as 3032 10, in hexadecimal notation given number will have the entry BD8 16.

In the syntax of programming languages. The syntax of different programming languages ​​sets differently the format for writing a number using hexadecimal code:

* The syntax of some varieties of assembly language uses the Latin letter “h”, which is placed to the right of the number, for example: 20Dh. If the number starts with Latin letter, then a zero is placed in front of it, for example: 0A0Bh. This is done in order to distinguish values ​​using constants from constants. hexadecimal code;

* In other varieties of assembler, as well as in Pascal (and its variants, such as Delphi) and some Basic dialects, the prefix “$” is used: $A15;

* In HTML markup language, as well as in cascading CSS files, to specify the color in RGB format with hexadecimal notation, the prefix “#” is used: #00DC00.

How to convert hexadecimal code to another system?

Convert from hexadecimal to decimal. To perform a conversion operation from the hexadecimal system to the decimal system, you need to represent the original number as the sum of the products of the digits in the digits of the hexadecimal number and the power of the base.

Binary SS

hex SS

For example, you need to translate the hexadecimal number A14: it has three digits. Using the rule, we write it as a sum of powers with a base of 16:

A14 16 = 10.16 2 + 1.16 1 + 4.16 0 = 10.256 + 1.16 + 4.1 = 2560 + 16 + 4 = 2580 10

Converting numbers from binary to hexadecimal and vice versa.

A notebook table is used for translation. To convert a number from binary to decimal, you need to split it into separate tetrads from right to left, and then, using a table, replace each tetrad with the corresponding hexadecimal digit. Moreover, if the number of digits is not a multiple of four, then it is necessary to add the corresponding number of zeros to the right of the number so that the total number of binary digits becomes a multiple of four.

Table of notebooks for translation.

To convert from hexadecimal to binary, you need to perform the reverse operation: replace each digit with a tetrad from the table.

Binary SS

Octal SS

Example conversion from hexadecimal to binary: A5E 16 = 1010 0101 1110 = 101001011110 2

Example conversion from binary to hexadecimal: 111100111 2 = 0001 1110 0111 = 1E7 16

In this example, the number of digits in the original binary number was not four (9), so leading zeros were added for a total number of digits of 12.

Automatic translation. A quick conversion from the hexadecimal number system to one of the three popular systems (binary, octal and decimal), as well as the reverse conversion, can be performed using a standard calculator included with Windows OS. Open the calculator, select View -> Programmer from the menu. In this mode, you can set the number system used in this moment(see menu on the left: Hex, Dec, Oct, Bin). In this case, changing the current number system automatically produces a translation.

Hello! There is this line:

Var BD 2,2,3,3,4,4; 223344 decimal 6-digit number in unpacked format with increased precision (ASCII format)

How can I convert this number 223344 into hexadecimal number? I found this code that converts from single-digit and two-digit decimal numbers to sixteen-digit numbers:

; Var. 17. Rear 1. Write a subroutine to convert some array of decimals; ASCII bytes. to an array of BCD bytes. Use this subroutine to process two arrays; Use the stack to pass parameters. Name Program ; Description of constants; Description of variables Data segment x1 db 2h db 1h db 3h db 1h db 4h db 1h ; numbers in ASCII format y1 db 3 dup (?) ; array in BCD format x2 db 8h ;98 db 9h db 5h ;95 db 9h db 7h ;87 db 8h db 2h ;92 db 9h ; array dec. ASCII bytes Y2 db 4 dup (?) ; array dec. BCD bytes Data ends Stack SEGMENT DW 16 dup(?) StkTOp LABEL word Stack ends Code SEGMENT ASSUME CS: Code, DS: Data, ES: Data, SS: Stack ; The subroutines abc proc push bp mov bp, sp are described here. snap to the top of the stack mov cx, ; reading parameters from the stack (number of numbers) mov di, ; address of variable Y1 mov si, ; address of variable X1 M1: mov al, +1 ; load the first number in ASCII format shl ax, 1 ; shift by 4 digits to the right shl ax, 1 shl ax ,1 shl ax, 1 or al, ;restore the number that you remembered mov , al ; write the number in BCD format inc si ; move to another number inc si inc di ; modify the BCD address - numbers dec cx ; reduce the number of numbers jnz M1 ; if they haven’t run out, then go to M1 pop BP ret 6 abc endp ; Main program Start: mov AX, Data mov DS, AX mov ES, AX mov Ax, Stack mov SS, AX mov SP, offset StkTop ; load the parameters onto the stack according to the task conditions mov ax, offset x1 ; load the address of the first array push ax mov ax, offset Y1 ; load the address of the result push ax mov ax, 3 ; number of numbers push ax call abc ; subroutine call mov ax, offset x2 ; load the address of the second array push ax mov ax, offset Y2 ; load the address of the result push ax mov ax, 4 ; number of numbers push ax call abc ; calling the subroutine code ends end start

This article is mostly for complete beginners. If you are well versed in number systems, you can only pay attention to the syntax features of the FASM assembler at the end of the article.

In fact, the processor only works with binary numbers consisting of ones and zeros :) All data and commands of any program are stored and processed in the form of binary numbers. However, binary notation of numbers is too cumbersome and inconvenient for humans, so assembly language programs also use other number systems: decimal, hexadecimal and octal.

A little theory

First of all, let's figure out what the difference is between number systems. Any decimal number can be represented as follows:

123 10 = 1 10 2 + 2 10 1 + 3 10 0

The subscript below indicates that it is a decimal number. The digit of each digit is multiplied by 10 to a power equal to the digit number, if counted from zero from right to left. More generally:

abc r= a r 2 + b r 1 + c r 0 ,

where a, b and c are some numbers, and r is the base of the number system. For decimal system r= 10, for binary - r= 2, for ternary r= 3, etc. For example, the same number in other systems:

443 5 = 4 5 2 + 4 5 1 + 3 5 0 = 4 25 + 4 5 + 3 1 = 123 10 (quinary system)

173 8 = 1 8 2 + 7 8 1 + 3 8 0 = 1 64 + 7 8 + 3 1 = 123 10 (octal system)

1111011 2 = 1·2 6 + 1·2 5 + 1·2 4 + 1·2 3 + 0·2 2 + 1·2 1 + 1·2 0 = 1·64 + 1·32 + 1·16 + 1 8 + 0 4 + 1 2 + 1 1 = 123 10 (binary)

Hexadecimal system

In the hexadecimal system, the letters A = 10, B = 11, C = 12, D = 13, E = 14, F = 15 are used to denote numbers greater than 9. For example:

C7 16 = 12 16 1 + 7 16 0 = 12 16 + 7 1 = 199 10

The convenience of the hexadecimal system is that it is very easy to convert binary numbers into it (and in the opposite direction too). Four digits of a binary number (tetrad) are represented by one digit of hexadecimal. To translate, simply break the number into groups of 4 bits and replace each tetrad with the corresponding hexadecimal digit.

Binary
tetrad
Hexadecimal
number
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9
1010 A
1011 B
1100 C
1101 D
1110 E
1111 F

To write one byte, only 2 hexadecimal digits are required:

0101 1011 2 = 5B 16

0110 0000 2 = 60 16

1111 1111 2 = FF 16

Octal system

Octal is also useful for representing binary numbers, although it is much less commonly used than hexadecimal. To quickly translate, you need to divide the binary number into groups of 3 digits (triplets or triads).

Binary
triad
Octal
number
000 0
001 1
010 2
011 3
100 4
101 5
110 6
111 7

For example: 001 110 101 2 = 165 8

FASM assembler syntax

By default, the number in the program is perceived by the assembler as decimal. To denote a binary number, you need to add a symbol to the end of it 'b'. An octal number is indicated similarly using the symbol 'o'. To write a hexadecimal number, FASM supports 3 notation forms:

  • symbols are written before the number '0x'(as in C/C++);
  • a symbol is written before the number ’$’ (as in Pascal);
  • a symbol is written after the number 'h'. If a hexadecimal number begins with a letter, you must add a leading zero (otherwise it is not clear whether it is a number or a label name).

This syntax is used both in data declarations and in commands. Here are examples of writing numbers in all four systems:

mov ax, 537 ;Decimal system movbl, 11010001b ;Binary system mov ch, 57o ;Octal system mov dl , $ C2 ;\ mov si , 0x013A ; \mov ah, 18h ; / Hexadecimal system mov al , 0FFh ;/ mov al , FFh ;Error!

mov ax,537 ;Decimal system mov bl,11010001b ;Binary system mov ch,57o ;Octal system mov dl,$C2 ;\ mov si,0x013A ; \mov ah,18h; / Hexadecimal system mov al,0FFh ;/ mov al,FFh ;Error!