1.查找一个数的下标 2.排序 3.二分 4.两个九九乘法表版本 5.递归求公约数 6.字符10->16字符 7.字符16->10字符
1、数据查找:内容:TAB开始的字节单元中存放N个无符号数,dat单元存放一个已知数据X,现从TAB中查找是否存在数据X,如果存在,NO单元存放该数据在TAB中的序号,否则NO单元存放0FFH 。
遍历一次
INCLUDE Irvine32.inc
.data
tab db 5,6,41,50,8,54,20,53,36
len dd ($ - tab) / 1
dat db 54
no db ?
.code
main proc
mov dl,0
mov ecx,len
mov bl,dat
mov esi,0
again:
cmp bl,byte ptr [tab+esi]
jnz a
mov no,dl
jmp b
a:
inc esi
inc dl
loop again
mov no,0ffh
b:
movsx eax,no
call writeint
exit
main endp
end main
2.无符号数排序 内容:TAB开始的单元中存放N个字节无符号数,请按照从大到小排序后,存入DAT单元中。(注意:TAB数据保持不变)
采用选择排序,用上次的代码修改了一点
INCLUDE Irvine32.inc
.data
tab db 50,42,51,2,4,85,36
len dd $-tab
dat db 7 dup(?)
.code
main proc
mov ecx,len
call fuzhi
mov ecx,len
mov edi,len
call select
mov ecx,len
call output
exit
main endp
fuzhi proc
mov ebx,0
again1:
mov dl,byte ptr [tab + ebx]
mov [dat + ebx],dl
inc ebx
loop again1
ret
fuzhi endp
select proc
mov edx,0
mov ebx,0
first:
call max
mov ah,[dat + ebx]
mov [dat + ebx],al
mov [dat + esi],ah
inc ebx
mov edx,ebx
dec edi
cmp edi,0
ja first
ret
select endp
max proc
mov al,byte ptr [dat + edx]
again:
cmp al,byte ptr [dat + edx]
ja next
mov al,byte ptr [dat + edx]
mov esi,edx ;最大值的下标
next:
inc edx
cmp ecx,edx ;遍历 edx-ecx
ja again
ret
max endp
output proc
mov ebx,0
again2:
movsx eax,byte ptr [dat + ebx]
call writeint
inc ebx
loop again2
ret
output endp
end main
3.二分
include irvine32.inc
.data
arr dw 11,22,33,44,55,66
dw 77,88,99,111,222,333
len dd ($ - arr) / 2
number dw 55
.code
main proc
mov esi,0 ;esi low
mov edi,len
dec edi ;edi high 0 - ( len-1)
again:
cmp esi,edi
ja final
mov ebx,esi
add ebx,edi
shr ebx,1 ;mid = (low + high) / 2
mov ax,arr[ebx * 2]
cmp ax,number
jz dengyu
jg dayu
jl xiaoyu
dayu:
mov edi,ebx ;high = mid - 1
dec edi
jmp next
xiaoyu:
mov esi,ebx ;low = mid + 1
inc esi
next:
jmp again
final:
mov ebx,-1
dengyu:
mov eax,ebx
call writeint
call crlf
movsx eax,word ptr arr[ebx * 2]
call writeint
exit
main endp
end main
4.九九乘法表
第一个参考老师的核心
include irvine32.inc
.data
.code
main proc
mov ecx,9
mov bh,0
mov bl,0
outt:
push ecx ;作为循环次数
inc bh ;bh为大,每次+1,加到9
mov bl,0
inn:
cmp bl,bh
jae a
inc bl ;前面设置为0,先+1,加到bh
mov al,bl ;做乘法,输出
mul bh
movsx edx,al
mov al,bl
add al,30h
call writechar
mov al,'*'
call writechar
mov al,bh
add al,30h
call writechar
mov al,'='
call writechar
mov eax,edx
call writeint
mov al,' '
call writechar
jmp inn
a:
cmp bh,9 ;最后一行没换行
jz b
call crlf
b:
pop ecx
dec ecx
jnz outt
exit
main endp
end main
第二个用自己的方式 前面都用cmp
include irvine32.inc
.data
.code
main proc
mov ch,9
mov bh,0
outt:
cmp bh,ch
jae b
inc bh
mov bl,1
inn:
cmp bl,bh
ja a
mov al,bl
mul bh
movsx edx,al
mov al,bl
add al,30h
call writechar
mov al,'*'
call writechar
mov al,bh
add al,30h
call writechar
mov al,'='
call writechar
mov eax,edx
call writeint
mov al,' '
call writechar
inc bl
jmp inn
a:
cmp bh,9
jz cc
call crlf
cc:
jmp outt
b:
exit
main endp
end main
#include<stdio.h>
int main()
{
int n=9;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
printf("%d*%d=%d ",i,j,i*j);
}
printf("\n");
}
return 0;
}
5.用递归求最大公约数,下面是参考代码
include irvine32.inc
.data
.code
main proc
call readint
push eax
call readint
push eax
call gcdd
mov eax,ebx
call writeint
exit
main endp
gcdd proc
push ebp
mov ebp,esp
mov eax,[ebp + 8]
mov ebx,[ebp + 12]
mov edx,0
div ebx
cmp edx,0
jz ending
push edx
push ebx
call gcdd
ending:
pop ebp
ret 8
gcdd endp
end main
;int gcd(int x, int y) //最大公约数
;{
;return y?gcd(y,x%y):x;
;}
6.10->16 都是字符串
INCLUDE Irvine32.inc
.data
ten dd 10
liu dd 16
arr db 10 dup(?)
.code
main proc
xor eax,eax
mov ecx,4
mov ebx,0
again:
mov eax,ebx
mul ten
mov ebx,eax
xor eax,eax
call readchar
sub al,48
add ebx,eax
loop again
mov eax,ebx
mov edi,0
again1:
mov edx,0
div liu
mov ebx,eax
mov eax,edx
add eax,48
cmp eax,57
jbe a
add eax,39
a:
mov [arr + edi],al
inc edi
mov eax,ebx
cmp eax,0
jnz again1
dec edi
output:
mov al,[arr + edi]
call writechar
dec edi
cmp edi,-1
jnz output
exit
main endp
end main
;4位10进制的字符串变成16进制字符串:先变成十进制的数,然后变成16进制
7.16->10
INCLUDE Irvine32.inc
.data
ten dd 10
liu dd 16
arr db 10 dup(?)
.code
main proc
mov ecx,4
mov ebx,0
again2:
mov eax,ebx
mul liu
mov ebx,eax
xor eax,eax
call readchar
cmp al,97
jb a
sub al,39
a:
sub al,48
add ebx,eax
loop again2
mov eax,ebx ;得到十进制数
mov ecx,0
again:
div ten
push edx
inc ecx
mov edx,0
cmp eax,0
ja again
again1:
pop eax
add al,48
call writechar
loop again1
exit
main endp
end main
;4位16进制的字符串变成10进制字符串:先变成十进制的数,然后变成10进制字符