做下面的题目主要学会怎么使用子程序,做现场保护,就可以用在别的程序上
1.快排 2.利用快排找中位数
3.用子程序设计的方法,分别把BUF字单元中的四位十六进制数转换为ASCII码存入MAS开始的单元中,并在终端上显示MAS开始的4个字节单元
4.从键盘输入一串字母并保存在string开始的地址单元,要求将该字符串中的大写字母转化为小写字母后用子程序实现在终端上依次显示该串字母的 ASCII码。
5.将从键盘输入的N个无符号数保存到数组DAT ,找出N个无符号数中的偶数存放到数组P,统计并在终端上显示数组P的数据个数no。
6.将从键盘输入的N个有符号数保存到数组TAB ,找出N个有符号数中绝对值大于X的最小负奇数存放到Min单元,如果没有找到则Min单元存放0。在终端上显示Min的绝对值。
1.快排,是先参考了c语言写的
INCLUDE Irvine32.inc
.data
arr dd 12,23,45,5,25,94,0,13,2,4
len dd ($ - arr) / 4
.code
main proc
lea edx,arr
mov esi,0
mov edi,len
dec edi
;call partition ;;int partition(int [], int low,int high)
call quicksort ;;void quicksort(int [],int low,int high)
call output
exit
main endp
quicksort proc
;;;;edx=a[],esi=low,edi=high
cmp esi,edi
jge final ;;if(low < high)
call partition ;;ploc = partition(a,low,high);
push eax ;ploc
push edi ;high
mov edi,eax
dec edi
call partition ;;quicksort(a,low,ploc-1);
pop edi
pop eax
mov esi,eax
inc esi
call quicksort ;;quicksort(a,ploc+1,high);
final:
ret
quicksort endp
partition proc
;;;;edx=a[],esi=low,edi=high,eax=a[low]
push esi ;;保存现场
push edi
push ebx
mov eax,[edx + 4 * esi] ;;key = a[low];
out_:
cmp esi,edi ;;low < high
jge final
in_1:
cmp esi,edi ;;low < high && key <= a[high]
jge final
cmp [edx + 4 * edi],eax
jl final_1
dec edi
jmp in_1
final_1:
mov ebx,[edx + 4 * esi] ;;swap(a[i],a[j]);
xchg ebx,[edx + 4 * edi]
mov [edx + 4 * esi],ebx
in_2:
cmp esi,edi ;;low < high && key >= a[low]
jge final
cmp [edx + 4 * esi],eax
jg final_2
inc esi
jmp in_2
final_2:
mov ebx,[edx + 4 * esi] ;;swap(a[i],a[j]);
xchg ebx,[edx + 4 * edi]
mov [edx + 4 * esi],ebx
jmp out_
final:
mov eax,esi ;;return low;
pop ebx ;;恢复现场
pop edi
pop esi
ret
partition endp
output proc
mov ecx,len
lea edx,arr
again:
mov eax,[edx]
call writeint
add edx,4
loop again
ret
output endp
end main
int partition(int [], int low,int high)
{
key = a[low];
while(low < high)
{
while(low < high && key <= a[high])
{
high--;
}
swap(a[i],a[j]);
while(low < high && key >= a[low])
{
low++;
}
swap(a[i],a[j]);
}
return low;
}
void quicksort(int [],int low,int high)
{
if(low < high)
{
ploc = partition(a,low,high);
quicksort(a,low,ploc-1);
quicksort(a,ploc+1,high);
}
}
2.中位数
INCLUDE Irvine32.inc
.data
.code
main proc
;lea edx,arr
;mov esi,0
;mov edi,len
;dec edi
;call partition ;;int partition(int [], int low,int high)
;call quicksort ;;void quicksort(int [],int low,int high)
call readint
push eax
call midnumber
exit
main endp
midnumber proc
push ebp
mov ebp,esp
push edx
push esi
push edi
push ecx
push ebx
mov ebx,eax
and ebx,1
mov eax,[ebp + 8] ;n
shl eax,2 ;4n
sub esp,eax
mov esi,0 ;i
mov ecx,[ebp + 8] ;n
next: ;;输入
call readint
mov [esp + 4 * esi],eax ;栈顶低
inc esi
loop next
mov edx,esp ;arr edx
mov esi,0 ;low
mov edi,[ebp + 8]
dec edi ;high
call quicksort
mov ecx,[ebp + 8]
call output
call crlf
mov eax,[ebp + 8]
shr eax,1 ;/2
cmp ebx,0
ja qi
mov ebx,[esp + 4 * eax]
dec eax
add ebx,[esp + 4 * eax]
mov eax,ebx
shr eax,1
jmp fi
qi:
mov eax,[esp + 4 * eax]
fi:
call writeint
mov eax,[ebp + 8]
shl eax,2
add esp,eax
pop ebx ;恢复现场
pop ecx
pop edi
pop esi
pop edx
pop ebp
ret 4
midnumber endp
quicksort proc
;;;;edx=a[],esi=low,edi=high
cmp esi,edi
jge final ;;if(low < high)
call partition ;;ploc = partition(a,low,high);
push eax ;ploc
push edi ;high
mov edi,eax
dec edi
call partition ;;quicksort(a,low,ploc-1);
pop edi
pop eax
mov esi,eax
inc esi
call quicksort ;;quicksort(a,ploc+1,high);
final:
ret
quicksort endp
partition proc
;;;;edx=a[],esi=low,edi=high,eax=a[low]
push esi ;;保存现场
push edi
push ebx
mov eax,[edx + 4 * esi] ;;key = a[low];
out_:
cmp esi,edi ;;low < high
jge final
in_1:
cmp esi,edi ;;low < high && key <= a[high]
jge final
cmp [edx + 4 * edi],eax
jl final_1
dec edi
jmp in_1
final_1:
mov ebx,[edx + 4 * esi] ;;swap(a[i],a[j]);
xchg ebx,[edx + 4 * edi]
mov [edx + 4 * esi],ebx
in_2:
cmp esi,edi ;;low < high && key >= a[low]
jge final
cmp [edx + 4 * esi],eax
jg final_2
inc esi
jmp in_2
final_2:
mov ebx,[edx + 4 * esi] ;;swap(a[i],a[j]);
xchg ebx,[edx + 4 * edi]
mov [edx + 4 * esi],ebx
jmp out_
final:
mov eax,esi ;;return low;
pop ebx ;;恢复现场
pop edi
pop esi
ret
partition endp
output proc
mov esi,0
next:
mov eax,[edx + 4 * esi]
call writeint
mov al,','
call writechar
inc esi
loop next
ret
output endp
end main
3.
INCLUDE Irvine32.inc
.data
buf dw 16ABH
mas db 4 dup(?)
.code
main proc
call trans
exit
main endp
trans proc
pushad
mov edi,0
mov ecx,4
again:
mov dx,buf
rol dx,4 ;右移4位
mov buf,dx
and dl,0FH
cmp dl,10
jb shuzi
add dl,7
shuzi:
add dl,48
mov mas[edi],dl
mov al,mas[edi]
movsx eax,al
call writeint
call crlf
inc esi
loop again
popad
ret
trans endp
end main
4.
INCLUDE Irvine32.inc
.data
string db 50 dup(?)
.code
main proc
call scan
call exchange
call print
exit
main endp
scan proc
;下面子程序都用了现场保护,这个没有用,因为输入感觉没必要保护,直接修改了
call readint ;read strlen
mov ecx,eax
lea edx,string
call readstring
ret
scan endp
exchange proc
pushad
mov esi,0
again:
mov al,[edx + esi]
cmp al,90
ja xiao
add al,32
xiao:
mov [edx + esi],al
inc esi
loop again
popad
ret
exchange endp
print proc
pushad
mov esi,0
again1:
mov al,[edx + esi]
movsx eax,al
call writeint
inc esi
loop again1
popad
ret
print endp
end main
5.
INCLUDE Irvine32.inc
.data
dat dw 10 dup(?)
p dw 10 dup(?)
no dw ?
.code
main proc
call scan
call change
call print
exit
main endp
scan proc
mov no,0
call readint
mov ecx,eax
push ecx
mov esi,0
sc:
call readint
mov [dat + esi],ax
add esi,2
loop sc
pop ecx
ret
scan endp
change proc
pushad
mov esi,0
mov edi,0
chan:
mov ax,[dat + esi]
and ax,1
jnz ji
inc no
mov [p + edi],ax
add edi,2
ji:
add esi,2
loop chan
popad
ret
change endp
print proc
mov ax,no
call writeint
ret
print endp
end main
6.
INCLUDE Irvine32.inc
.data
tab dd 10 dup(?)
x dd 6
min dd ?
.code
main proc
call scan
call finding
call print
exit
main endp
scan proc
mov min,0
mov esi,0
call readint
mov ecx,eax
push ecx
sc:
call readint
mov [tab + esi],eax
add esi,4
loop sc
pop ecx
ret
scan endp
finding proc
pushad
mov esi,0
find:
mov eax,[tab + esi]
cmp eax,0
jge stop
neg eax
cmp eax,x
jle stop
test eax,1
jz stop
neg eax
cmp eax,min
jge stop
mov min,eax
stop:
add esi,4
loop find
popad
ret
finding endp
print proc
mov eax,min
neg eax
call writeint
ret
print endp
end main