2012-02-09 27 views
1

下面該代碼發生在一個兩位數字(比方說12)和應該返回這個號碼作爲數字12(NOT ASCII)裝配AT&T語法,在ASCII返回一個ASCII整數

mov  $IBUFF, %eax # IBUFF stores the ASCII number 12 
movl $NUMBER, %ecx # NUMBER will i put the number 12 (NO ASCII) 

movb (%eax), %bl  # Moves the first byte (1) to %bl 
subb $48, %bl  # Decrease %bl so it is not longer a ASCII sign. 
movb %bl, (%ecx)  # Moves this byte into the first place in NUMBER 

inc %eax    # Increase IBUFF so it points to the next integer 
inc %ecx    # Increase NUMBER so it points to the next empty space 

movb (%eax), %bl  # Same as above 
subb $48, %bl  # Same as above 
movb %bl, (%ecx)  # Same as above 


mov NUMBER, %eax  # Move the number 12 from NUMBER to %eax (return register) 

當我運行這段代碼的返回值是513,而不是12.我在做什麼錯了?

回答

1

您需要累積結果(可能在一個寄存器中)乘以10,然後添加新的數字。基本的基地10代表。你在做什麼最終存儲結果數字基地256.

+0

你有這樣做的一些示例代碼? – Bewn 2012-02-14 10:22:18

+0

@Bewn - 不,但將數字乘以10,然後再添加另一個數字不應該那麼難... – 2012-02-15 03:55:17

+0

Okey,今晚我會試試它。謝謝 ! – Bewn 2012-02-16 10:51:05