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, eax
	invoke wsprintf, OFFSET Buf, OFFSET Fmt, [littleEndian] 
	invoke MessageBox, NULL, OFFSET Buf,  OFFSET Caption, MB_OK

	invoke ExitProcess, 0
	ret
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			0

Pause BYTE "pause", 0
Fmt BYTE "0x%02X ", 0
Buf BYTE 64 DUP(0)
.code 

start:
	mov ecx, ArrayLen
	xor ebx, ebx 
Exchange:
	mov al, [Array + ebx]
	mov TmpBuf, al				; 保存第1个数
	inc ebx 
	mov al, [Array + ebx]		; 保存第2个数
	dec ebx
	mov [Array + ebx], al		; 赋值第1个数
	inc ebx 
	mov al, TmpBuf		
	mov [Array + ebx], al		; 赋值第2个数
	inc ebx
	loop Exchange
	
	xor ebx, ebx
Print:
	mov al, [Array + ebx]
	invoke crt_printf, OFFSET Fmt, al 
	inc ebx
	cmp ebx, LENGTHOF Array
	jz Ending
	jmp Print
	
Ending:
	invoke crt_system, OFFSET Pause
	invoke 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			0

Pause BYTE "pause", 0
Fmt BYTE "%d", 0
Buf BYTE 64 DUP(0)
.code 

start:
	mov ecx, ArrayLen
	xor edx, edx
	mov ebx, TYPE Array
SUMTWO:
	mov eax, [Array + ebx]
	mov TmpBuf, eax
	sub ebx, TYPE Array
	mov eax, [Array + ebx]
	sub TmpBuf, eax
	add edx, TmpBuf
	add ebx, TYPE Array
	loop SUMTWO
	
	invoke crt_printf, OFFSET Fmt, edx
Ending:
	invoke crt_system, OFFSET Pause
	invoke 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			0

Pause BYTE "pause", 0
Fmt BYTE "%d ", 0
Buf BYTE 64 DUP(0)
.code 

start:
	mov ecx, ArrayLen
	xor eax, eax
	xor esi, esi
L0:
	movzx eax, WORD PTR [wArray + esi * TYPE wArray]
	mov [dwArray + esi * TYPE dwArray], eax  
	inc esi
	loop L0
	
	xor edi, edi
L1:
	invoke crt_printf, OFFSET Fmt, DWORD PTR [dwArray + edi * TYPE dwArray]
	inc edi
	cmp edi, ArrayLen
	jz Ending
	jmp L1
	
Ending:
	invoke crt_system, OFFSET Pause
	invoke 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.lib

FIBNUM = 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, Fib1
	mov ecx, FIBNUM
	xor edi, edi
L0:
	mov [dwArray + edi * TYPE dwArray], eax
	add eax, Fib0
	mov ebx, Fib1
	mov Fib0, ebx
	mov Fib1, eax
	inc edi
	loop L0
	xor edi, edi
L1:
	invoke crt_printf, OFFSET Fmt, DWORD PTR [dwArray + edi * TYPE dwArray]
	inc edi
	cmp edi, FIBNUM
	jz Ending
	jmp L1
	
Ending:
	invoke crt_system, OFFSET Pause
	invoke 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 - 1

Pause BYTE "pause", 0
Fmt BYTE "%d ", 0

.code 

start:
	mov edi, ArrayLen
	xor esi, esi 
	mov ecx, 3
L0:
	mov eax, [dwArray + edi * SIZEOF DWORD]
	xchg [dwArray + esi * SIZEOF DWORD], eax
	xchg [dwArray + edi * SIZEOF DWORD], eax
	dec edi
	inc esi 
	loop L0
	
L3:
	xor edi, edi
L1:
	invoke crt_printf, OFFSET Fmt, DWORD PTR [dwArray + edi * TYPE dwArray]
	inc edi
	cmp edi, LENGTHOF dwArray
	jz Ending
	jmp L1
	
Ending:
	invoke crt_system, OFFSET Pause
	invoke 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, srcLen
	xor edi, edi
L0:
	mov al, [source + ecx]
	mov [target + edi], al
	inc edi
	loop L0
	mov al, [source + ecx]
	mov [target + edi], al
	inc edi
	mov BYTE PTR [target + edi], 0
L3:
	xor edi, edi
L1:
	invoke crt_printf, OFFSET Fmt, BYTE PTR [target + edi * TYPE target]
	inc edi
	cmp edi, LENGTHOF target
	jz Ending
	jmp L1
	
Ending:
	invoke crt_system, OFFSET Pause
	invoke 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 - 1

TmpCh	DWORD  0

Pause BYTE "pause", 0
Fmt BYTE "%d ", 0

.code 

start:
	mov ecx, AryLen
	xor edi, edi
	; 保存最后一个数字
	mov ebx, [dwArray + TYPE dwArray * ecx]
	mov TmpCh, ebx
	dec ecx
L0:
	mov eax, [dwArray + TYPE dwArray * ecx]
	inc ecx 
	mov [dwArray + TYPE dwArray * ecx], eax
	dec ecx 
	loop L0
	mov eax, [dwArray + TYPE dwArray * ecx]
	inc ecx
	mov [dwArray + TYPE dwArray * ecx], eax
	dec ecx
	mov eax, TmpCh
	mov [dwArray + TYPE dwArray * ecx], eax
	
	xor edi, edi
L1:
	invoke crt_printf, OFFSET Fmt, BYTE PTR [dwArray + edi * TYPE dwArray]
	inc edi
	cmp edi, LENGTHOF dwArray
	jz Ending
	jmp L1
	
Ending:
	invoke crt_system, OFFSET Pause
	invoke ExitProcess, 0
end start

(完)

你可能感兴趣的:(IA-32,x32控制台汇编,assembly,Ollydbg)