8086汇编输入一正整数,统计其位数,输出各个位数及反向输出各个位数(逆序数)(输入范围:1~32767,带提示信息)
Posted u25th_engineer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了8086汇编输入一正整数,统计其位数,输出各个位数及反向输出各个位数(逆序数)(输入范围:1~32767,带提示信息)相关的知识,希望对你有一定的参考价值。
(二战复习忙里偷闲)出这个程序的原因是看到群里有同学贴了某个神仙写的搞笑程序。
图 1 程序运行示意(输入1位正整数)
图 2 程序运行示意(输入2位和3位正整数)
图 3 程序运行示意(输入4位正整数)
图 4 程序运行示意(输入边界数据和5位正整数)
stack segment stack
db 512 dup(?)
stack ends
data segment
string1 db "Press any key to continue!", 0ah, 0dh, '$'
string2 db " ", '$'
string3 db "Please enter an integer(1~32767):", 0ah, 0dh, '$'
string4_1 db "There are ", '$'
string4_2 db " digits in this integer.", 0ah, 0dh, '$'
string5 db "Each digit in this integer is as follows:", 0ah, 0dh, '$'
string6 db "The reverse order of each digit in this integer is as follows:", 0ah, 0dh, '$'
data ends
code segment
assume ds: data, cs: code, ss: stack
main:
mov ax, stack
mov ss, ax
mov ax, data
mov ds, ax
lea dx, string3
mov ah, 09h
int 21h
call far ptr readsiw
push ax
call far ptr solve
jmp done
;
; intput: register ax, the integer to be processed.
; output: none
; function:
; 1) To count the amount of digit in the input integer;
; 2) To display each digit of the input integer;
; 3) To display each digit of the input integer reversely.
;
solve proc far
push bp
mov bp, sp
sub sp, 8
push si
push di
mov di, word ptr [bp+6]
mov word ptr [bp-4], 0
mov ax, di
mov cx, ax
mov word ptr [bp-2], ax
mov ax, word ptr [bp-2]
mov word ptr [bp-6], ax
mov si, 1
jmp short output_digit_cnt
cnt_digit:
mov bx, 10
mov ax, di
cwd
idiv bx
mov di, ax
inc word ptr [bp-4]
output_digit_cnt:
or di, di
jne short cnt_digit
lea dx, string4_1
mov ah, 09h
int 21h
mov ax, word ptr [bp-4]
push ax
call far ptr dispsiw
lea dx, string4_2
mov ah, 09h
int 21h
lea dx, string5
mov ah, 09h
int 21h
jmp short tot_judges_10
cnt_power:
mov dx, 10
mov ax, si
imul dx
mov si, ax
mov bx, 10
mov ax, word ptr [bp-6]
cwd
idiv bx
mov word ptr [bp-6], ax
tot_judges_10:
cmp word ptr [bp-6], 10
jge short cnt_power
jmp short output_digit
extract_digit:
mov ax, word ptr [bp-2]
cwd
idiv si
mov word ptr [bp-8], ax
mov ax, word ptr [bp-8]
push ax
call far ptr dispsiw
lea dx, string2
mov ah, 09h
int 21h
mov ax, word ptr [bp-2]
cwd
idiv si
mov word ptr [bp-2], dx
mov bx, 10
mov ax, si
cwd
idiv bx
mov si, ax
output_digit:
cmp si, 1
jge short extract_digit
call far ptr lineFeed
lea dx, string6
mov ah, 09h
int 21h
jmp short restore_register
extract_digit_reverse:
mov ax, cx
mov bx, 10
cwd
idiv bx
mov ax, dx
push ax
call far ptr dispsiw
lea dx, string2
mov ah, 09h
int 21h
mov bx, 10
mov ax, cx
cwd
idiv bx
mov cx, ax
restore_register:
or cx, cx
jne short extract_digit_reverse
call far ptr lineFeed
jmp short solve_done
solve_done:
pop di
pop si
mov sp, bp
pop bp
ret
solve endp
readsiw proc far
push bx
push cx
push dx
xor bx, bx
xor cx, cx
mov ah, 1
int 21h
cmp al, '+'
jz rsiw1
cmp al, '-'
jnz rsiw2
mov cx, -1
rsiw1:
mov ah, 1
int 21h
rsiw2:
cmp al, '0'
jb rsiw3
cmp al, '9'
ja rsiw3
sub al, 30h
xor ah, ah
shl bx, 1
mov dx, bx
shl bx, 1
shl bx, 1
add bx, dx
add bx, ax
jmp rsiw1
rsiw3:
cmp cx, 0
jz rsiw4
neg bx
rsiw4:
mov ax, bx
pop dx
pop cx
pop bx
ret
readsiw endp
dispsiw proc far
push ax
push bx
push dx
test ax, ax
jnz dsiw1
mov dl, '0'
mov ah, 2
int 21h
jmp dsiw5
dsiw1:
jns dsiw2
mov bx, ax
mov dl, '-'
mov ah, 2
int 21h
mov ax, bx
neg ax
dsiw2:
mov bx, 10
push bx
dsiw3:
cmp ax, 0
jz dsiw4
xor dx, dx
div bx
add dl, 30h
push dx
jmp dsiw3
dsiw4:
pop dx
cmp dl, 10
je dsiw5
mov ah, 2
int 21h
jmp dsiw4
dsiw5:
pop dx
pop bx
pop ax
ret
dispsiw endp
lineFeed proc far
push ax
push dx
mov dl, 0dh
mov ah, 2
int 21h
mov dl, 0ah
mov ah, 2
int 21h
pop dx
pop ax
ret
lineFeed endp
done:
lea dx, string1
mov ah, 09h
int 21h
mov ah, 07h
int 21h
mov ah, 4ch
int 21h
code ends
end main
以上是关于8086汇编输入一正整数,统计其位数,输出各个位数及反向输出各个位数(逆序数)(输入范围:1~32767,带提示信息)的主要内容,如果未能解决你的问题,请参考以下文章
8086汇编输入一正整数,统计其位数,输出各个位数及反向输出各个位数(逆序数)(输入范围:1~32767,带提示信息)