1. 根据书上的例子, 自己写的4个过程, 改了一部分
c
include irvine32.inc
includelib irvine32.lib
include msvcrt.inc
includelib msvcrt.lib
.data
dwNum0 DWORD 15
dwNum1 DWORD 21
PDWORD TYPEDEF PTR DWORD
dwNumAry DWORD 25, 39, 14, 59
NumAryLen DWORD LENGTHOF dwNumAry
.code
; 找到两个数中的更大数
BigOne PROC Num0:DWORD, Num1:DWORD
mov eax, Num0
cmp eax, Num1
ja NUM0BIG
je EQUAL
mov eax, Num1
jmp NUM0BIG
EQUAL:
xor eax, eax
NUM0BIG:
ret
BigOne ENDP
; 数组求最大值
Biggest PROC USES esi ebx ecx pdwAry:PDWORD, dwCnt:DWORD
mov ebx, pdwAry
mov ecx, dwCnt
mov esi, 1
mov eax, DWORD PTR [ebx]
L0:
cmp eax, DWORD PTR [ebx + esi * TYPE DWORD]
jae NOMAX
mov eax, DWORD PTR [ebx + esi * TYPE DWORD]
NOMAX:
inc esi
dec ecx
or ecx, ecx
jnz L0
ret
Biggest ENDP
; 直到按下按键在退出循环
BrkUntilPushKey PROC
L0:
mov eax, 100
call Delay
call ReadKey
jz L0
ret
BrkUntilPushKey ENDP
; 在数组中找到对应值, 找到返回1否则0
SearchNum PROC USES esi ecx pdwAry:PDWORD, dwAryCnt:DWORD, dwObj:DWORD
mov esi, pdwAry
mov eax, dwObj
mov ecx, dwAryCnt
L0:
cmp eax, DWORD PTR [esi]
je found
add esi, TYPE dwAryCnt
dec ecx
test ecx, ecx
jnz L0
jmp notfound
found:
mov eax, 1
jmp quit
notfound:
xor eax, eax
quit:
ret
SearchNum ENDP
start:
push 39
push NumAryLen
push OFFSET dwNumAry
call SearchNum
test eax, eax
jz Ending
call WriteDec
call WaitMsg
Ending:
invoke ExitProcess, 0
end start
2. 书上实现xor加密的例子
c
include irvine32.inc
includelib irvine32.lib
include msvcrt.inc
includelib msvcrt.lib
.data
; 字节类型的指针
PBYTE TYPEDEF PTR BYTE
INPUTBUFSIZE = 256
RetLine BYTE 0dh, 0ah, 0
szInputPrompt BYTE "输入明文: ", 0
szOutputPrompt BYTE "解密后的明文: ", 0
pszInput PBYTE OFFSET szInputPrompt
szInputBuf BYTE INPUTBUFSIZE DUP(0)
pszInputBuf PBYTE OFFSET szInputBuf
g_dwInputLen DWORD 0
g_bKey BYTE 0
.code
GenKey PROC
call Randomize
call Random32
and eax, 0FFh
ret
GenKey ENDP
NextLine PROC
push eax
mov edx, OFFSET RetLine
call WriteString
pop eax
ret
NextLine ENDP
SymCryptic PROC USES esi pbTextBuf:PBYTE, dwTextSize:DWORD, bKey:BYTE
xor esi, esi
xor eax, eax
mov ebx, pbTextBuf
L0:
mov al, BYTE PTR [ebx + esi]
xor al, bKey
mov BYTE PTR [ebx + esi], al
inc esi
cmp esi, dwTextSize
jb L0
ret
SymCryptic ENDP
; 显示提示内容的封装函数
PrintPrompt PROC USES edx pShownString:PBYTE, pInputBuf:PBYTE, dwInputBufSize:DWORD
; 打印提示输入
mov edx, pShownString
call WriteString
; 让用户输入内容
mov edx, pInputBuf
mov ecx, dwInputBufSize
call ReadString
ret
PrintPrompt ENDP
start:
; 打印输入提示
push SIZEOF szInputBuf
push OFFSET szInputBuf
push OFFSET szInputPrompt
call PrintPrompt
mov g_dwInputLen, eax
; 生成密钥
call GenKey
mov g_bKey, al
; 进行对称加密
invoke SymCryptic, pszInputBuf, g_dwInputLen, g_bKey
mov edx, OFFSET szInputBuf
call WriteString
; 换行
call NextLine
; 显示解密后的明文
mov edx, OFFSET szOutputPrompt
call WriteString
; 进行对称解密
invoke SymCryptic, pszInputBuf, g_dwInputLen, g_bKey
mov edx, pszInputBuf
call WriteString
; 换行
call NextLine
call WaitMsg
invoke ExitProcess, 0
end start
3. 计算数组内比50大的元素之和
c
include irvine32.inc
includelib irvine32.lib
include msvcrt.inc
includelib msvcrt.lib
.data
; 字节类型的指针
PBYTE TYPEDEF PTR BYTE
array SDWORD 10, 60, 20, 33, 72, 89, 45, 65, 72, 18
ArraySize SDWORD ($ - array) / TYPE array
sample SDWORD 50
index SDWORD 0
sum SDWORD 0
.code
start:
xor ecx, ecx
mov esi, OFFSET array
xor eax, eax
L0:
cmp ecx, ArraySize
jge Ending
mov ebx, sample
cmp ebx, SDWORD PTR [esi]
jge NextLoop
add eax, SDWORD PTR [esi]
NextLoop:
inc ecx
add esi, TYPE array
jmp L0
Ending:
mov sum, eax
call WriteInt
call WaitMsg
invoke ExitProcess, 0
end start
4. 表驱动选择
如果选择分支过多可以构造一张表专门指向对应的地址
c
include irvine32.inc
includelib irvine32.lib
include msvcrt.inc
includelib msvcrt.lib
.data
CaseTable BYTE 'A'
DWORD Process_A
EntrySize = ($ - CaseTable)
BYTE 'B'
DWORD Process_B
BYTE 'C'
DWORD Process_C
BYTE 'D'
DWORD Process_D
NumberOfEntries = ($ - CaseTable) / EntrySize
Prompt BYTE "Press capital A, B, C or D: ", 0
msgA BYTE "Process_A", 0
msgB BYTE "Process_B", 0
msgC BYTE "Process_C", 0
msgD BYTE "Process_D", 0
.code
Process_A PROC
mov edx, OFFSET msgA
ret
Process_A ENDP
Process_B PROC
mov edx, OFFSET msgB
ret
Process_B ENDP
Process_C PROC
mov edx, OFFSET msgC
ret
Process_C ENDP
Process_D PROC
mov edx, OFFSET msgD
ret
Process_D ENDP
start:
mov edx, OFFSET Prompt
call WriteString
call ReadChar
mov ebx, OFFSET CaseTable
mov ecx, NumberOfEntries
L1:
cmp al, [ebx]
jne L2
call DWORD PTR [ebx + 1]
call WriteString
call Crlf
jmp L3
L2:
add ebx, EntrySize
loop L1
L3:
call WaitMsg
invoke ExitProcess, 0
end start
(完)