Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the conversion of various code systems in assembly language

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to realize the conversion of various code systems in assembly language". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how to realize the conversion of various code systems in assembly language"!

1. Hexadecimal to binary number design 1.1 Design requirements:

A conversion program is designed to convert the four-digit hexadecimal data typed by the keyboard into the equivalent binary number and display it on the terminal. Requirements: improve the program structure, set the program error exit. The value entered is not between 0Mel F, an error message is displayed, and re-input is required.

1.2 Design ideas:

Input four hexadecimal numbers in turn and store them in BX in turn, and finally output the result in BX as binary.

List of programs: DATA SEGMENT pkey DB "pleas input 4 hex (0dh F): $" ekey DB 0dh DATA ENDSCODE SEGMENT ASSUME CS:CODE,DS:DATA STATE 0h is:',0dh,0ah,'$' numm DB 5 dup (0) DATA ENDSCODE SEGMENT ASSUME CS:CODE,DS:DATA STATE: MOV AX,DATA MOV DS,AX MOV CX,4 Enter hexadecimal number cycle 4 times LEA SI,numm; open index register SI LEA DX,pkey MOV AH,9 INT 21H; DOS function call, output string lp: MOV AH, 1 INT 21H; DOS function call, enter 4 hexadecimal number CMP AL,'9' JA abow9 CMP AL,'0' JB eero SUB AL,30h When the input is 0 ~ 9, the ASCII code minus 30H stores JMP lop eero:LEA DX, ekey; when the input is not in 09and not in Avalf, the error MOV AH, 9 INT 21H JMP lpabow9:CMP AL,'A' JNB abowa JMP eero abowa:CMP AL,'F' JA eero SUB AL,37h is displayed. ASCII code minus 37H stores JMP lop lop: MOV [SI], AL ROL BX,4 ADD BX, [SI]; stores input data in BX register INC SI DEC CX JNZ lp MOV CX,16; output binary number loop 16 times LEA DX,huicheMOV AH,9 INT 21H; DOS function call, output string lp1: ROL BX,1 Move 1 bit to the left to display the highest MOV DL,BL AND DL,01H; shield DL high 7-bit ADD DL,30H; add 30H corresponding to ASCII code MOV AH,2 INT 21H; DOS function call, bit-by-bit output of DEC CX JNZ lp1 CODE ENDSEND STATE1.4 program running results and analysis:

Storage and output will encounter some minor problems, this program through shift and addition instructions to achieve storage, more troublesome but can be achieved, the following program has been improved.

Figure 1 Design result of hexadecimal conversion to binary number

two。 Hexadecimal to decimal number design 2.1 Design requirements:

A conversion program is designed to convert a hexadecimal number entered by the keyboard into an equivalent decimal number and display it on the terminal. Design requirements: improve the program structure, set program error exit. The output is not the number between 0mi F, the error message is displayed, and the re-input is required.

2.2 Design ideas:

Enter a hexadecimal number to determine whether it is 09,0,9, respectively, and output after conversion.

2.2.3 list of programs: DATA SEGMENT pkey DB 0dhjol 0ah, "pleas input 1 hex (0mm F): $" huiche DB 0dhreel 0ah is wrong',0dh,0ah,'please input 1 hex (0mm F) again:','$' numm DB 3 dup (0) DATA ENDSCODE SEGMENT ASSUME CS:CODE,DS:DATA STATE: MOV AX,DATA MOV DS,AX LEA SI,numm Open up buffer LEA DX,pkey MOV AH,9 INT 21H; DOS function call, output string LP: MOV AH, 1 INT 21H; DOS function call, enter a hexadecimal number CMP AL,'9' JA abow9 CMP AL,'0' JB eero; input less than 0 to re-enter ADD AH,2FH Input 09 high bit to 0 output JMP OUTPUT abow9: CMP AL,'A' JNB abowA JMP eero; input greater than 9 less than A re-enter abowA:CMP AL,'F' JA abowF SUB AL,11H ADD AH,30H; input AUBF high bit to 1, low minus 11H output JMP OUTPUTabowF:CMP AL,'a' JNB abow1a JMP eero Input greater than F less than a re-enter abow1a:CMP AL,'f' JA eero; input greater than f re-enter SUB AL,31H ADD AH,30H; input axif high bit to 1, low minus 31H output JMP OUTPUTeero:LEA DX,end2 MOV AH,9 INT 21H DOS function call, output error string JMP LP OUTPUT: MOV BX,AX LEA DX,huiche MOV AH,9 INT 21H; DOS function call, output string MOV [SI], BH MOV DL, [SI] MOV AH,2 INT 21H DOS function call, high output MOV [SI], BL MOV DL, [SI] MOV AH,2 INT 21H; DOS function call, low output JMP STATE CODE ENDSEND STATE2.4 program running results and analysis:

The output here is divided into high and low output, because it is a hexadecimal conversion, if you change two or more digits, you need to modify the program to be more intelligent.

Figure 2 Design result of hexadecimal conversion to decimal number

3. Design requirements for hexadecimal conversion to ASCII code 3.1:

A conversion program is designed to convert the hexadecimal data typed by the keyboard into the corresponding ASCII code and display it in the terminal.

3.2 Design ideas:

Shift the high position of the input number to the left to the ASCII output and the low bit to the ASCII output.

3.3.The list of programs: DATA SEGMENT pkey DB 0dhjol 0ah, "please input 1 hex (0mm F): $" huiche DB 0dhreel 0ah is wrong',0dh,0ah,'please input 1 hex (0mm F) again:','$' numm DB 3 dup (0) DATA ENDSCODE SEGMENT ASSUME CS:CODE,DS:DATA STATE: MOV AX,DATA MOV DS,AX LEA SI,numm Open up buffer LEA DX,pkey MOV AH,9 INT 21H; DOS function call, output string LP: MOV AH, 1 INT 21H; DOS function call, enter a hexadecimal number CMP AL,'9' JA abow9 CMP AL,'0' JB end1; input less than 0 and re-enter JMP OUTPUT abow9: CMP AL,'A' JNB abowA JMP end1 Input greater than 9 < A re-enter abowA:CMP AL,'F' JA end1; input greater than F re-enter JMP OUTPUT end1:LEA DX,end2 MOV AH,9 INT 21H; DOS function call, output error string JMP LP OUTPUT: MOV BX,AX ROL BX,4 AND BH,0FH ADD BH,30H The high bit is AX left four bits plus 30H to get AND AL,0FH MOV BL,AL ADD BL,30H; the low bit is AL low four bits plus 30H to get LEA DX,huiche MOV AH,9 INT 21H; DOS function call, the output string MOV [SI], BH MOV DL, [SI] MOV AH,2 INT 21H DOS function call, high output MOV [SI], BL MOV DL, [SI] MOV AH,2 INT 21H; DOS function call, low output JMP STATE CODE ENDSEND STATE3.4 program running results and analysis:

Figure 3 Design result of hexadecimal conversion to ASCII code

4. English alphabet case conversion design 4.1 Design requirements:

The case conversion of English letters is essentially a direct conversion of ASCII codes. It is required to master the representation and conversion of uppercase and lowercase letters in the computer. A program is designed to continuously convert lowercase letters entered by the keyboard into uppercase letters and display them at the terminal. Design requirements: lowercase letter conversion to uppercase letter output, non-letter or uppercase letter input, direct output without processing.

4.2 Design ideas:

Determine whether the input is aquarz, and if so minus 20H output, not direct output.

4.3Program list: DATA SEGMENT pkey DB 0dhjin0ah, "pleas input:$" result DB 0dhrecast 0ah INT result is:','$' DATA ENDSCODE SEGMENT ASSUME CS:CODE,DS:DATA STATE: MOV AX,DATA MOV DS,AX; initialization data segment LP: LEA DX,pkey MOV AH,9 INT 21H; DOS function call, output string MOV AH, 1 dh21H DOS function call, keyboard input CMP AL,'z' JA LLP CMP AL,'a'; input does not output JB LLP SUB AL,20H directly between aquiz; input is axiz conversion, ASCII code minus 20H MOV BL,AL OUTPUT: LEA DX,result MOV AH,9 INT 21H DOS function call, output string MOV DL,BL MOV AH,2 INT 21H; DOS function call, output DL content JMP LP LLP: MOV BL,AL JMP OUTPUT CODE ENDSEND STATE4.4 program running results and analysis:

Fig. 4 Design result of case conversion of English letters

At this point, I believe you have a deeper understanding of "how to realize the conversion of various code systems in assembly language". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report