Lu mingzhu, Zhao Xiaohua

(South campus of Cangzhou Normal College, Cangzhou 061001, Hebei)

【Abstract】when programming in assembly language, students think that it is difficult to display the result data in different numbers. The number of machines in a computer is processed and stored in binary format. How to convert them and display them on the display in binary, decimal, and hexadecimal formats, the article provides specific implementation methods.

【Key words】 assembly language; binary; decimal; hexadecimal; Display

[] Tp313 [document identification code] a [document No.] 1008-1151 (2006) 04-0032-02

I. Introduction

If we want to deal with a number-related problem, whether in the middle or the final result, we all want to be visually displayed on the display. The data storage method in a digital computer is a binary string consisting of "0" and "1". According to different needs, we usually require different data display formats, such as binary, decimal, hexadecimal, and octal. However, the display of data in assembly languages is not as convenient as the output format commands in advanced languages, generally, the system display function can be called to complete the display only by converting the ASCII code in one place. The conversion between the numbers must be completed according to the requirements of different numbers. Next we will discuss how to program a number to display it in binary, decimal, and hexadecimal notation on the display.

Ii. Programming ideas and Algorithms

Take a 16-bit binary machine Integer as an example. Its binary value ranges from 0 to 111111111111b, And the decimal value ranges from 0 to 65535 (unsigned number ), or-32768 to + 32767 (signed number); The hexadecimal format indicates that the range is 0000 to ffffh. For convenience, if this number is placed in the Bx register, the following describes the programming ideas and algorithms displayed in different hexadecimal forms.

(1) display in binary format

There are only two binary numbers: "0" and "1". Their ASCII codes are 30 h and 31 h respectively, as long as the bitwise judgment is "0" or "1 ", then convert it to the corresponding ASCII code.

In actual programming, you can set a 16-bit loop body for the 16-bit binary number in the Bx, and use the shift command to the left to pass the CF mark in the CPU mark register, it is determined by bit from the high position to the low position, obtains the ASCII code value by bit, and displays by bit. Of course, you can also convert Sixteen digits into an ascii code string and put it into the memory buffer before display in string mode. Note that the function numbers called by the system are different in the two methods.

(2) display in decimal format

First, we need to complete the conversion from binary to decimal number. Generally, we use the "Except 10 remainder" method. The general process is as follows: divide the number to be converted (in BX) by 10, obtain the first quotient and remainder. The first remainder is the single digit of the given decimal number. Then, divide the first quotient by 10 to obtain the second quotient and remainder, the second remainder is the ten digits of the decimal number ;......; Repeat this process until the quotient is 0. The remainder is the highest digit of the given decimal number.

Allocate a buffer in the memory to store each digit of the separated decimal number, and then convert it into ASCII code one by one. The process of converting it into ASCII code is very simple, it is to add 30 h to each decimal bit, and finally send the display output. From the conversion process, we can see that the first split is the low digit of the decimal number, which is to be displayed later; then the split is the high digit of the decimal number, which should be displayed first. Therefore, in programming, we can use the "First, First, Second, first, and foremost" feature in stack operations. When the decimal digits are separated, the data is converted into ASCII codes one by one, the output stack is displayed in sequence.

(3) display in hexadecimal format

There is a good ing between hexadecimal and binary (there is a one-to-one ing between every 4-bit binary and 1-bit hexadecimal ), when displaying, you only need to divide every four digits of the original binary number (in the Bx) into a group, and send the corresponding ASCII code to the display according to the group requirement.

The relationship between the ASCII code and the hexadecimal number is: 30h ~ The corresponding number for 39h is 0 ~ 9, 41h ~ 46h corresponds to number ~ F, from number 9 to A, the ASCII code is separated by 7 h, which requires special attention during conversion. In order to enable a hexadecimal number to be displayed in sequence from high to low, in actual programming, we shift the number in the first Bx to the left of each loop (4-bit binary ), then, the current 12-bit height is blocked, and the remaining 4 digits (I .e. the number of one-bit hexadecimal digits) are requested for the ASCII code. It must be determined that it is 0 ~ 9 or ~ F. If it is the former, the corresponding ASCII code is added for 30 h. If it is the latter, it must be added for 37 h, and the display output is finally sent. Repeat the preceding steps four times to display the numbers in a 4-digit hexadecimal format.

Iii. Example of assembly language source program

Assume that the numbers in the Bx register are displayed on the screen in Unsigned binary, decimal, and hexadecimal notation respectively. The following describes the implementation of the Assembly source program, which is a subroutine. to be concise, omitted on-site protection and recovery operations.

(1) In binary format

Dispbxb proc near

MoV CX, 16

LP: rol bx, 1

JC d1

MoV DL, 30 h

JMP outp

D1: mov DL, 31 H

Outp: mov ah, 2

Int 21 h

Loop lp

MoV DL, 'B'; displays the letter "B"

MoV ah, 2

Int 21 h

RET

Dispbxb endp

If it is set to (BX) = 7 fffh, the output format is 01111111111111b, followed by the uppercase letter B indicating the binary format, and the display result is fixed to sixteen characters.

(2) In decimal format

Dispbxd proc near

MoV Si, 10

Xor cx, CX

MoV ax, BX

Next: mov dx, 10

Div Si

Push DX

INC CX

CMP ax, 0; if the provider is 0, the conversion is completed.

Jnz next

Outp: Pop DX

Add DL, 30 h

MoV ah, 2

Int 21 h

Loop outp

RET

Dispbxd endp

The cyclic control mode of this program during conversion and display is different from the example of the binary form, that is, the number of digits of the display result is not fixed. If (BX) = 7 fffh = 32767, the result is 5 bits: 32767. If (BX) = 00ffh = 255, the result is 3 bits: 255, it is the same as the valid digits in decimal format.

(3) hexadecimal form

Dispbxh proc near

MoV cl, 4

MoV CH, 4

Next: rol bx, Cl

MoV Al, BL

And Al, 0fh

Add Al, 30 h

CMP Al, 3ah

Jl outp

Add Al, 07 h; Yes ~ F. Add 7 more

Outp: mov DL, Al

MoV ah, 02 h

Int 21 h

Dec ch

Cmp ch, 0

Ja next

MoV DL, 'H'; displays the letter "H"

MoV ah, 2

Int 21 h

RET

Dispbxh endp

The display format of this program is fixed to 4 digits, and the English letter "H" is added to the hexadecimal format. If (BX) = 7 fffh, the result is 7 fffh. If (BX) = 00ffh, the result is 00ffh.

4. Extended applications for the display of signed numbers

Cmp bx, 0

Jge Zs

MoV DL ,'-'

MoV ah, 2

Int 21 h

ZS: Call dispbxd

V. Conclusion

The preceding section describes how to display the number of machines in different formats based on different needs in assembly language programming, you only need to change the divisor in the sub-Program (dispbxd) displayed in decimal format from 10 to 8. In various methods, the data conversion methods are different, and the display function call commands are the same. Of course, you can also call the display function using other (such as string mode) methods, I will not go into details here.

[References]

IBM-PC assembly language programming [M]. Beijing: Tsinghua University Press, 1991.