第二次:1.求正负数的个数 2.求数组最大值 3.冒泡排序 4.选择排序 5.快排核心 6.复制字符串 7.翻转字符串
1.统计正数(负数)的个数 内容:DAT开始的单元中存放N个字节有符号数,统计正数的个数存入T0单元中;负数的个数存入T1单元中
INCLUDE Irvine32.inc
.data
dat db 12,23,-45,54,25,94,0,12,-2,4
t0 db ?
t1 db ?
len dd $-dat
.code
main proc
lea edx,dat
mov ecx,len
call sum
movsx eax,byte ptr t0
call writeint
movsx eax,byte ptr t1
call writeint
exit
main endp
sum proc
again:
test byte ptr [edx],80h
je plus ;=0
inc t0
jmp next
plus:
inc t1
next:
inc edx
loop again
ret
sum endp
end main
2.求最大数 内容:DAT开始的字单元中存放N个无符号数,求出最大数存入MAX单元中
INCLUDE Irvine32.inc
.data
dat dw 12,23,45,54,25,94,0,12,2,4
max1 dw ?
len dd ($-dat)/2
.code
main proc
lea edx,dat
mov ecx,len
mov max1,0
call max
movsx eax,word ptr max1
call writeint
exit
main endp
max proc
again:
mov ax,word ptr [edx]
cmp max1,ax
ja next
mov max1,ax
next:
add edx,2
loop again
ret
max endp
end main
部分参考老师
冒泡排序
INCLUDE Irvine32.inc
.data
arr dd 60,30,40,50,1,70,80,100,90,5
len dd ($-arr)/4
.code
main proc
lea edx,arr
mov ecx,len
call bubble_sort
lea edx,arr
mov ecx,len
call output
exit
main endp
bubble_sort proc
mov ebx,ecx ;n->ebx
mov esi,0 ;i->esi
out_loop:
push ebx
sub ebx,esi
dec ebx ;ebx->n-i-1
mov edi,0 ;edi->j
in_loop:
cmp edi,ebx
jnb final ;>=结束循环
mov eax,[edx + 4 * edi]
cmp eax,[edx + 4 * edi + 4]
jna next
xchg eax,[edx + 4 * edi + 4] ;exchange
mov [edx + 4 * edi],eax
next:
inc edi ;j++
jmp in_loop
final:
pop ebx
inc esi
loop out_loop
ret
bubble_sort endp
output proc
mov ebx,0
again:
mov eax,[edx + 4 * ebx]
call writeint
inc ebx
mov al,' '
call writechar
loop again
ret
output endp
end main
参考核心
int i, j
for(i = 0;i < n;i++)
{
for(j = 0;j < n-1-i;j++)
{
if(arr[j] > arr[j+1])
{
swap(arr[j], arr[j+1])
}
}
}
选择排序INCLUDE Irvine32.inc
.data
arr dd 1,4,34,56,845,516
len dd ($ - arr) / 4
len1 dd ?
.code
main proc
mov edx,offset arr
mov ecx,len ;ecx用于loop找最大值
mov edi,len ;edi保存最后一个数的位子
call select_sort ;排序
mov ecx,len ;输出
call output
exit
main endp
;总体思路,先找到max放到最后一个数,然后依次loop
select_sort proc
L1:
call max ;找最大
dec ebx ;最后一步加了1,减回来
mov ecx,[edx + 4 * ebx] ;交换
mov [edx + 4 * ebx],eax
mov [edx + 4 * esi],ecx
dec edi ;最后的位子减1
mov ecx,edi ;ecx用于loop找最大值
cmp edi,0 ;检查是否结束
ja L1
ret
select_sort endp
;find max. ebx begin 0 for index
;eax for max
;esi for max index
;edx for arr'firsr address
max proc
mov ebx,0
mov eax,[edx]
again:
cmp eax,[edx + 4 * ebx]
ja next
mov eax,[edx + 4 * ebx]
mov esi,ebx ;esi得知最大数的index
next:
inc ebx
loop again
ret
max endp
output proc
mov ebx,0
again1:
mov eax,[edx + 4 * ebx]
call writeint
inc ebx
mov al,' '
call writechar
loop again1
ret
output endp
END main
快排
INCLUDE Irvine32.inc
.data
arr dd 12,23,45,5,25,94,0,13,2,4
len dd ($ - arr) / 4
low1 dd 0
high1 dd 9
.code
main proc
lea edx,arr
mov esi,low1
mov edi,high1
mov eax,[arr + 4 * esi]
call par
exit
main endp
par proc
;edx=a[],esi=low,edi=high,eax=a[low]
out_:
cmp esi,edi
jge final
in_1:
cmp esi,edi
jge final
cmp [edx + 4 * edi],eax
jl final_1
dec edi
jmp in_1
final_1:
mov ebx,[edx + 4 * esi]
xchg ebx,[edx + 4 * edi]
mov [edx + 4 * esi],ebx
in_2:
cmp esi,edi
jge final
cmp [edx + 4 * esi],eax
jg final_2
inc esi
jmp in_2
final_2:
mov ebx,[edx + 4 * esi]
xchg ebx,[edx + 4 * edi]
mov [edx + 4 * esi],ebx
jmp out_
final:
call output
ret
par 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
参考核心
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]);
}
复制字符串
include Irvine32.inc
.data
arr1 db 'personal_computer',0
arr2 db 17 dup(?)
db 0
.code
main proc
lea esi,arr1
lea edi,arr2
mov ecx,sizeof arr1 ;esi->edi for ecx
cld ;标志位
rep movsb
lea edx,arr2
call writestring
exit
main endp
end main
翻转字符串
include Irvine32.inc
.data
arr1 db 'personal_computer',0
arr2 db 17 dup(?)
db 0
.code
main proc
lea esi,arr1
lea edi,arr2
mov ecx,sizeof arr1 ;esi->edi for ecx
cld ;标志位
rep movsb
mov ecx,sizeof arr1
dec ecx
L:
mov al,byte ptr [arr2 + ecx - 1]
call writechar
loop L
exit
main endp
end main