汇编语言(十四)之判断字符串是否包含数字
生活随笔
收集整理的這篇文章主要介紹了
汇编语言(十四)之判断字符串是否包含数字
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
輸入一串字符串,判斷字符串里面是否包含數字,如果包含數字輸出把cl的第五位設置為1,否則設置為0
程序運行:
?
代碼:
datas segmentSTRING_maxLength db 0ffhSTRING_Length db ?STRING db 100h dup(?)inputPrompt db 'input a line:$'outputPrompt db 'cl=$'nextLine db 0dh,0ah,'$'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,inputPromptmov ah,9int 21h;輸入一行字符lea dx,STRING_maxLengthmov ah,10int 21h;輸出換行lea dx,nextLinemov ah,9int 21h;mov dl,cl;call toBinaryPrintmov ch,STRING_Length ;把字符串的長度移至chand cl,11011111b ;初始化cl第5位為0;若字符串長度為0,直接輸出clcmp ch,0 jz breakmov bx,0 s:mov al,STRING[bx] ;將STRING中單元移至alcmp al,'0' ;比較al與'0'的大小jl next ;若al<'0',則跳轉cmp al,'9' ;否則,比較al與'9'的大小jg next ;若al>'9',則跳轉or cl,00100000b ;否則,設置cl的第五位為1jmp break ;跳出循環 next:add bx,type STRING ;STRING索引移至下一個單元dec ch ;計數器減1 cmp ch,0 ;比較ch與0ja s ;若ch>0,則循環繼續break:;輸出cl的二進制提示lea dx,outputPromptmov ah,9int 21h;輸出clmov dl,cl ;將cl移至dlcall toBinaryPrintretmain endptoBinaryPrint proc near;保存寄存器push dspush axpush cxpush dxpush bx;設置ds為cspush cspop ds;把目的數dl轉換成二進制字符串mov cx,8 ;設置循環次數mov bx,0 ;設置binaryArray下標bin:shl dl,1 ;左移一位jnc zero ;移除的位不為1(即0),跳轉mov binaryArray[bx],'1' ;若移除的位為1,則設置binaryArray[bx]為‘1’jmp next ;跳轉至zero:mov binaryArray[bx],'0' ;若移除的位為0,則設置binaryArray[bx]為‘0’next:add bx,type binaryArray ;binaryArray數組下標移至下一個元素loop binmov binaryArray[bx],'b' ;在二進制字符串后加上二進制單位mov binaryArray[bx+1],'$' ;設置字符串的結束標志;輸出目的數的二進制lea dx,binaryArraymov ah,9int 21h;恢復寄存器 pop bx pop dx pop cxpop axpop dsretbinaryArray db 16 dup(?) toBinaryPrint endp codes endsend main?
總結
以上是生活随笔為你收集整理的汇编语言(十四)之判断字符串是否包含数字的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 汇编语言(十三)之偶数转成哥德巴赫猜想
- 下一篇: 汇编语言(十五)之找出两个数组中的相同元