最近重新学习了一下X86汇编,毕竟自己都快把计算机原理的内容忘光了。我发现现在的教材还是万年不变的DOS汇编,网上的资料也很多是基于DOS的。这都是2020年了啊,为什么大家都还在学DOS下的汇编。忍不住要吐槽一下学校的教授们,你们真的是打算一套PPT用一辈子的嘛?我想起了以前的万年不变的TC2.0和VC++ 6.0。 有多少同学因为教材的过时而多花了不少时间,浪费了宝贵的青春。折腾了一宿,总算是弄出了一个可以在WIN10系统下应用的汇编。先写个简单的小程序来练手并备忘一些MASM32 的基本模板,程序的功能很简单,就是随便找的一个初学者连习题,就是比较当前目录下两个文本文件,找出不同的行。

.686
.model flat, C
printf PROTO C :VARARG  ; The secret sauce.. a prototype of printf
option casemap :none

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

.data
difLineCount   dd 0
FileNameA      db "testA.txt", 0
FileNameB      db "testB.txt", 0
ErrorMsg	   db "Cannot open file" ,0ah,0dh,0
SameMsg	       db "The two file is all same!" ,0ah,0dh,0
DiffMsg	       db "Line %d not same" ,0ah,0dh,0
.data?
hFileA HANDLE ?
hMemoryA HANDLE ?
pMemoryA DWORD ?
ReadSizeA DWORD ?
hFileB HANDLE ?
hMemoryB HANDLE ?
pMemoryB DWORD ?
ReadSizeB DWORD ?
.const
MEMORYSIZE equ 65535

.code

main:
xor eax,eax
invoke CreateFile, addr FileNameA, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
.if eax!=INVALID_HANDLE_VALUE		;if file exists ... then
	mov hFileA, eax

	invoke GlobalAlloc, GMEM_MOVEABLE or GMEM_ZEROINIT, MEMORYSIZE
	mov hMemoryA, eax
	invoke GlobalLock, hMemoryA
	mov pMemoryA, eax

	invoke ReadFile, hFileA, pMemoryA, MEMORYSIZE-1, addr ReadSizeA, NULL

		invoke CreateFile, addr FileNameB, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
		.if eax!=INVALID_HANDLE_VALUE		;if file exists ... then
		mov hFileB, eax

		invoke GlobalAlloc, GMEM_MOVEABLE or GMEM_ZEROINIT, MEMORYSIZE
		mov hMemoryB, eax
		invoke GlobalLock, hMemoryB
		mov pMemoryB, eax

		invoke ReadFile, hFileB, pMemoryB, MEMORYSIZE-1, addr ReadSizeB, NULL
		;invoke MessageBox, NULL, pMemoryB, addr FileNameB, MB_OK
		xor ecx,ecx
		xor edx,edx
		mov esi,pMemoryA
		mov edi,pMemoryB
READONE:
		mov eax,ReadSizeA
		mov ebx,ReadSizeB
		cmp eax,0
		je finished
		cmp ebx,0
		je finished
		mov dl,[esi]
		cmp dl,[edi]
		jne DifLine
		inc esi
		dec ReadSizeA
		inc edi
		dec ReadSizeB
		cmp dl,0dh
		jne READONE
		inc ecx
		jmp READONE
DifLine:
		inc difLineCount
		push ecx
LineA:
		cmp dl,0dh
		je LineB
		inc esi
		mov dl,[esi]
		dec ReadSizeA
		jmp LineA
LineB:
		mov dl,[edi]
		cmp dl,0dh
		je READONE
		inc edi
		dec ReadSizeB
		jmp LineB
finished:
		invoke GlobalUnlock, pMemoryB
		invoke GlobalFree, hMemoryB
		invoke CloseHandle, hFileB
		.else  ;if file does not texists ... than invoke the "Error" message box instead!
						invoke printf, addr ErrorMsg
						ret
		.endif
	invoke GlobalUnlock, pMemoryA
	invoke GlobalFree, hMemoryA
	invoke CloseHandle, hFileA
.else  ;if file does not texists ... than invoke the "Error" message box instead!
		invoke printf, addr ErrorMsg
		ret			
.endif
		cmp difLineCount,0
		jne dispDif
		invoke printf, addr SameMsg
		ret
dispDif:
		mov ecx,difLineCount
dispOneDifLine:
		pop eax
		inc eax
		push ecx
		invoke printf, addr DiffMsg,eax
		pop ecx
		loop dispOneDifLine
ret

end main

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据