1

したがって、私はアセンブリ言語にかなり慣れていないため、基礎をかなりしっかりと把握していますが、ユーザーの入力には常に困惑しています。したがって、現在、ユーザーから1桁を受け取る次のコードがあります。

mov eax, 3
mov ebx, 0
mov ecx, inStrBuf
mov edx, StrLen
int 80h

そして、定義は次のとおりです

SECTION .bss
inStrBuf:  times StrLen resb  ' ' 

Section .data
StrLen: equ 8 

ecx に値を入力すると、値は数字 + 2608 になります。つまり、私が行っているのは、単純に 2608 を引いて数字を取得することです。46 のように複数の数字を入力すると、10 進数に変換すると 669236 になります。以前のように 2608 を単純に減算する方法はありません。

まず第一に、2608 はどうなっているのですか? 654 のような数値を受け入れてレジスターに (もちろん 16 進値で) 入れる方法はありますか? ありがとう!

4

1 に答える 1

2

2608 がどこから来たのか、ましてや 669236 がどこから来たのかわからない! 一般的な考え方は次のとおりです。

;zero out someplace to put result
top:
;get a digit/character
;make sure it represents a decimal digit
;(if not - go to done)
;subtract '0' to convert character to number
;multiply "result so far" by 10
;add in the new number
;go to top
done:

普段使っているのはこれ…

section .bss
    inStrBuf resb StrLen ; 12+ is good...

section .text
    ...
    push inStrBuf ; pass parameter on stack
    call atoi
    add esp, 4 ; clean up stack
    mov [someplace], eax
    ...

;--------------------
atoi:
    push ebx

    mov edx, [esp + 8]  ; pointer to string
    xor ebx, ebx ; assume not negative

    cmp byte [edx], '-'
    jnz .notneg
    inc ebx ; indicate negative
    inc edx ; move past the '-'
.notneg:

    xor eax, eax        ; clear "result"
.top:
    movzx ecx, byte [edx]
    inc edx
    cmp ecx, byte '0'
    jb .done
    cmp ecx, byte '9'
    ja .done

    ; we have a valid character - multiply
    ; result-so-far by 10, subtract '0'
    ; from the character to convert it to
    ; a number, and add it to result.

    lea eax, [eax + eax * 4]
    lea eax, [eax * 2 + ecx - '0']

    jmp short .top
.done:
    test ebx, ebx
    jz .notminus
    neg eax
.notminus:
    pop ebx
    ret
;------------------------

これは、2 つの「賢い」方法を使用しleaて、10 を掛け、'0' を引き、新しい数を足します。フラグを設定しないという欠点があるため、オーバーフローをチェックできません。静かにロールオーバーするだけです。「無効な」文字は停止します-xero、改行(sys_readがそこにある)...または「ガベージ」で機能します。返されると、「無効な」文字が ecx にあり (cl だけが興味深い)、edx は次の文字を指します。「192.168.1.1」などの解析に便利です。もっと単純なものを使用することを好むかもしれません。:) C ライブラリの「atoi」または「scanf」が機能します...そのようにしたい場合は...

その2608がどこから来たのか本当に興味があります!

于 2012-12-04T09:03:45.120 に答える