汇编语言(十三)之偶数转成哥德巴赫猜想
生活随笔
收集整理的這篇文章主要介紹了
汇编语言(十三)之偶数转成哥德巴赫猜想
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
將輸入的偶數(shù)轉(zhuǎn)換成哥德巴赫猜想,輸出哥德巴赫猜想
程序運行:
?
代碼:
datas segmentmaxESLen db 0ffhevenSLen db 0evenString db 0ffh dup(?)evenNumber dw ?prime1 dw 0 prime2 dw 0 inputPrompt db 'input a even number(number>5):$'outputInvail db 0dh,0ah,'Invail$'outputNotEven db 0dh,0ah,'Must be even$'outputNotGreater5 db 0dh,0ah,'Must greater than or equal to 6$'GoldbachConjectureValue db 0dh,0ah, 40 dup(?)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;輸出輸入數(shù)字提示lea dx,inputPromptmov ah,9int 21h;輸入數(shù)字字符串lea dx,maxESLenmov ah,10int 21h;將字符串轉(zhuǎn)換成十進制數(shù)lea si,evenStringcall todigit;判斷轉(zhuǎn)換是否有效cmp bx,0jnz availableData ;若有效,則跳轉(zhuǎn);若無效,則輸出Invaillea dx,outputInvail mov ah,9int 21hjmp exit ;跳轉(zhuǎn)至退出availableData:mov evenNumber,ax ;保存輸入的數(shù)cmp ax,6 ;比較該數(shù)與6jae s ;若大于等于6,則跳轉(zhuǎn)至判斷該數(shù)是否是偶數(shù);若小于6,則輸出Must greater than or equal to 6lea dx,outputNotGreater5 mov ah,9int 21hjmp exit ;跳轉(zhuǎn)至退出s:test ax,1 ;判斷該數(shù)是否是偶數(shù)jz s1 ;若是偶數(shù),則跳轉(zhuǎn)查找歌德巴赫猜想;若不是偶數(shù),則輸出Must be evenlea dx,outputNotEven mov ah,9int 21hjmp exit ;跳轉(zhuǎn)至退出s1:mov ax,evenNumber ;將輸入的數(shù)移至axcall findGoldbachConjecture ;查找哥德巴赫猜想exit: retmain endptodigit proc near;si=EA ax=number,bx=available;保存寄存器push cxpush dxpush simov ax,0 ;十進制數(shù)的低16位mov dx,0 ;十進制數(shù)的高16位mov di,10 ;除數(shù)mov cl,[si-1] ;將字符串的個數(shù)移至clmov ch,0 ;清除chdigits:mov bl,[si] ;將字符移至blcmp bl,'0' ;判斷字符與‘0’的大小jl invail ;若小于‘0’,則字符為無效cmp bl,'9' ;判斷字符與‘9’的大小jg invail ;若大‘9’,則字符為無效mov bh,0 ;bh清0sub bl,30h ;字符轉(zhuǎn)換成數(shù)字的值mul di ;左移一位數(shù)乘10add ax,bx ;加上個位的和adc dx,0add si,1 ;移至下一個字符的索引loop digitscmp dx,0 ;比較十進制數(shù)的高16位是否為0jnz invail ;若不為0,則跳轉(zhuǎn)說明字符串數(shù)是無效的mov bx,1 ;否則設(shè)置bx為1jmp dexit ;跳轉(zhuǎn)至退出invail:mov bx,0 ;無效則設(shè)置bx為0;恢復(fù)寄存器dexit:pop sipop dxpop cxret todigit endpfindGoldbachConjecture proc near ;ax=source ;保存寄存器push cxpush dxpush bxpush sipush dimov cx,ax ;將哥德巴赫猜想的數(shù)移至cxmov si,2 ;從2開始查找素數(shù)mov di,cx ;將哥德巴赫猜想的數(shù)移至dishr di,1 ;取該數(shù)的一半ps:call isPrimeNumber ;判斷si是否為素數(shù)cmp bx,0 ;判斷返回值是否有效jz next ;若不為素數(shù),則跳轉(zhuǎn)下一個數(shù)mov dx,cx ;若為素數(shù),則將哥德巴赫猜想的數(shù)移至dxsub dx,si ;求差push si ;保存simov si,dx ; 將差移至sicall isPrimeNumber ;判斷差是否為素數(shù)pop si ;恢復(fù)sicmp bx,0 ;判斷返回值是否有效jz next ;若不為素數(shù),則跳轉(zhuǎn)下一個數(shù)call outputGoldbachConjecture ;若為素數(shù),則輸出哥德巴赫猜想next:inc si ;查找下一個數(shù)是不是素數(shù)cmp si,di ;判斷si是否大于哥德巴赫猜想的數(shù)的一半jbe ps ;若小于等于,則循環(huán)繼續(xù);恢復(fù)寄存器pop dipop sipop bxpop dxpop cxretfindGoldbachConjecture endpoutputGoldbachConjecture proc near;保存寄存器push axpush di;設(shè)置輸出哥德巴赫猜想lea di,GoldbachConjectureValue+2 ;設(shè)置輸出哥德巴赫猜想的數(shù)的字符串地址mov ax,evenNumber ;將哥德巴赫猜想的數(shù)移至axcall toDecimalist ;轉(zhuǎn)換成數(shù)字字符mov byte ptr [di],'=' ;保存‘=’inc di ;移至下一個字符保存的索引mov ax,si ;將第一個素數(shù)移至axcall toDecimalist ; 轉(zhuǎn)換成數(shù)字字符mov byte ptr [di],'+' ;保存‘+’inc di ;移至下一個字符保存的索引mov ax,dx ;將第二個素數(shù)移至axcall toDecimalist ;轉(zhuǎn)換成數(shù)字字符mov byte ptr [di],'$' ;設(shè)置字符串的結(jié)束符;輸出哥德巴赫猜想lea dx,GoldbachConjectureValuemov ah,9int 21h;恢復(fù)寄存器pop dipop axret outputGoldbachConjecture endpisPrimeNumber proc near;si=source bx=isPrime;保存寄存器push dxpush sipush dimov bx,si ;將判斷素數(shù)的數(shù)移至bxcmp bx,2 ;判斷該數(shù)是否為2jz prime ;若為2,則跳轉(zhuǎn)說明該數(shù)為素數(shù)cmp bx,3 ;判斷該數(shù)是否為3jz prime ;若為3,則跳轉(zhuǎn)說明該數(shù)為素數(shù)mov di,bx ;將該數(shù)移至dishr di,1 ;取該數(shù)的一半mov si,2 ;判斷素數(shù)從2開始cmp si,di ;判斷si是否大于該數(shù)的一半ja notPrime ;若大于,則該數(shù)不是素數(shù)pIS:mov ax,bx ;將該數(shù)移至axmov dx,0 ;無符號數(shù)擴展 div si ;除以sicmp dx,0 ;判斷余數(shù)是否為0jz notPrime ;若余數(shù)為0,則不是素數(shù),跳轉(zhuǎn)inc si ;若余數(shù)不為0,則si加1cmp si,di ;比較si與該數(shù)的一半的大小jbe pIS ;若小于等于,繼續(xù)循環(huán)prime:mov bx,1 ;是素數(shù),設(shè)置bx為1jmp pexit ;跳轉(zhuǎn)至退出notPrime: mov bx,0 ;不是素數(shù),設(shè)置bx為0;恢復(fù)寄存器pexit:pop dipop sipop dxret isPrimeNumber endptoDecimalist proc near;ax=source di=EA;保存寄存器push cxpush dxpush bxmov cx,0 ;計數(shù)器,計算十進制數(shù)的位數(shù)mov bx,10 ;除數(shù)ps:mov dx,0 ;無符號數(shù)擴展div bx ;除以10push dx ;保存余數(shù)inc cx ;計數(shù)器加1,位數(shù)增加cmp ax,0 ;判斷被除數(shù)是否為0jnz ps ;若非0,則循環(huán)繼續(xù)ps1:pop dx ;彈出余數(shù)add dl,30h ;轉(zhuǎn)換成數(shù)字字符mov [di],dl ;保存數(shù)字字符;mov ah,2 ;int 21hinc di ;移至下一個字符保存的索引loop ps1 ;恢復(fù)寄存器pop bxpop dxpop cxret toDecimalist endpcodes endsend main?
總結(jié)
以上是生活随笔為你收集整理的汇编语言(十三)之偶数转成哥德巴赫猜想的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 笔记本设置wifi软件 这个方法你学会了
- 下一篇: 汇编语言(十四)之判断字符串是否包含数字