Converting from hexadecimal to decimal in assembler. Assembly programming for beginners and more

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 be represented is equal to the base of the number system, for example, for the binary system, the base is the number 2, therefore, to represent numbers, two digits 0 and 1 are needed, for the hexadecimal number system it is 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, you need 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 down 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 to store 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). A variable of this type can contain an integer in the range from -32768 to +32767 or from 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 a number into the program text, its translation into a machine-readable representation is carried out by the assembler, and this number will be written in the proper (binary) form in the executable file. 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 variables of the word type (two bytes) the maximum value is 65,535, in variables of the double word type (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

1. Introduction

2. General information about assembly language

3. Software part

· Program description

· Stages of development of assembly programs ___

· Program for converting decimal numbers into binary and hexadecimal number systems

4. References


Introduction

The tools that ensure the functioning of computer technology are divided into 2 parts: hardware and software.

The hardware includes the following devices:

· central processor;

· RAM;

· peripherals;

All of the above devices are built on integrated circuits (ICs).

An integrated circuit is a microelectronic product that performs certain conversion functions, has a high packing density of electrically interconnected elements and components, and represents a single whole in terms of the requirements for acceptance and operation testing.

An example of an IC are digital device circuits: registers, adders, half-adders, counters, encoders, decoders, etc.

The software part includes: a set of programs and rules with all related documentation, which allows you to use a computer to solve various problems.

A program is a complete sequence of machine commands or programming language statements that define a sequence of actions to solve a certain problem.

The task in our work is: converting a three-digit decimal number into binary and hexadecimal number systems. This task is implemented using assembly language. This low-level language uses symbolic (mnemonic) notations for machine instructions and addresses. The advantage of this language is: firstly, that programs written in it require significantly less memory; secondly, knowledge of this language and the resulting machine code gives an idea of ​​the machine's architecture, which is unlikely to be provided when working in a high-level programming language.


General information about assembly language

Symbolic assembly language can largely eliminate the disadvantages of machine language programming.

Its main advantage is that in assembly language all program elements are presented in symbolic form. The conversion of symbolic command names into their binary codes is entrusted to a special program - an assembler, which frees the programmer from labor-intensive work and eliminates the inevitable errors.

Symbolic names entered when programming in assembly language usually reflect the semantics of the program, and the abbreviation of commands reflects their main function. For example: PARAM – parameter, TABLE – table, MASK – mask, ADD – addition, SUB – subtraction, etc. etc. Such names are easy for a programmer to remember.

For programming in assembly language, it is necessary to have more complex tools than for programming in machine language: you need computer systems based on a microcomputer or PC with a set of peripheral devices (alphanumeric keyboard, character display, float drive and printing device), as well as resident or cross-programming systems for the required types of microprocessors. Assembly language allows you to effectively write and debug much more complex programs than machine language (up to 1 - 4 KB).

Assembly languages ​​are machine-oriented, i.e., dependent on the machine language and structure of the corresponding microprocessor, since in them each microprocessor instruction is assigned a specific symbolic name.

Assembly languages ​​provide a significant increase in programmer productivity compared to machine languages ​​and at the same time retain the ability to use all software-available hardware resources of the microprocessor. This enables skilled programmers to write programs that run in less time and occupy less memory than programs written in a high-level language.

In this regard, almost all programs for controlling input/output devices (drivers) are written in assembly language, despite the presence of a fairly large range of high-level languages.

Using assembly language, the programmer can set the following parameters:

mnemonics (symbolic name) of each microprocessor machine language command;

a standard format for lines of a program written in assembly language;

a format for specifying different addressing methods and command options;

format for specifying character constants and integer constants in various number systems;

pseudo-commands that control the process of assembling (translating) a program.

In assembly language, a program is written line by line, that is, one line is allocated for each command.

For microcomputers built on the basis of the most common types of microprocessors, there may be several variants of assembly language, but usually one is widely used in practice - this is the so-called standard assembly language. In what follows, we will consider standard assembly languages.

Each line of a program written in assembly language contains four fields:

LABEL CODE OPERAND COMMENT

The LABEL field is optional; it marks the address of the memory cell in which the first byte of the marked command is located. Labels are used as transition addresses for control transfer commands, and thanks to their presence, the programmer can not operate with absolute addresses, but use symbolic addresses, which is much more convenient. The label can be one to six characters long, the first of which must be a letter. Many assemblers allow labels of any length, but only the first six characters are recognized. The label must not contain spaces or punctuation marks. In some assemblers, the last character of the label must be followed by a colon.

In a label field, each label must be defined only once, but references to it can be used as many times as necessary. Otherwise, the assembler will issue a diagnostic message about the multiply defined label.

The CODE field contains the symbolic name of the command or pseudo-command to be executed. The mnemonic for most commands is an abbreviation of sentences in English that characterize their main function.

For example:

MOV (MOVE) - transfer, forward

ADD (ADDITION) - addition

SUB (SUBSTRACT) - subtraction

LDA (LOAD DIRECT

ACCUMULATOR) - direct loading

INR (Battery INSCREMENT

REGISTER) - register increment

REGISTER) register decrement

Command mnemonics are assembler keywords, and if they are not included in the set of valid mnemonics, the assembler reports an invalid command.

The OPERAND field is usually defined depending on the instruction code field. It may contain one or more operands, separated by commas, or it may contain no operands for those instructions that operate on internal working registers.

An operand is an expression containing mnemonic notation, constants, and operators.

The simplest operands contain one mnemonic or one constant.

The identifiers of internal working registers, labels and the current value of the program counter can be used as a mnemonic.

Constants can be represented in different number systems.

Software part

Description programs

In this work, we will look at one of the ways to convert a number from the decimal number system to binary and hexadecimal using Assembly language. Before creating a program, we will consider in detail what steps need to be taken for this, that is, in other words, we will write an algorithm for solving our problem. In order for a computer to process data, it needs to enter this data, which means the first step in solving our problem will be entering a number. The second step in the work will be to display a message about the entered number. After this, we convert the decimal number to the binary system and display our number in binary equivalent on the screen. The next step is to convert the number to the hexadecimal equivalent and the last step is a loop that allows you to continue entering a new decimal number. Now let's put all the points together:

1. Entering a number from the keyboard.

2. Display a message about the entered number.

3. Converting a number to its binary equivalent.

4. Display a binary number.

5. Converting a number to hexadecimal.

6. Display a hexadecimal number.

7. Cycle (shall we continue?) if YES then point 1, otherwise point 8

8. Exit the program.

This is the algorithm of a natural language program.

stages of development of assembler programs

1. Statement of the problem. Includes a meaningful description of the problem and development of the algorithm.

2. Development of program text.

3. Entering text into the computer. The text of the program in mnemonic codes is entered into the computer using any text editor. This also creates a text file with the extension *.ASM.

4. Compilation or assembly. A text File with the *.ASM extension is converted into an object File containing a program in machine code with the *.OBJ extension. Also at this stage a program listing can be created. A file with the extension *.LST, which contains basic information about the program, as well as a Cross-Reference File with the extension *.CRF. At this stage, the program text is checked for errors. Assembly is carried out using the TASM.EXE translator program (ASM.EXE - in assembler, MASM.EXE - in macro assembler). TASM [options] *.ASM [,] - command for performing translation. If one comma is specified in the command, then the Listing File is Generated. TASM has two options: /ZI and /N. They are called: TASM.

5. Layout. At this stage, a relocatable program is created that can be loaded into any memory area. Saved in a File with the extension *.EXE or *.COM. To do this, use TLINK.exe (for the macro assembler LINK.EXE). The options are /T and /X.

6. Execution and Debugging (DEBUG).

7. Entering the machine code of the program into ROM (may be missing) Now we will look at the block diagram of our program, that is, the ordered actions.


; PROGRAM FOR CONVERTING A DECIMAL TO; BINARY AND HEXADECIMAL SYSTEMS; CUMULATIONS

;Data segment

;Conversion table“digit – ASCII-code

tabl_ascii db "0123456789abcdef"

;____________________________________________________________________

;Conversion table“ASCII-code - number

db 0,1,2,3,4,5,6,7,8,9

db 0ah,0bh, 0ch, 0dh, 0eh, 0fh

;____________________________________________________________________

;Reservation and initialization of variables in memory

x_ascii db 20h dup(?)

t1 db 0dh,0ah,"Enter a number and press Enter"

db 0dh, 0ah, "$"

t2 db 0dh,0ah,"You have entered a number”,0dh,0ah "$"

t3 db 0dh, 0ah, "In binary it looks like this"

t4 db 0dh, 0ah, "In hexadecimal like this"

db 0dh, 0ah, "$"

buf db 16 dup(?),"$"

t5 db 0dh,0ah, "Shall we continue the process? (Y/N)?"

;____________________________________________________________________

; Code segment

;Main procedure

d: lea dx, t1

;Procedure for entering a decimal number

;Procedure for displaying a decimal number

r1: mov dl,

; Converting a number (decimal) to binary

v1: mul si

;Procedure for displaying a binary number

; Procedure for converting a number (binary) to hexadecimal

; and display it on the screen

Notes :

Below are the commands used in the program:

sub– binary subtraction. Subtracts the contents of the second operand from the first operand

Mnemonics:sub< operand 1>,< operand 2>

call– procedure call. Transfers control to a procedure whose address is specified by the operand; after the procedure is completed, execution continues with the command following the call command

Mnemonics: call< procedure name>

ret– return to procedure

shr– move logically to the right

xor– exclusive OR

Mnemonics:xor<операнд 1>,<операнд 2>

lea– download EA

Mnemonics:lea reg,<операнд>

push– include in stack

Mnemonics: push< operand>

pop– retrieve from stack

Mnemonics: pop<операнд>

mov– forward

Mnemonics:mov< receiver>,<источник>

inc– increase by 1

Mnemonics:inc<операнд>

dec– decrease by 1

Mnemonics: dec< operand>

stosb– forwards connections to the al or ax register pointed to by the di register

loop– a command to organize a loop with a counter, also short transitions (127b) the command decreases the value of the counter cx, without changing any flags, if the connection cx > 0, then the transition to the given label is carried out, otherwise the loop ends.

Mnemonics: loop< label>

.CODE– opens a code segment

. DATA-- opens a data segment

.STACK N– defines the segment stack(a); Segment closing directives are not used in this case; N – shows the size of stack(a) in bytes

Note : When using such directives, the ds register is initialized as follows: mov ax,@data

mov ds,ax

assume is not used in this case

Bibliography

1. "I am assembly language for IBM PC and programming" Higher School 1992.

2. “IBM Personal Computer and MS-DOS Operating System” Radio and Communications 1991.

3. Ilyushechkin V.N., Kostin A.E., Khokhlov M.M. “System software”, M., “Higher School”, 1987

4. Norton P., Souhe D. “Assembly language for the IBM PC”, M., Publishing House “Computer”, 1993

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!

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 the architectural solutions of 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 the hexadecimal system this number will have the notation 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 a number begins with a 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;

* Other types of assembler, as well as Pascal (and its variants such as Delphi) and some Basic dialects, use the "$" prefix: $A15;

* In the HTML markup language, as well as in cascading CSS files, the prefix “#” is used to indicate a color in RGB format with a hexadecimal notation: #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 the binary to the decimal system, you need to split it into separate tetrads from right to left, and then, using the 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 currently in use (see menu on the left: Hex, Dec, Oct, Bin). In this case, changing the current number system automatically produces a translation.