2011-10-23 7 views
3

数字を書き込むように指示するメッセージをユーザーに要求してから、この番号を保存して何らかの操作を実行する必要があります INT 21hで検索した後、この:INT 21h(DOS)と8086を使用して数字を読むasshbly

INT 21h/AH=1 - read character from standard input, with echo, result is stored in AL. 
if there is no character in the keyboard buffer, the function waits until any key is pressed. 

example: 

    mov ah, 1 
    int 21h 

これが唯一の1つの文字を読み取り、私は番号を記入する必要がある場合はその「357」 私は3、5として、それを読みますASCII としてそれを表現することを主な問題、7

これは私の目標ではありません。 アイデア?

+0

を置きますあなたが望む3つの文字があるまで、ループの中でread-one-character呼び出しを置く必要があります。 –

+0

@Pet eWilson私は "全体"として全体の数字を読む必要があります 私はそれに加えて例えば...などを行うことができます – xsari3x

答えて

4

When you managed to get the user input、次の3つの文字を読むために必要がある場合はESIでのポインタ(文字列へのESI =アドレス)

.DATA 
myNumber BYTE "12345",0  ;for test purpose I declare a string '12345' 

Main Proc 
    xor ebx,ebx    ;EBX = 0 
    mov esi,offset myNumber ;ESI points to '12345' 

loopme: 

    lodsb      ;load the first byte pointed by ESI in al 

    cmp al,'0'     ;check if it's an ascii number [0-9] 
    jb noascii     ;not ascii, exit 
    cmp al,'9'     ;check the if it's an ascii number [0-9] 
    ja noascii     ;not ascii, exit 

    sub al,30h     ;ascii '0' = 30h, ascii '1' = 31h ...etc. 
    cbw      ;byte to word 
    cwd      ;word to dword 
    push eax 
    mov eax,ebx    ;EBX will contain '12345' in hexadecimal 
    mov ecx,10 
    mul ecx     ;AX=AX*10 
    mov ebx,eax 
    pop eax 
    add ebx,eax 
    jmp loopme     ;continue until ESI points to a non-ascii [0-9] character 
    noascii: 
    ret      ;EBX = 0x00003039 = 12345 
Main EndP 
+2

私はあなたのために私の帽子をヒント、ありがとう – xsari3x

2

文字列を取得したら、数字に変換する必要があります。問題は、それを行うための独自のプロシージャをコーディングする必要があることです。これは私が通常使っているものです(Cで書かれています):

int strToNum(char *s) { 
    int len = strlen(s), res = 0, mul = 0; 
    char *ptr = s + len; 

    while(ptr >= s) 
     res += (*ptr-- - '0') * (int)pow(10.0, mul++); 

    return res; 
} 

ここで説明します。まず、*ptr-- - '0'は数値の整数表現を取得します('9' - '0' = 9)。それで、それは前のcharを指し示すようにptrになります。その数を知ったら、入力が「357」であると仮定し、どのようなコードがないことである:

('7' - '0' = 7) * 10^0 = 7 + 
('5' - '0' = 5) * 10^1 = 50 + 
('3' - '0' = 3) * 10^2 = 300 = 
--------------------------------- 
          357 
+0

アセンブリでこれを行う方法:)? – xsari3x

+0

@ xsari3x:コードをアセンブリに変換する;)最も難しい部分は、自分でコード化する必要があるpow()関数です。しかし、16進数値が必要な場合は、単純なシフトでそれを行うことができます。 – BlackBear

関連する問題