編程寫一個完整的程序,將數組array中的元素按逆序存放,要求程序中附加的變量最少。數據段的定義如下:
.data
array dword 12,4, 168,122,-33,56,78,99,345, 66,-5
include vcIO.inc
.data
array dword 12,4, 168,122,-33,56,78,99,345, 66,-5
frmStr1 byte ' %5d',0
.code
main proc
mov esi,offset array
mov edi,esi
add edi,(lengthof array-1)*4
mov ecx,(lengthof array)/2
again:
mov eax,[esi]
xchg [edi],eax
mov [esi],eax
add esi,4
sub edi,4
loop again
mov esi,offset array
mov ecx,lengthof array
again2:
mov eax,[esi]
add esi,4
pushad
invoke printf,offset frmStr1, eax
popad
loop again2
pushad
invoke _getch
popad
ret
main endp
end main
若array是word類型,應該修改哪條指令?
此處輸出直接輸出無符號數,-33和-5無法直接輸出,等後續修改完善。
include vcIO.inc
.data
array word 12,4, 168,122,-33,56,78,99,345, 66,-5
frmStr1 byte ' %5d',0
.code
main proc
mov esi,offset array
mov edi,esi
add edi,(lengthof array-1)*2
mov ecx,(lengthof array)/2
again:
mov ax,[esi]
xchg [edi],ax
mov [esi],ax
add esi,2
sub edi,2
loop again
mov esi,offset array
mov ecx,lengthof array
again2:
mov eax,[esi]
add esi,2
pushad
invoke printf,offset frmStr1, ax
popad
loop again2
pushad
invoke _getch
popad
ret
main endp
end main
編程寫一個完整的程序,將數組array中的元素按逆序存放,要求程序中附加的變量最少。
算法描述
left,right分別指向數組的第一個和最後一個元素,採用首尾交換。
1. left=0,right=n-1 ;
2. while (left<right do)
{ swap a[left],a[right] ;left++;right--} ;
合理分配寄存器:left=esi,right=edi
採用帶比例因子的相對尋址處理數組
include vcIO.inc
.data
array dword 12,4, 168,122,-33,56,78,99,345, 66,-5
frmStr1 byte ' %5d',0
.code
main proc
xor esi,esi
mov edi,(lengthof array - 1)
mov ecx,(lengthof array)/2
again:
mov eax,array[esi*4]
xchg array[edi*4],eax
mov array[esi*4],eax
inc esi
dec edi
loop again
mov esi,offset array
mov ecx,lengthof array
again2:
mov eax,[esi]
add esi,4
pushad
invoke printf,offset frmStr1, eax
popad
loop again2
pushad
invoke _getch
popad
ret
main endp
end main
“迴文串”是一個正讀和反讀都一樣的字符串,比如“eye”、“level”、“noon”等。請寫一個程序測試一字符串是否是“迴文”
include vcIO.inc
.data
palstr byte 'huiuh',0
frmStr1 byte '%c',0
frmStr2 byte 'not pal',0
frmStr3 byte 'is pal',0
.code
main proc
mov esi,offset palstr
mov edi,esi
sub edi,2
add edi,lengthof palstr
mov ecx,(lengthof palstr)/2
again:
xor eax,eax
xor ebx,ebx
mov al,[esi]
mov bl,[edi]
cmp al,bl
jnz notpal
loop again
pushad
invoke printf,offset frmStr3
popad
jmp fin
notpal:
pushad
invoke printf,offset frmStr2
popad
fin:
pushad
invoke _getch
popad
ret
main endp
end main
Fibonacci numbers的定義:
f1=1,f2=1, fn= fn-1 + fn-2 n>=3
編程輸出Fibonacci numbers的前20項。
include vcIO.inc
.data
frmStr byte '第%d個Fibonacci數:%d',0ah,0ah,0
.code
main proc
mov eax,1
mov ebx,1
pushad
invoke printf,offset frmStr,eax,ebx
popad
inc eax
pushad
invoke printf,offset frmStr,eax,ebx
popad
inc eax
mov ecx,1
again:
xchg ebx,ecx
add ebx,ecx
pushad
invoke printf,offset frmStr,eax,ebx
popad
inc eax
cmp eax,21
jnz again
pushad
invoke _getch
popad
ret
main endp
end main
在不產生溢出的情況下n的最大值是多少?
46項
能被4整除但不能被100整除,或者年被400整除的年份是閏年。編程寫一個完整的程序,求出2012年~2099年中的所有閏年年份,並把它們存放在數組Lyear中。
include vcIO.inc
.data
leapyear dw 50 dup(0)
frmStr1 byte ' address=%d',0ah,0
frmStr byte ' %d',0ah,0
.code
main proc
mov esi,offset leapyear
mov edi,esi
mov eax,2012
again:
cmp eax,2100
jz fin
mov ebx,eax
mov ecx,4h
xor edx,edx
div ecx
mov eax,ebx
inc eax
cmp edx,0
jz addyear
jmp noyear
addyear:
mov [esi],ebx
add esi,4
noyear:
jmp again
fin:
again2:
cmp esi,edi
jz fina
pushad
mov eax,[edi]
invoke printf,offset frmStr,eax
popad
add edi,4
jmp again2
fina:
pushad
invoke _getch
popad
ret
main endp
end main
編程寫一個完整的程序統計字符串msg中的空格的個數與小寫字母的個數,並分別將它們存入space單元與char單元中並輸出。
include vcIO.inc
.data
hello byte 'Hello MASM32',0
frmStr byte 'spacenum=%d',0ah,0
frmStr1 byte 'lowcase=%d',0ah,0
.code
main proc
xor eax,eax
xor ebx,ebx
mov esi,offset hello
mov edi,esi
mov ecx,lengthof hello
add edi,ecx
again:
cmp esi,edi
jz fin
mov cl,[esi]
inc esi
cmp cl,' '
jz spaceadd
cmp cl,'a'
jl finoneloop
cmp cl,'z'
jl lowcaseadd
jmp finoneloop
spaceadd:
inc eax
jmp finoneloop
lowcaseadd:
inc ebx
jmp finoneloop
finoneloop:
jmp again
fin:
pushad
invoke printf,offset frmStr,eax
invoke printf,offset frmStr1,ebx
popad
pushad
invoke _getch
popad
ret
main endp
end main
編程寫一個完整的程序,求數組aray中的最大值與最小值,並將它們分別存入max和min單元中。
include vcIO.inc
.data
array dword 12,4, 168,122,-33,56,78,99,345, 66,-5
frmStr byte 'max=%d',0ah,0
frmStr1 byte 'min=%d',0ah,0
min dw ?
max dw ?
.code
main proc
mov esi,offset array
mov edi,esi
add edi,(lengthof array -1)*4
mov eax,[esi]
add esi,4
mov ebx,[esi]
add esi,4
cmp eax,ebx
jl nochange
xchg eax,ebx
nochange:
again:
cmp esi,edi
jz fin
mov ecx,[esi]
cmp eax,ecx
jl changemin
cmp ecx,ebx
jl changemax
return1:
jmp oneloopover
changemin:
mov eax,ecx
jmp return1
changemax:
mov ebx,ecx
jmp return1
oneloopover:
add esi,4
jmp again
fin:
pushad
invoke printf,offset frmStr,eax
invoke printf,offset frmStr1,ebx
popad
pushad
invoke _getch
popad
ret
main endp
end main
array是一無符號數數組,數據段的定義如下。編程寫一個完整的程序求出數組元素中偶數的和,並將它存入esum單元中 。
include vcIO.inc
.data
array dword 12,4, 168,122,33,56,78,99,345, 66,5
frmStr byte 'sum=%d',0ah,0
.code
main proc
mov esi,offset array
mov edi,esi
add edi,(lengthof array -1)*4
xor ebx,ebx
xor ecx,ecx
mov ecx,2h
again:
cmp esi,edi
jz fin
xor edx,edx
xor eax,eax
mov eax,[esi]
div ecx
cmp edx,0
jz addeven
jmp oneloopover
addeven:
add ebx,[esi]
oneloopover:
add esi,4
jmp again
fin:
pushad
invoke printf,offset frmStr,ebx
;invoke printf,offset frmStr1,ebx
popad
pushad
invoke _getch
popad
ret
main endp
end main
有一個首地址爲string的字符串 ,剔除string中所有的空格字符。請從字符串最後一個字符開始逐個向前判斷、並進行處理。
include vcIO.inc
.data
string byte 'H e l lo MAS M32',0
frmStr byte ' before:%s',0ah,0
frmStr1 byte ' after:%s',0ah,0
.code
main proc
pushad
invoke printf,offset frmStr,offset string
popad
mov esi,offset string
mov edi,esi
add edi,(lengthof string-1)
mov edx,edi
again:
cmp esi,edi
jz fin
mov al,[edi]
cmp al,' '
jnz oneloopover
mov ecx,edi
again2:
cmp ecx,edx
jz again2over
mov bl,[ecx]
mov bh,[ecx+1]
xchg bl,bh
mov [ecx],bl
mov [ecx+1],bh
inc ecx
jmp again2
again2over:
mov eax,0h
mov [edx],eax
dec edx
jmp oneloopover
oneloopover:
dec edi
jmp again
fin:
pushad
invoke printf,offset frmStr1,offset string
popad
pushad
invoke _getch
popad
ret
main endp
end main
palindrome(迴文)是指正讀和反讀都一樣的數或文本。例如:11、121、12321等,編寫程序,求10到10000之間所有迴文數並輸出。要求每行輸出10個數。
include vcIO.inc
.data
i dword ?
shang dword ?
yu dword ?
frmStr1 byte ' %d',0
frmStr2 byte ' ',0ah,0
num dword 0
.code
main proc
mov eax,10
mov i,eax
again:
mov eax,i
cmp eax,10000
jz fin
xor ebx,ebx
xor ecx,ecx
again2:
cmp eax,0
jz oneloopover
mov cx,10
xor edx,edx
div cx
mov shang,eax
mov yu,edx
xchg ebx,eax
mov edx,10
mul edx
xchg ebx,eax
add ebx,yu
mov eax,shang
jmp again2
oneloopover:
mov eax,i
cmp eax,ebx
jz printnum
inc eax
mov i,eax
jmp again
printnum:
pushad
mov ecx,num
inc ecx
mov num,ecx
xchg ecx,eax
xor edx,edx
xor ebx,ebx
mov ebx,10
div ebx
cmp edx,0
jz printline
jmp printval
printline:
invoke printf,offset frmStr2
mov eax,0
mov num,eax
printval:
mov ebx,i
invoke printf,offset frmStr1,ebx
popad
inc eax
mov i,eax
jmp again
fin:
pushad
invoke _getch
popad
ret
main endp
end main
編程寫一個完整的程序,求出2~100之間的所有素數,並將它們存入Prime數組中,素數的個數存入變量Pcounter中。
include vcIO.inc
.data
frmStr1 byte 'Pcounter=%d',0
frmStr2 byte ' %d',0ah,0
prime byte 50 dup(0)
num dword 0
i byte 2
j byte 2
.code
main proc
mov esi,offset prime
mov edi,esi
mov bl,i
again:
cmp bl,101
jz fin
mov bh,j
again2:
cmp bh,bl
jz overagain2
xor eax,eax
xor edx,edx
mov al,bl
mov cl,bh
div cl
cmp ah,0
jz overloop
inc bh
jmp again2
overagain2:
jmp addnum
overloop:
inc bl
jmp again
addnum:
mov eax,num
inc eax
mov num,eax
mov [esi],bl
inc bl
inc esi
jmp again
fin:
pushad
mov eax,num
invoke printf,offset frmStr1,eax
popad
pushad
again3:
cmp esi,edi
jz fin2
mov eax,[edi]
invoke printf,offset frmStr2,al
inc edi
;inc bl
jmp again3
popad
fin2:
pushad
invoke _getch
popad
ret
main endp
end main
回答問題:
1. 在循環結構中,如何避免無限循環情況的發生?
適當位置加入跳轉語句
2. 下面的程序段中,循環體將被執行多少次?如果的初值爲-100,循環體將被執行多少次?
mov ecx,0
loopTest: mov eax,ecx
call dispuid
loop loopTest
0 總-100??? //此答案有待考證