2014-11-23 159 views
3

如何在屏幕(不是ASCII碼符號)上打印數組的數字?彙編在屏幕上打印數字

MODEL SMALL 
STACK 256 
DATASEG 
    Data1 dw 4h, -23h, 3h, 276h, -79h, -345h, 6h, 44h, -77h, 111, 12,10, '$' 
CODESEG 
Start: 
mov ax,@data  
mov ds,ax 
mov dx,offset Data1      
mov ah,09h 
int 21h 

Exit: 
    mov ah,04Ch 
    mov al,0h  
    int 21h  

End Start 

此代碼打印出與數組中的數字對應的ASCII符號。我想打印數字

+0

您需要一個代碼塊來確定每個數字(無論您喜歡什麼樣的基數)並找到t他適當的性格。不幸的是,沒有直接的辦法。 – 2014-11-23 12:24:44

回答

3

我假設您想獲得簽名的數字,並且您知道負數如何編碼(請參閱Wikipedia's article of Ones' complement。MSDOS不提供將數據轉換爲數字的準備功能,因此您必須自己創建這樣一個函數,主要問題是重複除以10,然後處理餘數,網上有很多例子和解釋,用谷歌搜索:「彙編轉換爲十進制」

這個是我的方法(TASM):

LOCALS @@ 

MODEL SMALL 
STACK 256 
DATASEG 
    Data1 dw 4h, -23h, 3h, 276h, -79h, -345h, 6h, 44h, -77h, 111, 12,10, '$' 
    Decimal db 8 DUP ('$') 
CODESEG 

main PROC 
     mov ax, @data 
     mov ds, ax 
     mov es, ax 

     mov si, OFFSET Data1 

    Loop: 

     mov di, OFFSET Decimal ; Convert WORD to decimal string 
     mov ax, [si] 
     call ax2dec_signed 

     push si     ; store SI for later use 

     mov dx, OFFSET Decimal ; Print the number 
     mov ah,09h 
     int 21h 

     mov ah, 02h    ; Print a space 
     mov dl, 20h 
     int 21h 

     pop si     ; restore SI 
     add si, 2    ; next WORD 
     cmp word ptr [si], '$' ; End of String? 
     jne Loop    ; No: once more 

    Exit: 
     mov ah,04Ch 
     mov al,0h 
     int 21h 
main ENDP 

ax2dec_signed PROC    ; Args: AX register to convert, ES:DI pointer to target string 
     test ax, ax    ; AX negative? 
     jns @@J1    ; No: skip the following code 
     mov byte ptr [di], '-' ; Yes: store the negative sign 
     inc di     ; Increment the pointer to the target string 
     neg ax     ; One's complement, i.e. AX = ABS(AX) 
    @@J1: 
     mov bx, 10    ; Base 10 -> divisor 
     xor cx, cx    ; CX=0 (number of digits) 
    @@Loop_1: 
     xor dx, dx    ; Clear DX for division (don't forget it!) 
     div bx     ; AX = DX:AX/BX Remainder DX 
     push dx     ; Push remainder for LIFO in Loop_2 
     add cl, 1    ; Equivalent to 'inc cl' 
     test ax, ax    ; AX = 0? 
     jnz @@Loop_1    ; No: once more 
    @@Loop_2: 
     pop ax     ; Get back pushed digits 
     or al, 00110000b  ; Conversion to ASCII 
     stosb     ; Store only AL to [ES:DI] (DI is a pointer to a string) 
     loop @@Loop_2   ; Until there are no digits left 
     mov [di], '$'   ; Store termination character for 'int 21h fn 09h' 
     ret      ; Ret: target string contains decimal '$'-terminated ASCII-String 
ax2dec_signed ENDP 

END main 
+1

感謝您的回答。這非常有幫助 – 2014-11-27 22:08:56