Intel汇编语言程序设计(第7版)第四章编程练习题答案
1. 大端序转成小端序
.386
.model flat, stdcall
option casemap:none include windows.inc
include kernel32.inc
includelib kernel32.lib
include user32.inc
includelib user32.lib.stack 4096.data
bigEndian BYTE 12h, 34h, 56h, 78h
littleEndian DWORD ?Fmt BYTE "0x%08X", 0
Caption BYTE "Caption", 0
Buf BYTE 64 DUP(0)
.code start:mov eax, DWORD PTR [bigEndian]mov littleEndian, eaxinvoke wsprintf, OFFSET Buf, OFFSET Fmt, [littleEndian] invoke MessageBox, NULL, OFFSET Buf, OFFSET Caption, MB_OKinvoke ExitProcess, 0ret
end start
2. 交换数组元素对
.386
.model flat, stdcall
option casemap:none include windows.inc
include kernel32.inc
includelib kernel32.lib
include user32.inc
includelib user32.lib
include msvcrt.inc
includelib msvcrt.lib.stack 4096.data
Array BYTE 11h, 22h, 33h, 44h, 55h, 66h, 77h, 88h
ArrayLen DWORD ($ - Array) / SIZEOF WORD
TmpBuf BYTE 0Pause BYTE "pause", 0
Fmt BYTE "0x%02X ", 0
Buf BYTE 64 DUP(0)
.code start:mov ecx, ArrayLenxor ebx, ebx
Exchange:mov al, [Array + ebx]mov TmpBuf, al ; 保存第1个数inc ebx mov al, [Array + ebx] ; 保存第2个数dec ebxmov [Array + ebx], al ; 赋值第1个数inc ebx mov al, TmpBuf mov [Array + ebx], al ; 赋值第2个数inc ebxloop Exchangexor ebx, ebx
Print:mov al, [Array + ebx]invoke crt_printf, OFFSET Fmt, al inc ebxcmp ebx, LENGTHOF Arrayjz Endingjmp PrintEnding:invoke crt_system, OFFSET Pauseinvoke ExitProcess, 0
end start
3. 数组元素间隔之和
.386
.model flat, stdcall
option casemap:none include windows.inc
include kernel32.inc
includelib kernel32.lib
include user32.inc
includelib user32.lib
include msvcrt.inc
includelib msvcrt.lib.stack 4096.data
Array DWORD 0, 2, 5, 9, 10
ArrayLen DWORD ($ - Array) / SIZEOF DWORD
TmpBuf DWORD 0Pause BYTE "pause", 0
Fmt BYTE "%d", 0
Buf BYTE 64 DUP(0)
.code start:mov ecx, ArrayLenxor edx, edxmov ebx, TYPE Array
SUMTWO:mov eax, [Array + ebx]mov TmpBuf, eaxsub ebx, TYPE Arraymov eax, [Array + ebx]sub TmpBuf, eaxadd edx, TmpBufadd ebx, TYPE Arrayloop SUMTWOinvoke crt_printf, OFFSET Fmt, edx
Ending:invoke crt_system, OFFSET Pauseinvoke ExitProcess, 0
end start
4. 将字数组复制到双字数组
.386
.model flat, stdcall
option casemap:none include windows.inc
include kernel32.inc
includelib kernel32.lib
include user32.inc
includelib user32.lib
include msvcrt.inc
includelib msvcrt.lib.stack 4096.data
wArray WORD 5, 9, 12, 31, 46, 68
ArrayLen DWORD ($ - wArray) / SIZEOF WORD
dwArray DWORD 32 DUP(0)
TmpBuf DWORD 0Pause BYTE "pause", 0
Fmt BYTE "%d ", 0
Buf BYTE 64 DUP(0)
.code start:mov ecx, ArrayLenxor eax, eaxxor esi, esi
L0:movzx eax, WORD PTR [wArray + esi * TYPE wArray]mov [dwArray + esi * TYPE dwArray], eax inc esiloop L0xor edi, edi
L1:invoke crt_printf, OFFSET Fmt, DWORD PTR [dwArray + edi * TYPE dwArray]inc edicmp edi, ArrayLenjz Endingjmp L1Ending:invoke crt_system, OFFSET Pauseinvoke ExitProcess, 0
end start
5. 斐波那契数列
.386
.model flat, stdcall
option casemap:none include windows.inc
include kernel32.inc
includelib kernel32.lib
include user32.inc
includelib user32.lib
include msvcrt.inc
includelib msvcrt.libFIBNUM = 7.stack 4096.data
Fib0 DWORD 0
Fib1 DWORD 1
dwArray DWORD 64 DUP(0)Pause BYTE "pause", 0
Fmt BYTE "%d ", 0.code start:mov eax, Fib1mov ecx, FIBNUMxor edi, edi
L0:mov [dwArray + edi * TYPE dwArray], eaxadd eax, Fib0mov ebx, Fib1mov Fib0, ebxmov Fib1, eaxinc ediloop L0xor edi, edi
L1:invoke crt_printf, OFFSET Fmt, DWORD PTR [dwArray + edi * TYPE dwArray]inc edicmp edi, FIBNUMjz Endingjmp L1Ending:invoke crt_system, OFFSET Pauseinvoke ExitProcess, 0
end start
6. 数组反向
.386
.model flat, stdcall
option casemap:none include windows.inc
include kernel32.inc
includelib kernel32.lib
include user32.inc
includelib user32.lib
include msvcrt.inc
includelib msvcrt.lib.stack 4096.data
dwArray DWORD 15, 21, 34, 49, 52, 98
ArrayLen DWORD LENGTHOF dwArray - 1Pause BYTE "pause", 0
Fmt BYTE "%d ", 0.code start:mov edi, ArrayLenxor esi, esi mov ecx, 3
L0:mov eax, [dwArray + edi * SIZEOF DWORD]xchg [dwArray + esi * SIZEOF DWORD], eaxxchg [dwArray + edi * SIZEOF DWORD], eaxdec ediinc esi loop L0L3:xor edi, edi
L1:invoke crt_printf, OFFSET Fmt, DWORD PTR [dwArray + edi * TYPE dwArray]inc edicmp edi, LENGTHOF dwArrayjz Endingjmp L1Ending:invoke crt_system, OFFSET Pauseinvoke ExitProcess, 0
end start
7. 将字符串复制为反向
.386
.model flat, stdcall
option casemap:none include windows.inc
include kernel32.inc
includelib kernel32.lib
include user32.inc
includelib user32.lib
include msvcrt.inc
includelib msvcrt.lib.stack 4096.data
source BYTE "This is the source string", 0
srcLen DWORD ($ - source) / TYPE source - 2
target BYTE SIZEOF source DUP('$')Pause BYTE "pause", 0
Fmt BYTE "%c", 0.code start:mov ecx, srcLenxor edi, edi
L0:mov al, [source + ecx]mov [target + edi], alinc ediloop L0mov al, [source + ecx]mov [target + edi], alinc edimov BYTE PTR [target + edi], 0
L3:xor edi, edi
L1:invoke crt_printf, OFFSET Fmt, BYTE PTR [target + edi * TYPE target]inc edicmp edi, LENGTHOF targetjz Endingjmp L1Ending:invoke crt_system, OFFSET Pauseinvoke ExitProcess, 0
end start
8. 数组元素位移
.386
.model flat, stdcall
option casemap:none include windows.inc
include kernel32.inc
includelib kernel32.lib
include user32.inc
includelib user32.lib
include msvcrt.inc
includelib msvcrt.lib.stack 4096.data
dwArray DWORD 10, 20, 30, 40, 50, 60
AryLen DWORD LENGTHOF dwArray - 1TmpCh DWORD 0Pause BYTE "pause", 0
Fmt BYTE "%d ", 0.code start:mov ecx, AryLenxor edi, edi; 保存最后一个数字mov ebx, [dwArray + TYPE dwArray * ecx]mov TmpCh, ebxdec ecx
L0:mov eax, [dwArray + TYPE dwArray * ecx]inc ecx mov [dwArray + TYPE dwArray * ecx], eaxdec ecx loop L0mov eax, [dwArray + TYPE dwArray * ecx]inc ecxmov [dwArray + TYPE dwArray * ecx], eaxdec ecxmov eax, TmpChmov [dwArray + TYPE dwArray * ecx], eaxxor edi, edi
L1:invoke crt_printf, OFFSET Fmt, BYTE PTR [dwArray + edi * TYPE dwArray]inc edicmp edi, LENGTHOF dwArrayjz Endingjmp L1Ending:invoke crt_system, OFFSET Pauseinvoke ExitProcess, 0
end start
(完)