The program for the translation of number systems in assembler. Abstract: The program for converting a decimal number into binary and hexadecimal number systems. General information about assembly language

Hexadecimal number system(also hexadecimal code) is a positional number system with integer base 16. Sometimes the term hex is also used in the literature (pronounced "hex", short for English hexadecimal). The numbers of this number system are commonly used 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, together 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 it is convenient to write the value of a byte using two hexadecimal digits. A byte value can range from #00 to #FF (0 to 255 in decimal notation) - in other words, using hex 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 represent the character number. The RGB (Red, Green, Blue) color notation also often uses a hexadecimal code (for example, #FF0000 is a bright red notation).

A way to write a hexadecimal code.

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

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

* In the syntax of some varieties of assembly language, the Latin letter "h" is used, which is placed to the right of the number, for example: 20Dh. If the number starts with a Latin letter, then it is preceded by a zero, for example: 0A0Bh. This is done in order to distinguish values ​​from constants using hex code;

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

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

How to translate a hexadecimal code to another system?

Convert from hexadecimal to decimal. To perform a conversion operation from a hexadecimal system to a decimal one, it is required to represent the original number as the sum of the products of the digits in the digits of the hexadecimal number by the degree of the base.

Binary SS

hexadecimal 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 base 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.

For translation, a table of notebooks is used. To convert a number from binary to decimal, it is necessary to split it into separate tetrads from right to left, and then, using the table, replace each tetrad with the corresponding hexadecimal digit. In this case, 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 must 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 trailing zeros were added to bring the total number of digits to 12.

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

1. Introduction

2. General information about assembly language

3. Software part

· Program description

Stages of development of assembly programs ___

Program for converting decimal numbers to binary and hexadecimal number systems

4. References


Introduction

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

The hardware includes the following devices:

the central processing unit;

· 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 acceptance and operation test requirements.

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

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

A program is a complete sequence of machine instructions or programming language operators that defines 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) notation for machine instructions and addresses. The advantage of this language is: firstly, that programs written in it require much less memory; secondly, knowledge of this language and the resulting machine code gives an idea of ​​the architecture of the machine, which is hardly provided when working in a high-level programming language.


General information about assembly language

The symbolic assembly language makes it possible to largely eliminate the shortcomings of machine language programming.

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

Symbolic names introduced when programming in assembly language, as a rule, reflect the semantics of the program, and the abbreviation of commands - their main function. For example: PARAM - parameter, TABLE - table, MASK - mask, ADD - addition, SUB - subtraction, etc. n. Such names are easily remembered by the programmer.

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

Assembly languages ​​are machine-oriented, that is, dependent on the machine language and structure of the corresponding microprocessor, since they assign a specific symbolic name to each microprocessor instruction.

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

In this regard, almost all I / O device control programs (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:

mnemonic (symbolic name) of each command of the machine language of the microprocessor;

standard format for lines of a program described in assembler;

a format for specifying various addressing methods and command options;

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

pseudo-commands that control the process of assembly (translation) of the program.

In assembly language, the program is written line by line, i.e., one line is allocated for each instruction.

For microcomputers built on the basis of the most common types of microprocessors, there may be several variants of the assembly language, but the practical distribution usually has one - this is the so-called standard assembly language. In the future, we will consider the 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 where the first byte of the marked instruction is located. Labels are used as transition addresses for control transfer commands, and due to their presence, the programmer can not operate with absolute addresses, but use symbolic addresses, which is much more convenient. The label can be from 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. In some assemblers, the last character of the label must be followed by a colon.

In the label field, each label need only be defined once, but it can be referenced as many times as needed. 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 being executed. The mnemonic of 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 (INSCREMENT battery)

REGISTER) - register increment

REGISTER) register decrement

Command mnemonics are assembler keywords, and if they are not included in the set of valid mnemonics, then the assembler issues an invalid instruction message.

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

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

The simplest operands contain one mnemonic notation or one constant.

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

Constants can be represented in different number systems.

Software part

Description programs

In this work, we will consider one of the ways to convert a number from a decimal system to binary and hexadecimal using assembly language. Before creating a program, we will consider in detail what steps we need to take 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 that the first step in solving our problem will be to enter a number. The second step in the work will be to display a message about the entered number. After that, 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 a 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. Displaying a message about the entered number.

3. Converting a number to a binary equivalent.

4. Displaying a binary number.

5. Converting a number to a hexadecimal system.

6. Displaying a hexadecimal number.

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

8. Exit the program.

This is the natural language program algorithm.

stages of development of assembler programs

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

2. Development of the text of programs.

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

4. Compilation or assembly. A text File with *.ASM extension is converted into an object File containing a machine code program with *.OBJ extension. Also at this stage, a listing of the program can be created. File with *.LST extension, which contains basic information about the program, as well as Cross-reference file with *.CRF extension. At this stage, the program text is boiled 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 to perform translation. If the command contains one comma, then Listing file is Formed. 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. It is saved in a File with *.EXE or *.COM extension. For this, TLINK.exe (for the macro assembler LINK.EXE) is used. 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 see the block diagram of our program, that is, ordered actions.


; PROGRAM FOR CONVERSING DECIMAL NUMBER TO; BINARY AND HEXADIMAL SYSTEMS; NUMBERS

;Data segment

;Conversion table“digit - ASCII-the code

tabl_ascii db "0123456789abcdef"

;____________________________________________________________________

;Conversion table“ASCII-code - digit

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 entered a number",0dh,0ah "$"

t3 db 0dh, 0ah, "It looks like this in binary"

t4 db 0dh, 0ah, "In hex 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

;Decimal entry procedure

;Procedure for displaying a decimal number

r1: movdl,

; Converting a number (decimal) to binary

v1: mul si

;Procedure for displaying a binary number

; Converting a number (binary) to hexadecimal

; and display it on the screen

Notes :

Below are the commands used in the program:

sub- binary subtraction. The contents of the second operand are subtracted from the first operand.

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

call- procedure call. Transfers control to the procedure whose address is given by the operand, after the completion of the procedure, execution continues with the command following the call command

Mnemonics: call< procedure name>

ret- return to procedure

shr- shift logically to the right

xor- XOR

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

lea- download EA

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

push- include in stack

Mnemonics: push< operand>

pop- extract 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 of register al or ax pointed to by register di

loop– a command for organizing a cycle with a counter, also short jumps (127b) the command decreases the value of the counter cx, without changing any flags, if the connection cx > 0, then the transition to the specified label is performed, otherwise the cycle ends.

Mnemonics:loop< label>

.CODE- opens the code segment

. DATA-- opens a data segment

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

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

mov ds,ax

assume is not used in this case

Bibliography

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

2. "IBM personal computer and MS-DOS operating system" Radio and communication 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 IBM PC”, M., Computer Publishing House, 1993


Binary, octal, decimal, 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 score, 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 represent numbers in that system.

The assembler allows the use of numbers in binary, octal, decimal, or hexadecimal. By default, the assembler considers all numbers encountered in the program to be 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 the number, a zero is written at the beginning - according to the rules of the assembler, the designations of numbers must begin with a digit. For example:

.data var1 byte 00001111b ; 15 in binary representation var2 byte 00001111y ; 15 in binary representation var3 byte 17o ; 15 in octal var4 byte 17q ; 15 in octal var5 byte 15d ; 15 in decimal 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 an instruction of the form

.RADIX (base)

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

.RADIX 16

The existence of two variants of tags for binary and decimal numbers is caused by considerations of compatibility of earlier versions of MASM32, in which there was no possibility of writing numbers in hexadecimal format, with later versions. For hexadecimal numbers, Arabic numerals are not enough, so the number row 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 - match the hexadecimal digits, which makes it impossible to use them with the .RADIX 16 directive. The o tag is duplicated by the q tag because the former is similar to zero. For these reasons, the tags y , q , t are preferred.

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

The translation of a machine number into a given number system is carried out by successive division of this number and the resulting result by the basis of the desired number system. The remainder of the division is entered in the least significant digit, the quotient is again divided by the base of the number system, the remainder is entered in 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 in a byte is 255. If, according to 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 - 255 is three decimal orders. In variables of type word (two bytes), the maximum value is 65,535, in variables of type double word (four bytes) - 4,294,967,295. Accordingly, we divide word numbers by 10 to convert to decimal format no more than five times, double word - no more than more than ten times.

Codes of character-digits 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 digit into a symbolic form, it is enough to add 48 to it.

A fragment of the program that translates a byte variable into a decimal character 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, parametr ; copy 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 quotient to buffer_dec ADD buffer_dec, 48 ; add 48 - get digit character "2" ADD buffer_dec, 48 ; add 48 - get digit character "5" ADD buffer_dec, 48 ; add 48 - get the character of the number "5"

Add a comment



Continuing the topic:
Windows

Natalya Komarova , 05/28/2009 (03/25/2018) When you read a forum or blog, you remember the authors of the posts by nickname and ... by the user's picture, the so-called avatar ....