汇编语言如何递增多位十进制ASCII字符串?

时间:2012-07-09 17:08:17

标签: assembly x86

所以我有这部分代码

    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 个答案:

答案 0 :(得分:1)

您应该使用inc命令,例如:inc var,但是我发现您在代码中使用了这个命令无济于事。如果inc不适合您,则还有add destination, source

希望有所帮助。

答案 1 :(得分: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;

这是您应该在程序集中实现的代码。不要盲目地做,想想你正在做什么以及为什么!

提示:您可以避免所有这些乘法和除法,只需仔细查看除以或乘以的内容:)