How to set up smartphones and PCs. Informational portal

Hexadecimal number system. Binary octal hexadecimal number system

To write programs in assembler, you need to understand the hexadecimal number system. There is nothing complicated in it. We use the decimal system in our lives. I am sure that you all know it, so I will try to explain the hexadecimal system by drawing an analogy with the decimal.

So, in the decimal system, if we add zero to the right of any number, then this number will increase by 10 times. For example: 1 x 10 = 10; 10 x 10 = 100; 100 x 10 = 1000 etc. In this system, we use numbers from 0 to 9, i.e. ten different digits (in fact, that's why it is called decimal).

In the hexadecimal system, we use, respectively, sixteen "digits". I specifically wrote the word "numbers" in quotation marks, because it uses more than just numbers. And how is it really? I explain: from 0 to 9 we count in the same way as in decimal, but then it will be like this: A, B, C, D, E, F. The number F, no matter how difficult count, it will be equal to 15 in the decimal system (see Table 1).

Decimal number

Hexadecimal number

Table 1. Decimal and hexadecimal systems.

Thus, if we add zero to the right to any number in the hexadecimal system, then this number will increase in16 once.

Example 1: 1 x 16 = 10; 10 x 16 = 100; 100 x 16 = 1000 etc.

Were you able to distinguish hexadecimal from decimal in Example 1? And from this row: 10, 12, 45, 64, 12, 8, 19? It can be either hexadecimal or decimal. In order to avoid confusion, and the computer could unambiguously distinguish one number from another, it is customary in Assembler to put the symbol h or H after the hexadecimal number ( H is short for English. hexadecimal (hexadecimal). For short, it is sometimes called simply hex ) . And after the decimal, do not put anything. Because numbers from 0 to 9 in both systems have the same meaning, then the numbers written as 5 and 5h are the same.

That. Example 1 (see above) would be more correct to write as follows: 1 x 16 = 10h; 10h x 16 = 100h; 100h x 16 = 1000h. Or like this: 1h x 10h = 10h; 10h x 10h = 100h; 100h x 10h = 1000h.

Why the hexadecimal system is needed, we will consider in subsequent issues. For now, for our example program, which will be discussed below, we need to know about the existence of hexadecimal numbers.

So let's sum it up. The hexadecimal number system consists of 10 digits (from 0 to 9) and 6 letters of the Latin alphabet (A, B, C, D, E, F). If we add zero to the right of any number in the hexadecimal system, then this number will increase by16 once. It is very important to understand this topic., since we will constantly use it when writing programs.

Now a little about how I will build examples in assembler. It is not very convenient to present them in HTML format, so first there will be the program code itself with numbered lines, and immediately after it there will be explanations and notes.

More or less like this:

lines Program code
(1) mov ah,9

Explanations:

In line (1) we do something, and in line (15) we do something.

Huge Request: DO NOT copy programs from the page to the clipboard and then paste them into Notepad (or anywhere else)! Type them manually in a text editor. If there is a printer, then select the program, print the selected fragment, and then transfer to the editor from paper. All examples must be typed by yourself! This will speed up the memorization of operators.

And further. Lowercase and UPPERCASE letters are not distinguished in Assembler. Entries like:

The assembler is perceived equally. You can, of course, force the assembler to distinguish between lowercase and UPPER characters, but we will not do this for now. For readability of the program, it is best to type statements in lowercase letters, and start the names of subroutines and labels with capital letters. But this is how it will be convenient for someone.

So let's move on to our first program:

(1) CSEG segment

(2) org 100h

(4) Start:

(6) mov ah,9

(7) mov dx,offset Message

(8) int 21h

(10) int 20h

(11)

(12) Message db "Hello, world!$"

(13) CSEG ends

(14) endBegin

In order to explain all the operators of this example, we will need several episodes. Therefore, we will simply omit the description of some commands at this stage. Just think that's how it should be. In the very near future we will consider these operators in detail. So, lines with numbers (1), (2) and (13) you simply ignore.

Lines (3), (5), (9) and (11) are left blank. This is done for clarity. The assembler will simply omit them.

Now let's move on to the rest of the operators. From line (4) the program code begins. This is a label that tells the assembler to start the code. Line (14) contains the statements end Begin ( Begin English Start; end the end). This is the end of the program. In general, instead of the word Begin something else could have been used. For example, Start:. In this case, we would have to complete the program End Start (14).

Lines (6) (8) display the message Hello, world!. Here we have to briefly talk about processor registers (we will consider this topic in more detail in the next issue).

A processor register is a dedicated memory for storing a number.

For example:

If we want to add two numbers, then in mathematics we write like this:

A, B and C they are a kind of registers (if we talk about a computer) in which some data can be stored. A=5 can be read as: Assign A the number 5 .

To assign a value to a register, there is a mov operator in Assembler (from English move load). Line (6) should be read like this: Uploading to the register AHthe number 9 (in other words, we assign AHnumber 9). Below we will consider why this is necessary.

In line (7) we load into the register DX the address of the message to be output (in this example it will be the stringHello, world!$).

Interrupts will be covered in detail in future releases. Here I will say a few words.

Interrupt MS-DOS is a kind of subroutine (part of MS DOS), which resides permanently in memory and can be called at any time from any program.

Consider the above with an example (highlight notes in small print):

Program for adding two numbers

HomePrograms

A=5 in variable A we enter the value 5

B=8 to variable B value 8

Subroutine Call Addition

now C is 13

A=10 the same, just different numbers

B=25

Subroutine Call Addition

now C is 35

End of Program

Subroutine Addition

C=A+B

ReturnFrom Subroutine we return to the place from which we called

End of Subroutine

In this example, we called the subroutine twice Addition, which added two numbers passed to it in variables A and B . The result is placed in variable C. When a subroutine is called, the computer remembers where it was called from, and then, when the subroutine has finished, the computer returns to the place it was called from. That. You can call subroutines an indefinite number of times from anywhere.

When executing line (8) of an assembler program, we call a subroutine (in this case called an interrupt) that prints the line to the screen. To do this, we, in fact, put the necessary values ​​in the registers. All the necessary work (line output, cursor movement) is performed by the subroutine. This line can be read like this: we call the twenty-first interrupt ( int from English. interrupt interrupt). Please note that after the number 21 there is a letter h . This, as we already know, is a hexadecimal number (33 in decimal). Of course, nothing prevents us from replacing the line int 21h to int 33. The program will work correctly. It's just that in Assembler it is customary to indicate the interrupt number in hexadecimal.

In line (10) we, you guessed it, call interrupt 20 h . To call this interrupt, you do not need to specify any values ​​​​in the registers. It performs only one task: exiting the program (exiting to DOS). As a result of interrupt 20h, the program will return to where it was launched (loaded, called). For example, in Norton Commander or DOS Navigator.

Line (12) contains the message to be output. First word ( message message) the name of the message. It can be anything (for example, mess or string, etc.). ABOUT pay attention to line (7), in which we load into the register DX our message address.

We can create another line, which we will call mess2. Then, starting from line (9), insert the following commands:

(10) mov dx,offset Mess2

(13) Message db "Hello, world!$"

(14) Mess2 db "It's me! $"

and assemble our program again. I hope you guess what's going to happen

Pay attention to the last character in the lines Message and Mess2 - $. It points to the end of the line. If we remove it, then 21 h the interrupt will continue outputting until a character is encountered somewhere in memory $. On the screen we will see trash .

If you have a debugger, you can see how our program will work.

The purpose of this issue was not to understand in detail with each operator. This is impossible, because you don't have enough knowledge yet. I believe that after 3-4 releases you will understand the principle and structure of the program in Assembler. Maybe you thought the assembly language is extremely complicated, but believe me, at a glance.

Hexadecimal notation ("Hex") is a convenient way to represent binary values. Just as decimal has base ten and binary has base two, hexadecimal has base sixteen.

The base 16 number system uses the numbers 0 to 9 and the letters A to F. The figure shows the equivalent decimal, binary, and hexadecimal values ​​for the binary numbers 0000 to 1111. It is easier for us to express a value as a single hexadecimal digit than as four bits.

Understanding Bytes

Considering that 8 bits (bytes) is a standard binary grouping, binary numbers from 00000000 to 11111111 can be represented in hexadecimal notation as numbers from 00 to FF. Leading zeros are always displayed to complete the 8-bit representation. For example, the binary value 0000 1010 would be 0A in hexadecimal.

Representing Hexadecimal Values

Note: It is important to distinguish hexadecimal values ​​from decimal values ​​for characters 0 to 9, as shown in the figure.

Hexadecimal values ​​are usually represented in text by a value preceded by 0x (eg 0x73) or by a subscript 16. More rarely, they may be followed by the letter H, such as 73H. However, because subscript text is not recognized on the command line or in programming environments, hexadecimal numbers are technically preceded by "0x" (zero X). Therefore, the examples above would be shown as 0x0A and 0x73 respectively.

The hexadecimal notation is used to represent Ethernet MAC addresses and Version 6 IP addresses.

Hexadecimal Conversions

Number conversions between decimal and hexadecimal values ​​are straightforward, but a quick division or multiplication by 16 is not always convenient. If such conversions are needed, it's usually easier to convert the decimal or hexadecimal value to binary, and then convert the binary value to decimal or hexadecimal, whichever you want to get.

With practice, it is possible to recognize binary bit patterns that correspond to decimal and hexadecimal values. The figure shows these patterns for some 8-bit values.

To represent numbers in a microprocessor, binary system.
In this case, any digital signal can have two stable states: "high level" and "low level". In the binary system, for the image of any number, two digits are used, respectively: 0 and 1. Arbitrary number x=a n a n-1 ..a 1 a 0 ,a -1 a -2 …a -m written in binary notation as

x = an 2 n +a n-1 2 n-1 +…+a 1 2 1 +a 0 2 0 +a -1 2 -1 +a -2 2 -2 +…+a -m 2 -m

where a i— binary digits (0 or 1).

Octal number system

In the octal number system, the base digits are the numbers from 0 to 7. The 8 units of the least significant digit are combined into the most significant unit.

Hexadecimal number system

In the hexadecimal number system, the base digits are the numbers 0 through 15 inclusive. To designate base digits greater than 9 with one character, in addition to the Arabic numerals 0 ... 9, the letters of the Latin alphabet are used in the hexadecimal number system:

10 10 = A 16 12 10 = C 16 14 10 = E 16
11 10 = B 16 13 10 = D 16 15 10 = F 16 .

For example, the number 175 10 in hexadecimal will be written as AF 16 . Really,

10 16 1 +15 16 0 =160+15=175

The table contains numbers from 0 to 16 in decimal, binary, octal and hexadecimal number systems.

Decimal Binary octal Hexadecimal
0 0 0 0
1 1 1 1
2 10 2 2
3 11 3 3
4 100 4 4
5 101 5 5
6 110 6 6
7 111 7 7
8 1000 10 8
9 1001 11 9
10 1010 12 A
11 1011 13 B
12 1100 14 C
13 1101 15 D
14 1110 16 E
15 1111 17 F
16 10000 20 10

Binary-octal and binary-hexadecimal conversions

The binary number system is convenient for performing arithmetic operations by microprocessor hardware, but inconvenient for human perception, since it requires a large number of digits. Therefore, in computer technology, in addition to the binary number system, the octal and hexadecimal number systems are widely used for a more compact representation of numbers.

Three bits of the octal number system implement all possible combinations of octal digits in the binary number system: from 0 (000) to 7(111). To convert a binary number to octal, you need to combine binary digits into groups of 3 digits (triads) in two directions, starting from the separator of the integer and fractional parts. If necessary, insignificant zeros must be added to the left of the original number. If the number contains a fractional part, then insignificant zeros can also be added to the right of it until all triads are filled. Then each triad is replaced by an octal digit.

Example: Convert the number 1101110.01 2 to octal.

We combine binary digits into triads from right to left. We get

001 101 110,010 2 = 156,2 8 .

To convert a number from the octal system to binary, you need to write each octal digit in its binary code:

156,2 8 = 001 101 110,010 2 .

Four bits of the hexadecimal number system implement all possible combinations of hexadecimal digits in the binary number system: from 0 (0000) to F(1111). To convert a binary number to hexadecimal, you need to combine binary digits into groups of 4 digits (tetrads) in two directions, starting from the separator of the integer and fractional parts. If necessary, insignificant zeros must be added to the left of the original number. If the number contains a fractional part, then insignificant zeros must also be added to the right of it until all tetrads are filled. Each tetrad is then replaced with a hexadecimal digit.

Example: Convert the number 1101110.11 2 to hexadecimal.

We combine binary digits into tetrads from right to left. We get

0110 1110.1100 2 = 6E,C 16 .

To convert a number from hexadecimal to binary, you need to write each hexadecimal digit in its binary code.

The hexadecimal number system has an alphabet consisting of 16 digits:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, b, c, d, e, f.

When writing a number in the hexadecimal system, to write the numbers denoting the numbers 10, 11, 12. 13, 14. 15, the letters A, B, C, D, E, F are used, respectively.

Converting numbers from hexadecimal to decimal

You can convert any hexadecimal number to decimal using the already known formula

Examples.

    AE07 16 =10∙16 3 +14∙16 2 +0∙16 1 +7∙16 0 =44551 10 .

    100 16 =1∙16 2 +0∙16 1 +0∙16 0 =256 10 .

    58 16 =5∙16 1 +8∙16 0 =.88 10 .

    2A 16 \u003d 2 16 1 + 10 16 0 \u003d 42 10.

The conversion of a number from the decimal system to hexadecimal is carried out in the same way as in binary.

Converting numbers from hexadecimal to binary and vice versa

You can convert any hexadecimal number to binary as follows. Each hexadecimal digit of a number is written as a four-digit binary number - tetrad. After that, the zeros on the left can be discarded.

2) 2A= 0010 1010 2 = 101010 2 .

3) 58 16 = 0101 1000 2 = 1011000 2 .

Conversely, you can convert any binary number to hexadecimal in the same way. Every four binary digits, counting from right to left, is written as one hexadecimal digit. These figures are also arranged from right to left.

Examples.

2. 101010 2 = 10 1010 2 = 2A.

3. 1011000 2 = 101 1000 2 = 58 16 .

Octal number system

The octal number system has an alphabet consisting of 8 digits:

0, 1, 2, 3, 4, 5, 6, 7.

Converting a number from decimal to octal and vice versa is carried out by analogy with converting to / from binary.

Converting numbers from octal to binary and vice versa

Each digit of the octal notation of a number is written as a three-digit binary number - triad.

Examples.

2563 8 = 010 101 110 011 2 =10101110011 2 .

1001101 2 = 001 001 101 2 = 115 8 .

Methodical materials for laboratory lesson No. 1

Topic of the laboratory lesson: Number systems. Measurement of information.

Number of hours: 2.

Examples with Solutions

    Translation fromp -ary system to 10-ary. Suppose it is necessary to convert a number in some number system to decimal. To do this, we need to represent it in the form

11100110 2 = 1∙2 7 + 1∙2 6 + 1∙2 5 + 0∙2 4 + 0∙2 3 + 1∙2 2 + 1∙2 1 + 0∙2 0 = 128 + 64 + 32 + 4 + 2 = 230 10 .

2401 5 = 2∙5 3 + 4∙5 2 + 0∙5 1 + 1∙5 0 = 250 + 100 + 0 + 1 = 351.

    Convert from decimal system top -ic.

2.1 98 10 → X 2 .

We divide the number by 2. Then we divide the incomplete quotient by 2. We continue until the incomplete quotient becomes less than 2, i.e. equal to 1.

    98: 2 = 49. Remainder - 0 .

    49: 2 = 24. Remainder - 1 .

    24: 2 = 12. Remainder - 0 .

    12: 2 = 6. Remainder - 0 .

    6: 2 = 3. Remainder - 0 .

    3: 2 = 1 . Remainder - 1 .

Since the last incomplete quotient is 1, the process is over. We write down all the remainders from the bottom up, starting with the last incomplete quotient, and we get the number 1100010. So 98 10 \u003d 1100010 2.

2.2 2391 10 → X 16 .

Divide the number by 16. Then divide the partial quotient by 16. Continue until the partial quotient is less than 16.

    2391: 16 = 149. Remainder - 7 .

    149: 16 = 9 . Remainder - 5 .

Since the last incomplete quotient (9) is less than 16, the process is over. We write down, starting from the last incomplete quotient, all the remainders from the bottom up and get the number 957. So 2391 10 \u003d 957 16.

2.3 12165 10 → X 2 .

If you translate division into a binary system, you get a contented cumbersome process. You can first convert the number to the octal system, and then replace the octal digits from right to left with triads.

12165 10 = 27605 8 = 010 111 110 000 101 = 10111110000101.

    Determination of the base of the number systemp .

One boy wrote about himself this way: “I have 24 fingers, 5 on each hand, and 12 on my feet.” How can this be?

Solution. Determine the base of the number system p. Since we know that there are only 10 10 toes, then 12 p =1∙p+2 = 10 10 . From here we get the equation p + 2 = 10  p= 8. So the boy meant numbers in the octal system. Indeed, there are 24 8 = 2∙8+4 = 20 10 fingers in total, and 12 8 = 1∙8+2 = 10 10 on the legs.

Top Related Articles