输入四进制的数,转为十进制输出
程序运行:
代码:
datas segmentN_string_max_length db 0ffhN_string db 0, 100h dup(?)N dw 0,0 sum dd 0 input db 'input quanternary=$'output db 0dh,0ah,'output decimal=$'datas endsstacks segment stackdb 100h dup(?)stacks endscodes segmentassume cs:codes,ds:datas,ss:stacks
main proc far
start:push dsmov ax,0hpush axmov ax,datas ;初始化dsmov ds,ax;输入提示lea dx,inputmov ah,9int 21h;输入Nlea dx,N_string_max_lengthmov ah,10 int 21h;转换成为数字lea si,N_stringcall todigitmov N,axmov N+2,dx;输出提示lea dx,outputmov ah,9int 21hmov ax,N mov dx,N+2 call print_decimalretmain endptodigit proc near uses bx cx si di ;si ,axmov cl,[si] ;设置循环次数xor ch,ch cmp cx,8ja errinc si ;si指向第一个元素mov ax,0 ;初始化ax,dxmov dx,0 mov di,4 ;乘数dig:mov bl,[si] cmp bl,'0' ;判断是否小于'0'jb err ;若低于'0',则错误字符cmp bl,'3' ;判断是否大于'3'ja err ;若高于'3',则错误字符sub bl,30h ;否则,将数字字符转成数字xor bh,bh mul di ;乘4add ax,bx ;加上个位adc dx,0 inc siloop digjmp exit err: ;输出错误push ds push cs pop ds lea dx,error_tip mov ah,9int 21hpop dsmov ah,4ch int 21hexit:ret error_tip db 0dh,0ah,'input error$'
todigit endpprint_decimal proc near uses ax bx cx dx mov bx,10 mov cx,0 dea: div bx push dx inc cx xor dx,dx cmp ax,0 jnz deadea1:pop dxadd dl,30hmov ah,2int 21hloop dea1 ret
print_decimal endp
codes endsend main