2014-04-29 133 views
0

我正在編寫一個簡單的sprintf()函數。當我嘗試除以$ 0xA時,我似乎無法找出爲什麼我的代碼得到一個算術豁免。如果我將所有內容都更改爲擴展版本(如%EAX),則不會發生這種情況。爲什麼使用divw時會出現算術異常?

.data 
divisor: .byte 0xA 

.text 
.globl sprinter 
sprinter: 

. 
. 
. 

add_unsigned_decimal_number: 

pushl (%ebx) 
call uint_inner 
jmp next_char 
uint_inner: 
xor %eax, %eax 
xor %edx, %edx 
movw 4(%esp), %dx #Move first half of the parameter into %dx 
movw 6(%esp), %ax #Move second half into %ax 
#################Here it crashes################### 
divw divisor #Divide by 10 (0xA) 
################################################### 
cmpw $0x0, %ax 
je return #return if equal 

pushw %dx #Save the result 
pushw %ax #add a parameter for the next call 
call uint_inner 

pop %eax #to remove the top layer 
pop %eax #To extract the required layer 
addb $48, %al #To make it into a number symbol 
movb %al, (%ebx) #Add the number 

ret 

非常感謝您的幫助。 請記住,這是一個學校問題,所以試圖解釋,而不只是給工作代碼。

回答

0

divw產生16位商,如果結果不合適,則會得到異常。由於您除以10,如果高位字不爲零,則保證有溢出。順便說一下,divisor應宣佈爲.short

鑑於你明確的代碼爲32位,你應該只使用32位除法。在這種情況下,除數應該是.int,您應該將股息加載到eax。不要忘記零edx其中包含股息的前32位。

+0

我明白了。我認爲它會被忽略。感謝您的解釋清楚。最後一個問題,爲什麼我應該使用.short而不是.byte? – NoobsDeSroobs

+0

因爲'divw'使用16位除數和'.byte'只定義了它的8位,所以沒有告訴什麼前8位都將是。 – Jester

相關問題