Intel汇编语言程序设计(第7版)第四章编程练习题答案

1. 大端序转成小端序

c 复制代码
.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. 交换数组元素对

c 复制代码
.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. 数组元素间隔之和

c 复制代码
.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. 将字数组复制到双字数组

c 复制代码
.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. 斐波那契数列

c 复制代码
.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. 数组反向

c 复制代码
.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. 将字符串复制为反向

c 复制代码
.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. 数组元素位移

c 复制代码
.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

(完)

相关推荐
小小工匠2 个月前
Maven - Assembly实战
maven·assembly
codists4 个月前
《Programming from the Ground Up》阅读笔记:p95-p102
assembly·编程人
codists5 个月前
《Programming from the Ground Up》阅读笔记:p88-p94
assembly·编程人
codists5 个月前
《Programming from the Ground Up》阅读笔记:p75-p87
assembly·编程人
codists6 个月前
《Programming from the Ground Up》阅读笔记:p19-p48
assembly·编程人
codists6 个月前
《Programming from the Ground Up》阅读笔记:p1-p18
assembly
Kiopler1 年前
Intel汇编语言程序设计(第7版)第六章编程学习过程中写的小例子
汇编·assembly
嵌入式底层1 年前
汇编攻城记-Cortex-M3指令集
开发语言·汇编·assembly