2010-11-22 28 views
6

Khi tôi thêm hai giá trị trong cụm 16 bit, cách tốt nhất để in kết quả vào bảng điều khiển là gì?In số nguyên để bàn điều khiển trong lắp ráp x86

Tại thời điểm này tôi có mã này:

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

Tôi nghĩ rằng giá trị dl nên trong mã ASCII, nhưng tôi không chắc chắn làm thế nào để chuyển đổi giá trị rìu sau khi bổ sung vào ASCII.

Trả lời

10

Bạn về cơ bản muốn chia cho 10, in phần còn lại (một chữ số), và sau đó lặp lại với thương.

; 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 

Như một mặt lưu ý, bạn có một lỗi trong mã của bạn ngay tại đây:

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. 

Bạn đặt 2 trong ah, và sau đó bạn đặt ax trong dl. Bạn đang cơ bản junking ax trước khi in nó.

Bạn cũng có kích thước không khớp kể từ dl rộng 8 bit và ax rộng 16 bit.

gì bạn cần làm là lật hai dòng cuối cùng và sửa chữa không phù hợp kích thước:

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

Các thuật toán cơ bản là:

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

Lưu ý rằng điều này sẽ mang lại các chữ số theo thứ tự ngược lại, vì vậy bạn có lẽ sẽ muốn thay thế "phát ra" bước với một cái gì đó mà các cửa hàng mỗi chữ số, vì vậy sau đó bạn có thể lặp lại ngược lại các chữ số được lưu trữ.

Ngoài ra, lưu ý rằng để chuyển đổi số nhị phân từ 0 đến 9 (thập phân) thành ascii, chỉ cần thêm mã ascii cho '0' (48) vào số đó.

1
mov dl, ax 

Điều này sẽ không hoạt động như dlax có kích thước bit khác nhau. Những gì bạn muốn làm là tạo ra một vòng lặp mà trong đó bạn chia giá trị 16 bit cho 10, nhớ phần còn lại trên ngăn xếp, và sau đó tiếp tục vòng lặp với kết quả phân chia số nguyên. Khi bạn đạt được kết quả là 0, hãy dọn sạch chữ số ngăn xếp bằng chữ số, thêm 48 vào các chữ số để biến chúng thành chữ số ASCII, sau đó in chúng.

4

Chỉ cần sửa chữa các thứ tự của @ Nathan Fellman của mã

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 
Các vấn đề liên quan