| 发表于:2007-05-11 21:17:0317楼 得分:0 |
第一题: ----------------- data segment msg db 'please input a string: ', '$ ' buff db 80,0,80 dup(?) cnta db 0 data ends code segment assume cs:code,ds:data main: mov ax,data mov ds,ax lea dx,msg mov ah,9 ;显示字符串 int 21h lea dx,buff mov ah,0ah ;等待输入一个字符串,以回车结束 int 21h mov dl,10 mov ah,2 int 21h mov dl,13 mov ah,2 int 21h ;10,13表示换行 mov bx,offset buff+2 s: mov al,[bx] cmp al, 'a ' jnz _13 ;不等于字符a则判断是不是结束符(回车键的ascii码) inc cnta jmp next _13: cmp al,13 jz exit next: inc bx jmp s exit: xor ah,ah mov al,cnta mov cl,10 div cl add ax,3030h mov bx,ax ;转移至bx中,以免ax的值在调用中断时被改变 cmp al, '0 ' jz disp1 ;如果计数器的十位数为0则不显示 mov dl,bl mov ah,2 int 21h disp1: mov dl,bh mov ah,2 int 21h mov ah,4ch int 21h code ends end main 第二题: ============== data segment buff db 9,?,9 dup(?) errmsg db 10,13, 'input error!$ ' data ends code segment assume cs:code,ds:data main: mov ax,data mov ds,ax lea dx,buff mov ah,0ah int 21h mov dl,10 mov ah,2 int 21h mov dl,13 mov ah,2 int 21h mov bx,offset buff+2 cmp byte ptr[bx+1],13 je one ;输入的二进数只有一位时的处理情况 s1: mov cl,[bx] cmp cl, '0 ' jz next cmp cl, '1 ' jz next cmp cl,13 jz _13 ;如果是回车符,则跳至标号_13处 jmp error next: sub cl,30h mov [bx],cl inc bx jmp s1 error: lea dx,errmsg mov ah,9 int 21h jmp short bye _13: mov bx,offset buff+2 push bx xor ch,ch mov cl,[bx-1] ;确定输入的有效字符的个数 push cx s3: push cx dec cx shl byte ptr [bx],cl pop cx inc bx loop s3 ;循环s3的作用在于对每个有效字符移位操作。比如输入的二进数为5位数(11111b), ;则第一位向左移4位,第二位向左移3位,第三位向左移2位,依此类推 pop cx pop bx mov dl,[bx] dec cx s4: inc bx add dl,[bx] loop s4 ;循环s4的作用就是将移位后的各个数相加,得到实际的[非可显示的]16进数 mov dh,dl mov cl,4 shr dh,cl and dl,00001111b xchg dh,dl add dx,3030h cmp dh, '9 ' jg h37dh ;大于9则再加7h jmp short dl7h h37dh: add dh,7h dl7h: cmp dl, '9 ' jg h37dl jmp short quit h37dl: add dl,7h ;使16进数成为能显示在屏幕上的相对应的字符 ;以上出现的标号中的 'l '都是小写字母 quit: cmp dl, '0 ' je no0 ;如果16进数的高位为0则不显示 mov ah,2h int 21h no0: mov dl,dh mov ah,2h int 21h jmp short disph ;输入的二进数不止一位时直接跳到disph one: mov dl,[bx] mov ah,2h int 21h disph: mov dl, 'h ' ;显示十六进数后的标识 mov ah,2h int 21h bye: mov ah,4ch int 21h code ends end main | | |
|