In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to implement string operations in assembly language. The quality of the article is high. Therefore, Xiaobian shares it with you as a reference. I hope you have a certain understanding of relevant knowledge after reading this article.
Software: emu8086
Language: Assembly Language
Note: Xiaobian lists the code displayed by string matching and string input. All the codes are written by Xiaobian alone, which will have defects and be used with caution.
1. String matching design 1.1 Design requirements:
Requirements to master the prompt information settings and read the information typed in the method. Design procedures, input two character strings from the keyboard to compare, such as the same, then display "MATCH", otherwise display "NO MATCH".
1.2 Design Idea:
DOS function calls input two character strings, compare character strings one byte by byte through CMPSB instruction, output MATCH for the same, output NO MATCH for different.
1.3 Program list: DATA SEGMENT pkey DB 0DH,0AH, 'please input str1 end by enter:','$' qkey DB 0DH,0AH,'please input str2 end by enter:','$' skey DB 0DH,0AH,'MATCH','$' tkey DB 0DH,0AH,'NO MATCH','$' BUFF1 DB 101,0,101 DUP(0) BUFF2 DB 101,0,101 DUP(0)`DATA ENDSCODE SEGMENT ASSUME CS:CODE,DS:DATA START: MOV AX,DATA MOV DS,AX MOV ES,AX ; Data segment and stack segment initialization LEA DX,pkey MOV AH,9 INT 21H ;DOS function call, output string LEA DX,BUFF1 MOV AH,0AH INT 21H ; Enter the first string into BUFF1 buffer LEA DX,qkey MOV AH,9 INT 21H LEA DX,BUFF2 MOV AH,0AH INT 21H ; Enter the second string into BUFF2 buffer LEA SI,BUFF1 ; Give the first string to SI LEA DI,BUFF2 ; Give the second string to DI MOV CX,53 Give the counter value CLD Clear direction standard DF REPZ CMPSB ; Byte-by-byte comparison string, SI and DI incremented JZ MATCH All bytes are equal output MATCH LEA DX,tkey MOV AH,9 INT 21H ; unequal output NO MATCH JMP START MATCH: LEA DX,skey MOV AH,9 INT 21H DOS function call, output MATCH JMP STARTCODE ENDS END START1.4 program running results and analysis:
The main thing is to learn the use of CMPSB instructions, clear the DF flag to increment DI,SI, and then achieve a byte by byte comparison.
Figure 7 String matching design results
2. Character string input and display design 2.1 Design requirements:
It is required to master the display method of characters and data. Design procedures to receive keyboard input string, including characters and numbers, and display in the terminal. Design requirements: There are typing and end prompts, such as INPUT STRING,THE END FLAG IS $. Required statistics type string numbers (0-9) and the number of characters, lines displayed.
2.2 Design Idea:
it is jud that DI is accumulated in that range of 0 to 9, SI is accumulate in the ranges of a to z and A to Z, and SI and DI, i. e., the number of letters and the numb of digits, are respectively converted into decimal and output.
2.3 Program list: DATA SEGMENT pkey DB 0dh,0ah,"input string,the end flag is $" skey DB 0dh,0ah,'abc length is:','$' tkey DB 0dh,0ah,'num length is:','$' DATA ENDSCODE SEGMENT ASSUME CS:CODE,DS:DATA START: MOV AX,DATA MOV DS,AX MOV ES,AX ; data segment stack segment initialization LEA DX,pkey MOV AH,9 INT 21H ;DOS function call, output string MOV DX,'$' MOV AH,2 INT 21H ;DOS function call, output $ MOV DX,3AH MOV AH,2 INT 21H ;DOS function call, output: MOV DX,0DH MOV AH,2 INT 21H DOS function call, homing MOV DX,0AH MOV AH,2 INT 21H DOS function call, line feed MOV SI,0 MOV DI,0 ; Clear, SI stores the number of letters, DI stores the number of digits INPUT: MOV AH,1 INT 21H CMP AL,'$' JZ INEND CMP AL,'z' JA INPUT CMP AL,'a' JAE ABC1 ; Input is a~f,SI plus 1 CMP AL,'Z' JA INPUT CMP AL,'A' JAE ABC1 ; Input A~F,SI also plus 1 CMP AL,'9' JA INPUT CMP AL,'0' JAE NUM1 ; Input 0~9,DI plus 1 JMP INPUT ABC1: INC SI JMP INPUT NUM1: INC DI JMP INPUT INEND: MOV BX,0 MOV CX,10 MOV AX,SI ; Give AX the number of letters (hexadecimal to decimal) LP1: MOV DX,0 DIV CX 32-digit number represented by AX divided by 10, quotient in AX, remainder in DX PUSH DX Push the remainder into the stack INC BX CMP AX,0 If the quotient is not 0, continue dividing by 10. JNZ LP1 LEA DX,skey MOV AH,9 INT 21H ;DOS function call, output string LLP1: POP DX Data in DX is pushed out of the stack and displayed in turn. ADD DX,30H MOV AH,2 INT 21H DOS function call, output letter number DEC BX JNZ LLP1 MOV BX,0 MOV AX,DI ; Number of digits given to AX(hexadecimal to decimal) LP2: MOV DX,0 DIV CX 32-digit number represented by AX divided by 10, quotient in AX, remainder in DX PUSH DX Push the remainder into the stack INC BX CMP AX,0 JNZ LP2 If the quotient is not 0, continue dividing by 10. LEA DX,tkey MOV AH,9 INT 21H ;DOS function call, output string LLP2: POP DX Data in DX is pushed out of the stack and displayed in turn. ADD DX,30H MOV AH,2 INT 21H DOS function call, output number DEC BX JNZ LLP2 JMP START CODE ENDS END START2.4 program running results and analysis:
When the number of input letters or numbers is greater than 10, the statistical output is more troublesome. Here, the method of dividing by 10 cycles and storing them in the stack is used to realize the output in turn.
Figure 8 Character string input and display design results
About how to achieve string operation in assembly language to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.