2010-11-22 15 views
6

में कंसोल के लिए पूर्णांक प्रिंट करें जब मैं 16 बिट असेंबली में दो मान जोड़ता हूं, तो परिणाम को कंसोल पर प्रिंट करने का सबसे अच्छा तरीका क्या है?x86 असेंबली

फिलहाल मैं इस कोड है:

;;---CODE START---;; 
mov ax, 1 ;put 1 into ax 
add ax, 2 ; add 2 to ax current value 
mov ah,2 ; 2 is the function number of output char in the DOS Services. 
mov dl, ax ; DL takes the value. 
int 21h ; calls DOS Services 

mov ah,4Ch ; 4Ch is the function number for exit program in DOS Services. 
int 21h  ; function 4Ch doesn't care about anything in the registers. 
;;---CODE END---;; 

मुझे लगता है कि डीएल मूल्य ASCII कोड में होना चाहिए, लेकिन मुझे यकीन है कि कैसे ASCII में इसके बाद कुल्हाड़ी मूल्य कन्वर्ट करने के लिए नहीं कर रहा हूँ।

उत्तर

10

आप मूल रूप से 10 से विभाजित करना चाहते हैं, शेष (एक अंक) प्रिंट करें, और फिर भाग्य के साथ दोहराएं।

; assume number is in eax 
    mov ecx, 10 

loophere: 
    mov edx, 0 
    div ecx 

    ; now eax <-- eax/10 
    ;  edx <-- eax % 10 

    ; print edx 
    ; this is one digit, which we have to convert to ASCII 
    ; the print routine uses edx and eax, so let's push eax 
    ; onto the stack. we clear edx at the beginning of the 
    ; loop anyway, so we don't care if we much around with it 

    push eax 

    ; convert dl to ascii 
    add dl, '0' 

    mov ah,2 ; 2 is the function number of output char in the DOS Services. 
    int 21h ; calls DOS Services 

    ; now restore eax 
    pop eax 

    ; if eax is zero, we can quit 

    cmp eax, 0 
    jnz loophere 

एक तरफ ध्यान दें के रूप में, आप यहीं से अपने कोड में एक बग है:

mov ax, 1 ;put 1 into ax 
add ax, 2 ; add 2 to ax current value 
mov ah,2 ; 2 is the function number of output char in the DOS Services. 
mov dl, ax ; DL takes the value. 

आप ah में 2 डाल दिया, और फिर आप dl में ax डाल दिया। प्रिंटिंग से पहले आप ax मूल रूप से जंकिंग कर रहे हैं।

आपके पास dl 8 बिट्स चौड़ा और ax 16 बिट चौड़ा है, इसलिए आपके आकार का मिलान भी है।

mov ax, 1 ;put 1 into ax 
add ax, 2 ; add 2 to ax current value 

mov dl, al ; DL takes the value. 
mov ah,2 ; 2 is the function number of output char in the DOS Services. 
4

बुनियादी एल्गोरिथ्म है:

divide number x by 10, giving quotient q and remainder r 
emit r 
if q is not zero, set x = q and repeat 

ध्यान दें कि यह उलटा क्रम में अंक निकलेगा, तो आप शायद बदलने के लिए "फेंकना" कुछ है कि प्रत्येक अंक संग्रहीत करता है के साथ कदम चाहते करने जा रहे हैं, इसलिए कि आप बाद में संग्रहीत अंकों के विपरीत में फिर से शुरू कर सकते हैं।

इसके अलावा, ध्यान दें कि 0 और 9 (दशमलव) से एएससीआई के बीच बाइनरी संख्या को परिवर्तित करने के लिए, बस संख्या में '0' (जो 48 है) के लिए एएससीआई कोड जोड़ें।

1
mov dl, ax 

इस रूप में dl और ax अलग सा आकार है काम नहीं करेगा। आप जो करना चाहते हैं वह एक लूप बनाएं जिसमें आप 16 बिट मान को 10 तक विभाजित करते हैं, बाकी को स्टैक पर याद रखें, और उसके बाद पूर्णांक विभाजन परिणाम के साथ लूप जारी रखें। जब आप 0 के परिणाम तक पहुंचते हैं, तो अंकों से स्टैक अंकों को साफ करें, उन्हें ASCII अंकों में बदलने के लिए अंकों को 48 जोड़ दें, फिर उन्हें प्रिंट करें।

4

बस @Nathan Fellman के कोड

PrintNumber proc 
    mov cx, 0 
    mov bx, 10 
@@loophere: 
    mov dx, 0 
    div bx       ;divide by ten 

    ; now ax <-- ax/10 
    ;  dx <-- ax % 10 

    ; print dx 
    ; this is one digit, which we have to convert to ASCII 
    ; the print routine uses dx and ax, so let's push ax 
    ; onto the stack. we clear dx at the beginning of the 
    ; loop anyway, so we don't care if we much around with it 

    push ax 
    add dl, '0'      ;convert dl to ascii 

    pop ax       ;restore ax 
    push dx       ;digits are in reversed order, must use stack 
    inc cx       ;remember how many digits we pushed to stack 
    cmp ax, 0      ;if ax is zero, we can quit 
jnz @@loophere 

    ;cx is already set 
    mov ah, 2      ;2 is the function number of output char in the DOS Services. 
@@loophere2: 
    pop dx       ;restore digits from last to first 
    int 21h       ;calls DOS Services 
    loop @@loophere2 

    ret 
PrintNumber endp 
के आदेश फिक्सिंग:

आपको क्या करना चाहिए क्या अंतिम दो पंक्तियों फ्लिप और आकार बेमेल ठीक है

संबंधित मुद्दे