質問

私はアセンブリ16ビットで2つの値を追加すると、コンソールに結果を印刷するための最良の方法は何ですか?

私はこのコードを持っている瞬間ます:

;;---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---;;

私はdlの値がASCIIコードであるべきだと思うが、私はASCIIに加えた後、斧値を変換するかどうかはわかりません。

役に立ちましたか?

解決

あなたは基本的に残りの(一桁)を印刷し、その後商で繰り返し、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.

あなたは2ahを入れ、その後、あなたはaxdlを置きます。あなたは基本的にそれを印刷する前にaxをjunkingしています。

dlは8ビット幅で、axは16ビット幅であるため、あなたはまた、サイズの不一致があります。

は何をすべきことは、最後の2行を反転し、サイズの不一致を修正されます:

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.

他のヒント

ちょうど@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

基本的なアルゴリズムである。

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

あなたはおそらく何か店舗各桁なので、後で反復保存された数字を超える逆にできることをそれと「EMIT」ステップを交換したいしようとしているので、これは、逆の順番に数字が得られることに注意してください。

また、わずか数に(48で)「0」のASCIIコードを追加し、ASCIIに0〜9(10進数)との間の2進数を変換することに注意してください。

mov dl, ax
dlaxが異なるビットサイズを持っているように、

これは動作しません。あなたがやりたいことは、あなたが10で16ビットの値を分割するループを作成し、スタック上の残りの部分を覚えて、その後、整数の除算結果でループを続けています。あなたが0の結果に到達すると、その後、それらを印刷、ASCII数字にそれらを回すために数字に48を追加し、桁スタック桁をクリーンアップします。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top