题目:1.1234h输出为1234 2.;输入n个十六进制数的字符,存入bx后,转换成十进制的数字 如223->+547
3.输入n个十进制数的字符,存入bx后,以十进制数字输出 如120->+120 4.不输出+号的数字 5.求最大公约数 6.递归求n!
1.先处理1,左移得到2341然后最低位就是1,转移到al处理
include irvine32.inc
.data
.code
s:
mov bx,1234h
mov ch,4 ;循环4次
r:
mov cl,4 ;左移的位数
rol bx,cl ;左移bx,第一次变成2341
mov al,bl ;将bx低8位bl存入al处理输出
and al,0fh ;将al的高4位变成0
add al,30h ;用asc2码 0-9
cmp al,3ah
jl print ;<58
add al,7h ;a-f
print:
call writechar
dec ch
jnz r ;不为0转移
exit
end s
2.先判断0-9,a-f,因为是16进制,左移然后加入就可以直接算出十进制数
include irvine32.inc
.data
.code
main proc
xor eax,eax
xor ebx,ebx
xor ecx,ecx
xor edx,edx
mov ecx,3
mov bx,0 ;存最后的数
new:
call readchar
sub al,48 ;先判断0-9
cmp al,0 ;<0
jl final
cmp al,10 ;>=0,<10
jl ad
sub al,39 ;a-f
cmp al,10
ja final ;<a
cmp al,15
jge final ;>f
ad:
shl bx,4 ;左移
add bx,ax
loop new
final:
movsx eax,bx
call writeint
exit
main endp
end main
3.先判断0-9,因为是10进制,所以要*10做法
include irvine32.inc
.data
.code
main proc
xor eax,eax
xor ebx,ebx
xor ecx,ecx
xor edx,edx
mov ecx,3
mov bx,0 ;存最后的数
new:
call readchar
sub al,30h ;字符变成数字
cmp al,0
jl final
cmp al,9
jg final
xchg ax,bx
mov dx,10 ;放入ax乘10
mul dx
xchg ax,bx ;放回来
add bx,ax ;把新来的ax加到bx
loop new
final:
movsx eax,bx
call writeint
exit
main endp
end main
4.提取数字 push pop
include irvine32.inc
.data
num dd 3215
ten dd 10
.code
main proc
xor eax,eax
xor edx,edx
mov ecx,0
mov eax,num
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
5.模拟辗转相除法
INCLUDE Irvine32.inc
.code
main PROC
mov eax,16
mov ebx,32
lop:
xchg eax,ebx
mov edx,0
div ebx
mov eax,edx
test ax,ax
jnz lop
mov eax,ebx
call writeint
exit
main ENDP
END main
6.n!,参考了《Intel汇编语言程序设计》一书的做法,理解了n久,就是没听ret和esp ebp的错include irvine32.inc
.data
.code
main proc
push 3
call digui
call writeint
exit
main endp
digui proc
push ebp ;用来计算return的
mov ebp,esp
mov eax,[ebp+8]
cmp eax,0
ja lop1
mov eax,1
jmp lop2
lop1:
dec eax
push eax
call digui
return:
mov ebx,[ebp+8]
mul ebx
lop2:
pop ebp
ret 4 ;强烈注意ret用法
digui endp
end main