1. 设置文本颜色
.data
Pause BYTE "pause", 0
Fmt BYTE "%d ", 0
hConsole HANDLE NULL
str0 BYTE "Hello world!", 0dh, 0ah, 0
str1 BYTE "What do you want?", 0dh, 0ah, 0
str2 BYTE "Do you like code?", 0dh, 0ah, 0
StrPtrsAry DWORD OFFSET str0, OFFSET str1, OFFSET str2
ForegroundColors DWORD FOREGROUND_GREEN, FOREGROUND_BLUE, FOREGROUND_RED
.code
start:
invoke GetStdHandle, STD_OUTPUT_HANDLE
mov hConsole, eax
xor esi, esi
L1:
invoke SetConsoleTextAttribute, hConsole, [ForegroundColors + esi * SIZEOF DWORD]
invoke crt_printf, [StrPtrsAry + esi * SIZEOF DWORD]
inc esi
cmp esi, 3
je L2
jmp L1
L2:
invoke crt_system, OFFSET Pause
invoke ExitProcess, 0
end start
2. 链接数组项
.data
Pause BYTE "pause", 0
Fmt BYTE "%c ", 0
start1 = 1
chars BYTE 'H', 'A', 'C', 'E', 'B', 'D', 'F', 'G', 0
links DWORD 0, 4, 5, 6, 2, 3, 7, 0
objChars BYTE 8 DUP(0)
.code
start:
mov ecx, 8
mov esi, start1
xor edi, edi
L1:
mov ebx, [links + esi * TYPE links]
mov al, [chars + esi * TYPE chars]
mov [objChars + edi * TYPE objChars], al
inc edi
mov esi, ebx
loop L1
xor edi, edi
L3:
invoke crt_printf, OFFSET Fmt, [objChars + edi]
inc edi
cmp edi, 8
jz L2
jmp L3
L2:
invoke crt_system, OFFSET Pause
invoke ExitProcess, 0
end start
3. 简单加法(1)
include irvine32.inc
includelib irvine32.lib
.data
szPrompt1 BYTE "请输入第1个数: ", 0
szPrompt2 BYTE "请输入第2个数: ", 0
iFirstNum DWORD ?
rows BYTE ?
cols BYTE ?
.code
GotoPos PROC USES edx bRows:BYTE, bCols:BYTE
mov dh, bRows
mov dl, cols
call Gotoxy
ret
GotoPos ENDP
IncRow PROC USES ebx eax pNum:DWORD
mov eax, pNum
mov bl, BYTE PTR [eax]
inc bl
mov eax, pNum
mov [eax], bl
ret
IncRow ENDP
start:
call Clrscr
call GetMaxXY
mov rows, al
mov cols, dl
movzx ax, rows
mov bl, 3
div bl
mov rows, al
movzx ax, cols
mov bl, 3
div bl
mov cols, al
invoke GotoPos, rows, cols
; 提示用户输入第1个数并保存
mov edx, OFFSET szPrompt1
call WriteString
call ReadDec
mov iFirstNum, eax
; 挪动到下一行
invoke IncRow, OFFSET rows
invoke GotoPos, rows, cols
; 提示用户输入第2个数
mov edx, OFFSET szPrompt2
call WriteString
call ReadDec
add eax, iFirstNum
; 挪动到下一行
invoke IncRow, OFFSET rows
invoke GotoPos, rows, cols
; 显示结果
call WriteDec
invoke IncRow, OFFSET rows
; 等待
call WaitMsg
invoke ExitProcess, 0
end start
4. 简单加法(2)
include irvine32.inc
includelib irvine32.lib
.data
szPrompt1 BYTE "请输入第1个数: ", 0
szPrompt2 BYTE "请输入第2个数: ", 0
iFirstNum DWORD ?
rows BYTE ?
cols BYTE ?
.code
GotoPos PROC USES edx bRows:BYTE, bCols:BYTE
mov dh, bRows
mov dl, cols
call Gotoxy
ret
GotoPos ENDP
start:
call Clrscr
call GetMaxXY
mov rows, al
mov cols, dl
movzx ax, rows
mov bl, 3
div bl
mov rows, al
movzx ax, cols
mov bl, 3
div bl
mov cols, al
mov ecx, 3
L1:
push ecx
; 提示用户输入第1个数并保存
invoke GotoPos, rows, cols
mov edx, OFFSET szPrompt1
call WriteString
call ReadDec
mov iFirstNum, eax
; 提示用户输入第2个数
call Clrscr
invoke GotoPos, rows, cols
mov edx, OFFSET szPrompt2
call WriteString
call ReadDec
add iFirstNum, eax
; 显示结果
invoke GotoPos, rows, cols
mov eax, iFirstNum
call WriteDec
; 等待
call WaitMsg
call Clrscr
pop ecx
loop L1
invoke ExitProcess, 0
end start
5. BetterRandomRange过程
include irvine32.inc
includelib irvine32.lib
.data
.code
BetterRandomRange PROC
sub eax, ebx
call RandomRange
add eax, ebx
ret
BetterRandomRange ENDP
start:
mov ebx, -300
mov eax, 100
call BetterRandomRange
call WriteInt
call WaitMsg
invoke ExitProcess, 0
end start
6. 随即字符串
include irvine32.inc
includelib irvine32.lib
.data
PBYTE TYPEDEF PTR BYTE
CharAry BYTE "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0
MyAry BYTE 64 DUP(0)
bCurRow BYTE 0
bCurCol BYTE 0
.code
GotoPos PROC USES edx bRows:BYTE, bCols:BYTE
mov dh, bRows
mov dl, bCols
call Gotoxy
ret
GotoPos ENDP
GenString PROC USES esi edi ecx ebx AryAddr:PBYTE
mov ecx, eax
xor esi, esi
xor edi, edi
mov ebx, AryAddr
call Randomize
L1:
mov eax, 26
call RandomRange
mov esi, eax
mov al, BYTE PTR [CharAry + esi * TYPE CharAry]
mov BYTE PTR [ebx + edi * TYPE CharAry], al
inc esi
inc edi
loop L1
mov BYTE PTR [ebx + edi * TYPE CharAry], 0
mov eax, AryAddr
ret
GenString ENDP
start:
mov ecx, 20
L1:
mov eax, 20
invoke GenString, OFFSET MyAry
mov edx, eax
invoke GotoPos, bCurRow, bCurCol
call WriteString
mov al, bCurRow
inc al
xchg al, bCurRow
mov eax, 100
call Delay
loop L1
call WaitMsg
invoke ExitProcess, 0
end start
7. 随机屏幕位置
include irvine32.inc
includelib irvine32.lib
.data
CharAry BYTE "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
CharArySize DWORD LENGTHOF CharAry
bCurRow BYTE 0
bCurCol BYTE 0
bMaxRow BYTE 0
bMaxCol BYTE 0
.code
GotoPos PROC USES edx bRows:BYTE, bCols:BYTE
mov dh, bRows
mov dl, bCols
call Gotoxy
ret
GotoPos ENDP
PrintRandomChar PROC
mov eax, CharArySize
call RandomRange
mov al, [CharAry + eax * TYPE CharAry]
call WriteChar
ret
PrintRandomChar ENDP
GotoRandomPos PROC USES edx
movzx eax, bMaxRow
call RandomRange
mov dh, al
movzx eax, bMaxCol
call RandomRange
mov dl, al
call Gotoxy
ret
GotoRandomPos ENDP
GetMaxScreen PROC USES eax ebx
call GetMaxXY
mov bMaxRow, al
mov bMaxCol, dl
ret
GetMaxScreen ENDP
start:
call Randomize
call GetMaxScreen
mov ecx, 100
L1:
call GotoRandomPos
call PrintRandomChar
mov eax, 1000
call Delay
loop L1
call WaitMsg
invoke ExitProcess, 0
end start
8. 颜色矩形
include irvine32.inc
includelib irvine32.lib
.data
dwBkClr DWORD 0
dwFgClr DWORD 0
ColorAry DWORD 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
ColorAryLen DWORD LENGTHOF ColorAry
OriginClr BYTE 0
.code
LoadOriginColor PROC USES ebx
xor ebx, ebx
mov bl, OriginClr
shr bl, 4
shl bl, 4
movzx eax, bl
xor ebx, ebx
mov bl, OriginClr
and bl, 0Fh
add eax, ebx
call SetTextColor
ret
LoadOriginColor ENDP
start:
call GetTextColor
mov OriginClr, al
mov ecx, ColorAryLen
xor esi, esi
xor eax, eax
L1:
push ecx
mov ecx, ColorAryLen
xor edi, edi
L2:
mov eax, DWORD PTR [ColorAry + esi * TYPE ColorAry]
SHL eax, 4
add eax, DWORD PTR [ColorAry + edi * TYPE ColorAry]
call SetTextColor
mov al, 'A'
call WriteChar
inc edi
loop L2
inc esi
pop ecx
loop L1
call LoadOriginColor
call WaitMsg
invoke ExitProcess, 0
end start
9. 递归过程
include irvine32.inc
includelib irvine32.lib
include msvcrt.inc
includelib msvcrt.lib
.data
iTimes DWORD 0
.code
Recursive PROC
cmp ecx, 0
jz Ending
inc iTimes
dec ecx
invoke Recursive
Ending:
ret
Recursive ENDP
start:
mov ecx, 5
call Recursive
mov eax, iTimes
call WriteDec
call WaitMsg
invoke ExitProcess, 0
end start
10. 斐波那契生成器
include irvine32.inc
includelib irvine32.lib
.data
PDWORD TYPEDEF PTR DWORD
NextLine BYTE 0dh, 0ah, 0
FibAry DWORD 64 DUP(0)
dwTmp DWORD 0
N = 47
.code
CalcFibs PROC pFibAry:PDWORD, dwCnt:DWORD
mov ecx, dwCnt
mov edx, pFibAry
xor edi, edi
xor eax, eax
mov ebx, 1
L1:
mov DWORD PTR [edx + TYPE DWORD * edi], ebx
mov dwTmp, ebx
add ebx, eax
mov eax, dwTmp
inc edi
loop L1
ret
CalcFibs ENDP
ShowAry PROC pAry:PDWORD, Cnt:DWORD
mov ecx, Cnt
xor esi, esi
mov ebx, pAry
L1:
mov eax, DWORD PTR [ebx + TYPE DWORD * esi]
call WriteDec
mov edx, OFFSET NextLine
call WriteString
inc esi
loop L1
ret
ShowAry ENDP
start:
invoke CalcFibs, OFFSET FibAry, N
invoke ShowAry, OFFSET FibAry, N
call WaitMsg
invoke ExitProcess, 0
end start
11. 找出K的倍数
include irvine32.inc
includelib irvine32.lib
include msvcrt.inc
includelib msvcrt.lib
.data
PBYTE TYPEDEF PTR BYTE
NextLine BYTE 0dh, 0ah, 0
Space BYTE 20h
N = 50
bNumAry BYTE N DUP(0)
.code
GetNoPrime PROC K:DWORD
mov esi, 1
L1:
mov eax, K
mul esi
cmp eax, N
jge Ending
mov BYTE PTR [bNumAry + eax], 1
inc esi
jmp L1
Ending:
ret
GetNoPrime ENDP
ShowAry PROC pAry:PBYTE, Cnt:DWORD
mov ecx, Cnt
xor esi, esi
mov ebx, pAry
xor eax, eax
L1:
mov al, BYTE PTR [ebx + esi]
call WriteDec
mov al, Space
call WriteChar
inc esi
loop L1
mov edx, OFFSET NextLine
call WriteString
ret
ShowAry ENDP
start:
invoke GetNoPrime, 2
invoke ShowAry, OFFSET bNumAry, N
invoke crt_memset, OFFSET bNumAry, 0, N
invoke GetNoPrime, 3
invoke ShowAry, OFFSET bNumAry, N
call WaitMsg
invoke ExitProcess, 0
end start
(完)