;链接库测试 #3 TestLib3.asm;计算嵌套循环执行的时间
include irvine32.inc
.686p
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD
.data
OUTER_LOOP_COUNT =3
startTime DWORD ?
msg1 BYTE "Please wait ... ",0dh,0ah
msg2 BYTE "Elapsed milliseconds:",0dh,0ah
.code
main PROC
mov edx,OFFSET msg1 ;Please wait...
call WriteString
;保存开始时间
call GetMSeconds
mov startTime,eax
;开始外层循环
mov ecx,OUTER_LOOP_COUNT
L1:
call innerLoop
loop L1
;计算执行时间
call GetMSeconds
sub eax,startTime
;显示执行时间
mov edx,OFFSET msg2 ;"Elapsed milliseconds:"
call WriteString
call WriteDec ;输出毫秒数
call Crlf
exit
main ENDP
innerLoop PROC
pushad
mov ecx,0fffffffh
L1:
mul eax
mul eax
mul eax
loop L1
popad
ret
innerLoop ENDP
END main
10.USE运算符
include irvine32.inc
.686p
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD
.data
array DWORD 1,2,3,4,5,6.code
main PROC
mov ecx,lengthof array
mov esi,offset array
call ArraySum
call DumpRegs
call WaitMsg
invoke ExitProcess,0
main ENDP
ArraySum PROC USES esi ecx
mov eax,0
L1:
add eax,[esi]
add esi,TYPE DWORD
loop L1
ret
ArraySum ENDP
END main
习题
5.4.5 11
include irvine32.inc
.686p
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD
.data
prompt BYTE "please input a string of number:"
number DWORD 10DUP(?).code
main PROC
;显示提示信息
mov edx,offset prompt
call WriteString
mov ecx,10
L1:
call ReadInt
mov [edx],eax
add edx,4
loop L1
;打印字节数组内的信息
mov ecx,10
mov edx,offset prompt
L2:
mov eax,[edx]
call WriteInt
mov al,9
call WriteChar ;输出制表符
add edx,4
loop L2
call WaitMsg
invoke ExitProcess,0
main ENDP
END main
5.8.1 15
.686p
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD
.data
.code
main PROC
push 5
push 6
pop eax
pop eax
invoke ExitProcess,0
main ENDP
END main
5.8.1 16
.686p
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD
.data
.code
main PROC
push 10
push 20
call Ex2Sub
pop eax
invoke ExitProcess,0
main ENDP
Ex2Sub PROC
pop eax
ret ;发生错误的原因是这里把栈帧弹出 ret无法返回
Ex2Sub ENDP
END main
5.8.1 17
.686p
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD
.data
.code
main PROC
mov eax,30
push eax
push 40
call Ex3Sub
invoke ExitProcess,0
main ENDP
Ex3Sub PROC
pusha
mov eax,80
popa
ret
Ex3Sub ENDP
END main
5.8.1 18
.686p
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD
.data
.code
main PROC
mov eax,40
push offset Here
jmp Ex4Sub
Here:
mov eax,30
invoke ExitProcess,0
main ENDP
Ex4Sub PROC
ret
Ex4Sub ENDP
END main
5.8.1 19
.686p
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD
.data
.code
main PROC
mov edx,0
mov eax,40
push eax
call Ex5Sub
invoke ExitProcess,0
main ENDP
Ex5Sub PROC
pop eax
pop edx
push eax
ret
Ex5Sub ENDP
END main
5.8.1 20
.686p
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD
.data
array DWORD 4DUP(0).code
main PROC
mov eax,10
mov esi,0
call proc_1
add esi,4
add eax,10
mov array[esi],eax
invoke ExitProcess,0
main ENDP
proc_1 PROC
call proc_2
add esi,4
add eax,10
mov array[esi],eax
ret
proc_1 ENDP
proc_2 PROC
call proc_3
add esi,4
add eax,10
mov array[esi],eax
ret
proc_2 ENDP
proc_3 PROC
mov array[esi],eax
ret
proc_3 ENDP
END main
5.8.2 01
.686p
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD
.data
.code
main PROC
mov eax,1
mov ebx,2
push eax
push ebx
pop eax
pop ebx
invoke ExitProcess,0
main ENDP
END main
5.8.2 02
.686p
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD
.data
.code
main PROC
call Proc1
invoke ExitProcess,0
main ENDP
Proc1 PROC
mov eax,1;这一条指令占三个字节
ret
END Proc1
END main
5.8.2 03
.686p
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD
.data
arr dword 2dup(0).code
main PROC
mov edx,offset arr
mov eax,1000h
mov [edx+0],eax
mov eax,2000h
mov [edx+4],eax
invoke ExitProcess,0
main ENDP
END main
5.8.2 04
.686p
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD
.data
arr DWORD 1,2,3,4,5,6,7,8,9
tmp DWORD 9dup(0).code
main PROC
mov edx,[arr];将arr复制到tmp
mov ecx,lengthof arr-1
mov edi,0
L:
mov eax,arr[edi+4]
mov tmp[edi],eax
add edi,4
loop L
;将tmp拷贝回arr
mov ecx,lengthof arr
mov edi,0
R:
mov eax,[tmp+edi*4]
mov [arr+edi*4],eax
inc edi
loop R
mov [arr+edi*4-4],edx
invoke ExitProcess,0
main ENDP
END main
5.8.2 05
include irvine32.inc
.686p
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD
.data
.code
main PROC
call SubProc
call WaitMsg
invoke ExitProcess,0
main ENDP
SubProc PROC
pop eax
push eax
call DumpRegs
ret
SubProc ENDP
END main
5.9.01
include irvine32.inc
.686p
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD
.data
string BYTE "abcdefghijklmnopqrstuvwxyz",0.code
main PROC
mov ebx,1;表示颜色用来递增改变前景色
mov ecx,4
L:
mov eax,ebx
inc ebx
call SetTextColor
;显示文字
mov edx,offset string
call WriteString
call Crlf
loop L
invoke ExitProcess,0
main ENDP
END main
5.9.02
; Chapter 5 Exercise 3: Linking Array Items
.386.model flat,stdcall
.stack 4096
ExitProcess proto,dwExitCode:dword
.data
start dword 1
chars byte 'H','A','C','E','B','D','F','G'
links dword 0,4,5,6,2,3,7,0
count = LENGTHOF links
result byte count DUP(0).code
main proc
mov edi,0
mov esi,start ; first link index
mov ecx,count
L1:
mov al,chars[esi]; get next linked character
mov result[edi],al ; save in result array
inc edi
mov esi,links[esi*4]; find next link
loop L1
invoke ExitProcess,0
main endp
end main
5.9.03
include irvine32.inc
.686p
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD
.data
.code
main PROC
;定位光标
mov dh,10
mov dl,40
call Gotoxy
;输入两个数字并且求和
call ReadInt
mov ebx,eax
call Crlf
call ReadInt
add ebx,eax
mov eax,ebx
call WriteInt
call WaitMsg
invoke ExitProcess,0
main ENDP
END main
5.9.04
include irvine32.inc
.686p
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD
.data
.code
main PROC
mov ecx,3
L:
call Clrscr
;定位光标
mov dh,10
mov dl,40
call Gotoxy
;输入两个数字并且求和
call ReadInt
mov ebx,eax
call Crlf
call ReadInt
add ebx,eax
mov eax,ebx
call WriteInt
call WaitMsg
loop L
invoke ExitProcess,0
main ENDP
END main
5.9.05
; Chapter 5 Exercise 5: Better Random Range
COMMENT !
The RandomRange procedure from the Irvine32 library generates a
pseudorandom integer between 0and N -1. Your task is to create
an improved version that generates an integer between M and N -1.
Let the caller pass M in EBX and N in EAX. Write a short test program
that calls BetterRandomRange from a loop that repeats 50 times.
Display each randomly generated value.!
INCLUDE Irvine32.inc
.data
COUNT =50.code
main PROC
call Clrscr
mov ebx,-20
mov eax,50
mov ecx,COUNT
L1:
push eax
call BetterRandomRange
call WriteInt
call Crlf
pop eax
loop L1
quit:
exit
main ENDP
;-------------------------------------------------
BetterRandomRange PROC
;; Generates a random integer between M and N-1.; Receives:; EBX = lower range(M); EAX = upper range(N); Returns:; EAX = pseudorandom integer
;-------------------------------------------------
sub eax,ebx ; calc difference between ranges
call RandomRange
add eax,ebx ; scale to desired range
ret
BetterRandomRange ENDP
END main
5.9.06
include irvine32.inc
.686p
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD
.data
.code
main PROC
mov eax,100
call Res
invoke ExitProcess,0
main ENDP
Res PROC
pushad
mov ecx,eax
L:
call RandomString
loop L
call Crlf
call WaitMsg
popad
ret
Res ENDP
RandomString PROC
pushad
mov eax,26
call RandomRange
add eax,64
call WriteChar
popad
ret
RandomString ENDP
END main
5.9.07
include irvine32.inc
.686p
.model flat,stdcall
.stack 4096ExitProcess PROTO,dwExitCode:DWORD
.data
;保存当前控制台窗口的行数和列数
rows BYTE 0
cols BYTE 0;保存需要显示的元素的随机位置
X DWORD 0
Y DWORD 0.code
main PROC
;获得当前控制台的行数和列数
call GetMaxXY
mov rows,al
mov cols,dl
;获得需要显示元素的随机位置
movzx eax,rows
call RandomRange
mov X,eax
movzx eax,cols
call RandomRange
mov Y,eax
;将光标定位到X Y位置
mov dl,byte ptr X
mov dh,byte ptr Y
call Gotoxy
;在指定位置写一个字符
mov al,70
call WriteChar
call WaitMsg
invoke ExitProcess,0
main ENDP
END main
5.9.08
include irvine32.inc
.686p
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD
.data
frontColor WORD 0
backColor WORD 0
color DWORD 0
str1 BYTE "hello",0.code
main PROC
mov ecx,16
OuterLoop:
pushad
mov ecx,16
InnerLoop:
call Clrscr
call SET
call WaitMsg
mov ax,frontColor
inc ax
mov frontColor,ax
mov ax,backColor
inc ax
mov backColor,ax
loop InnerLoop
popad
loop OuterLoop
invoke ExitProcess,0
main ENDP
;本过程设置前景色与背景色 并显示一段预设文本
;参数 frontColor backColor常量
;返回 无
SET PROC
pushad
;设置颜色
;frontColor+backColor*16
movzx eax,backColor
shl eax,4
movzx edx,frontColor
add eax,edx
;设置前景色以及背景色
call SetTextColor
;编写一段文本
pushad
mov edx,offset str1
call WriteString
popad
popad
ret
SET ENDP
END main
5.9.9
; Chapter 5 Exercise 9: Recursion
.386.model flat,stdcall
.stack 4096
ExitProcess proto,dwExitCode:dword
.data
.code
main proc
mov ecx,4
mov eax,0
call CallMe
invoke ExitProcess,0
main endp
CallMe proc
inc eax
loop L1
ret
L1: call CallMe
ret
CallMe endp
end main
### 5.9.10
```cpp
.686p
.model flat,stdcall
.stack 4096
ExitProcess PROTO,dwExitCode:DWORD
.data
FibArr DWORD 50DUP(0)
temp DWORD 0.code
main PROC
mov edx,offset FibArr
mov eax,1
mov ebx,1
mov ecx,47;先写入前两个数据
mov [edx],eax
mov [edx+4],eax
add edx,8
l:
mov eax,0
add eax,[edx-4]
add eax,[edx-8]
mov [edx],eax ;存入数据
add edx,4;指向下一个位置
loop l
invoke ExitProcess,0
main ENDP
END main
5.09.11
; Exercise 11: Setting Multiples of K
; Write a procedure that sets N multiples of K
; in an array to True..386.model flat,stdcall
.stack 4096
ExitProcess proto,dwExitCode:dword
.data
sieve dword 150DUP(0).code
main proc
mov eax,2
mov ecx,50
call SetMultiples
mov eax,3
call SetMultiples
invoke ExitProcess,0
main endp
SetMultiples proc
; Set N multiples of K in sieve to True(1).; EAX = K
; ECX = N
push ecx
push esi
mov esi,eax
L1:
mov sieve[esi*4],1
add esi,eax
loop L1
pop esi
pop ecx
ret
SetMultiples endp
end main