2012-07-09 48 views
0

所以我的代碼彙編語言如何增加多位十進制ASCII字符串?

mov SI, 0002 
    mov ah, INPUT[SI] 
    INC SI 
    mov al, INPUT[SI] 
    sub AX, 3030h 
    aad 
    inc al 
    cmp byte ptr INPUT[0002], 39h 
    jne OTHER 



OTHER: aam 
     add ax, 3030h 
     mov INPUT[0003], al 
     mov INPUT[0002], ah 

其中輸入是所述用戶輸入的這個部分。 這個代碼做的是增加一個2位數字, 我的問題,當一個三位數字要增加。

實施例: 輸入:98 輸出:99

輸入:99 輸出:110

期望的結果: 輸入:99 輸出:100

+2

乘這是嗯,功課? – 2012-07-09 17:13:43

+2

'aam'? 'aad'?聖8086,蝙蝠俠! – 2012-07-09 18:20:37

+0

將兩個輸入數字轉換爲AX中的0-9整數後,您只增加低位數字,而不從AL進位到AH。所以你的代碼會執行'39' - >'30'而不是'40'。處理3位數的結果是一個單獨的,更難的問題。另外,'jne OTHER'是無用的,因爲分支的兩邊(落後或被佔用)是相同的地方。另外,前4條指令可以是'mov ax,[INPUT + 2]''''xchg al,ah'。 (或者更有效地說,'rol ax,8',除非你需要向後兼容8086,它不會立即旋轉且計數> 1) – 2017-07-12 16:28:07

回答

1

您應使用inc命令,例如:inc var,但是我看到你已經在你的代碼中使用了這個功能無濟於事。如果inc不適合你,還有add destination, source

希望有幫助。

0

如果將所有與進位有關的東西留給CPU,我建議將輸入數字完全轉換爲整數,遞增,然後再轉換回字符串並輸出,這會簡單得多。我希望你想想這個,所以我只會給你一個C類僞代碼,並幫助您將其轉換爲組件,如果你需要更多的幫助;)

int nInput = 0; 

// Converting to decimal 
if(input[ 0 ] > '9') input[ 0 ] -= 'a' + 10; 
else input[ 0 ] -= '0' 
nInput += input[ 0 ]; 

if(input[ 1 ] > '9') input[ 1 ] -= 'a' + 10; 
else input[ 1 ] -= '0' 
nInput += input[ 1 ] * 16; 

if(input[ 2 ] > '9') input[ 2 ] -= 'a' + 10; 
else input[ 2 ] -= '0' 
nInput += input[ 2 ] * 256; 

if(input[ 3 ] > '9') input[ 3 ] -= 'a' + 10; 
else input[ 3 ] -= '0' 
nInput += input[ 3 ] * 4096; 

// Incrementing :) 
nInput += 1; 

// Converting back to string 
char output[ 5 ]; 

int digit = nInput & 15; 
if(digit > 9) digit += 'a' + 10; 
else digit += '0'; 
output[0] = digit; 

digit = (nInput & 255)/16; 
if(digit > 9) digit += 'a' + 10; 
else digit += '0'; 
output[1] = digit; 

digit = (nInput & 4095)/256 
if(digit > 9) digit += 'a' + 10; 
else digit += '0'; 
output[2] = digit; 

digit = (nInput & 65535)/4096; 
if(digit > 9) digit += 'a' + 10; 
else digit += '0'; 
output[3] = digit; 

output[4] = 0; 

這是你應該在彙編實現代碼。不要盲目做,想想你在做什麼,爲什麼!

提示:您可以避免所有這些乘法和除法,只要仔細觀察一下你把什麼或者:)

相關問題