汇编语言(七)循环结构
程序员文章站
2022-07-12 18:22:18
...
1.计算 1+2+3+...+n=? 其中n通过键盘输入,累加和小于2^16。
assume ds:data,cs:code
data segment
inf1 db "Please input a number (0-65535):$"
ibuf db 7,0,6 dup (?)
obuf db 6 dup(?)
inf2 db 0ah,0dh,"1+2+...+n=$"
data ends
code segment
start:
TenTo2 macro
local again
mov dx,offset inf1
mov ah,09h
int 21h
mov dx,offset ibuf
mov ah,0ah
int 21h
mov cl,ibuf+1
mov ch,0
mov si,offset ibuf+2
mov ax,0
again: mov dx,10
mul dx
and byte ptr [si],0fh
add al,[si]
adc ah,0
inc si
loop again
endm
TwoTo10 macro
local loop1
mov bx,offset obuf+5
mov byte ptr [bx],'$'
mov cx,10
loop1: mov dx,0
div cx
add dl,30h
dec bx
mov [bx],dl
or ax,ax
jnz loop1
mov dx,bx
mov ah,09h
int 21h
endm
;;;;;;;;;;;;;;;;;;;Begin;;;;;;;;;;;;;;;;;;;;;;
mov ax,data
mov ds,ax
TenTo2
mov cx,ax
mov ax,0
mov bx,1
loop2:
add ax,bx
inc bx
loop loop2
push ax
mov dx,offset inf2
mov ah,09h
int 21h
pop ax
TwoTo10
mov ah,4ch
int 21h
code ends
end start
2.从自然数1开始累加,直到累加和大于60000为止,显示累加的自然数的个数和累加和。
显示格式为:1+2+...+n=sum
其中n为累加个数,sum为累加和。
assume ds:data,cs:code
data segment
ibuf db 7,0,6 dup (?)
obuf db 6 dup(?)
inf1 db 0ah,0dh,"1+2+...+$"
data ends
code segment
start:
TwoTo10 macro
local loop1
mov bx,offset obuf+5
mov byte ptr [bx],'$'
mov cx,10
loop1: mov dx,0
div cx
add dl,30h
dec bx
mov [bx],dl
or ax,ax
jnz loop1
mov dx,bx
mov ah,09h
int 21h
endm
;;;;;;;;;;;;;;;;;;;Begin;;;;;;;;;;;;;;;;;;;;;;
mov ax,data
mov ds,ax
mov cx,ax
mov ax,0
mov bx,1
loop2:
add ax,bx
inc bx
cmp ax,60000
jna loop2
push bx
push ax
mov dx,offset inf1
mov ah,09h
int 21h
dec bx
mov ax,bx ;>60000后 bx多加了1 故应减去
twoto10
mov dl,'='
mov ah,2
int 21h
pop ax
pop bx
TwoTo10
mov ah,4ch
int 21h
code ends
end start
3.从键盘输入一个6位的十进制数(<65535),将其转化为二进制输出显示,统计该二进制数中包含1的个数,并显示统计结果。
data segment
infor1 db 0ah,0dh,"Please Input a Decimal Number!(<65535 && 'Esc' to exit)$"
infor2 db 0ah,0dh,"Your Input is Iileage!$"
infor3 db 0ah,0dh,"Decimal To Binary is:$"
infor4 db 0ah,0dh,"the number of 1 is:$"
everbit db 0,0,0,0,0,0,0 ;[5:0]存取读入的数字真实值 [6]存取1的个数
data ends
code segment
assume cs:code,ds:data
start:
print macro str
push ax
push dx
mov dx,offset str
mov ah,09h
int 21h
pop dx
pop ax
endm
prind macro ch
push ax
push dx
mov dl,ch
mov ah,02h
int 21h
pop dx
pop ax
endm
digita macro num
local cf,nc,ouput1
push ax
push bx
push cx
push dx
mov al,num
mov cx,4
shl al,cl
cf:
shl al,1
jc ouput1
prind '0'
nc: loop cf
pop dx
pop cx
pop bx
pop ax
jmp re2
ouput1:
prind '1'
inc byte ptr [bx+6]
jmp nc
endm
;;;;;;;;;;;;;;;;;;;;;;;;begin;;;;;;;;;;;;;;;;;;;;;;;
mov ax,data
mov ds,ax
mov bx,offset everbit
input:
mov byte ptr [bx+6],0 ;每次执行完计数器清0
print infor1
mov ah,01h
int 21h
mov [bx],al
cmp al,27
jz exit
mov bx,offset everbit
mov cx,5
mov di,0
save:
inc di
mov ah,01h
int 21h
mov [bx+di],al
loop save
mov di,0
mov cx,6
print infor3
read:
mov al,[bx+di]
cmp al,byte ptr '0'
jb error
cmp al,byte ptr '9'
ja error
jmp digit
re:
digita al
re2:
inc di
loop read
jmp prin2
jmp input
exit:
mov ah,4ch
int 21h
error:
print infor2
jmp input
digit:
sub al,30h
jmp re
prin2:
print infor4
mov al,[bx+6]
cmp al,9
jna x
daa
push ax
mov ah,2
mov dl,'1'
int 21h
pop ax
and al,0fh
x:
add al,30h
mov dl,al
mov ah,2
int 21h
jmp input
code ends
end start
上一篇: java入门(七) | 循环结构
下一篇: 强连通分量