English | 简体中文
Translated with DeepL Translator
Using PC system machine resources, program design in assembly language to find the specified string in a text file.
(1) Design an operation interface that displays prompt operations, allows input of file names, input of strings to be found, etc.. (2) Display the contents of its text file, mark the found characters or strings with different colors or blinking. (3) Display the number found.
(1) A beautiful graphical interface design with friendly human-computer interaction. (2) implementation of string replacement function to replace the found string with a user-specified string and save it.
Variable Name | Type | Variable Value | Comments |
---|---|---|---|
handle_file | handle | ? | |
str_filename_original | byte | FILENAME_BUFFER_SIZE DUP(0) | |
str_filename_result | byte | "result.txt",0 | |
original | byte | CONTENT_BUFFER_SIZE DUP(0) | Strings in the original file |
str_find | byte | CONTENT_BUFFER_SIZE DUP(0) | The string to find |
str_replace | byte | CONTENT_BUFFER_SIZE DUP(0) | String to replace |
result | byte | CONTENT_BUFFER_SIZE DUP(0) | The strings in the final new file |
set_pos_ori | dword | CONTENT_BUFFER_SIZE DUP(0) | Array of the starting position of each "find" string |
set_pos_res | dword | CONTENT_BUFFER_SIZE DUP(0) | Array of the starting position of each "replace" string |
pos1 | dword | 0 | Tracks variable I and gives the required value to set_pos_ori |
pos2 | dword | 0 | Tracks variable J and gives the required value to set_pos_res |
count_filename | dword | ? | Counting the length of file names |
count_original | dword | ? | Count the length of strings in the file |
count_find | dword | ? | Count the length of strings to be found |
count_replace | dword | ? | Count the length of strings to be replaced |
count_result | dword | ? | Count the length of the strings in the new file |
count | dword | 0 | Count the number of target strings found |
i | dword | ? | Translated with www.DeepL.com/Translator (free version) |
j | dword | ? | ;INDEX FOR "RESULT". |
The key to this project is the implementation of the string lookup algorithm.
Implemented by calling the Win32 API
functions CreateFile
, ReadFile
, WriteFile
, CloseFile
.
To facilitate the find, we need a "pointer" to look up the string variable character by character from the beginning. We compare the characters pointed by the original
and find
variables, and if they are equal, we move them backwards by one at the same time; if they are not equal, the original
pointer moves on and the find
pointer returns to the starting position. In the whole process, each time we move the pointer, we need to check whether the pointer points to the last bit of the variable original
or find
.
To achieve the string substitution in the enhancement requirement, the best practice is to complete the string substitution when finding the string. This is done by.
- each time the target string is found, the traversal pointer is moved back to the beginning of the target string and the replacement string is written to the variable
result
according to the length of the replacement string. - when the looked-up character is not the target string, copy the character in the variable
original
that the traversal pointer points to into the variableresult
.
Since there is a highlighting requirement in the enhancement request, it is necessary to construct array variables set_pos_ori
and set_pos_res
to store the location of the target string (e.g., the first character). After the final file is saved, it is output by printing character by character, and when it reaches the position of the target string, the color of the printed text is modified by the function SetTextColor
defined by the Irvine
link library.
Visual Studio 2019
Main libraries used: link library Irvine32.inc
, console functions Win32 API
Visual Studio configuration to support assembly: 1.
- Configure
VC++ directory
,Linker -> General
,Additional Libraries directory
ofMicrosoft Macro Assember
, addIrvine
folder. Reference Link - Configure the
Linker -> Input
ofAdditional Dependencies
, add the corresponding .lib file in theIrvine
folder. Solution Explorer -> Project
right-click menu (right click on the project title) -> Generate dependencies -> Generate custom files -> Check MASM- Assembly code highlighting helper plugin (optional): AsmDude
Note 1: AsmDude may not recognize the linked libraries under the Irvine
folder and will mark a red warning under the function, but this does not affect the normal compilation, the way to remove the warning is to manually copy the referenced linked library .inc
from the Irvine
folder to the same directory as .asm
(I have done this, but in reality they do not work).
Note 2: Irvine32
is actually a further wrapper around the Win32 API
, SmallWin.inc
contains the constant definitions, equivalent text, and function prototypes for the Win32 API.
- the code is uniformly indented with Tab, and the Tab length is set to 5 spaces.
- lowercase instructions in the code.
- variable names in code are lowercase, underscored, and the first word describes the variable type.
- code, subroutine naming uniformly begins with an underscore and lowercase.
- comments are uniformly capitalized.
- the functions encapsulated in the Irvine library use camel naming, and use the call instruction when calling.
- the Win32 API functions are uniformly named with a camel and invoked with the invoke instruction.
Assembly Language for x86 Processors 7th edition - Kip Irvine
tenghiz/solutions-for-parts-4-16
StackOverflow/assembly-replace-substring-with-another-string