2012-07-09 50 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; 

这是你应该在汇编实现代码。不要盲目做,想想你在做什么,为什么!

提示:您可以避免所有这些乘法和除法,只要仔细观察一下你把什么或者:)

相关问题