繁体   English   中英

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

[英]Assembly language how to increment multi-digit decimal ASCII strings?

所以我有这部分代码

    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位数字,这是我的问题,当要增加3位数字时。

示例:输入:98输出:99

输入:99输出:110

期望的结果:输入:99输出:100

您应该使用inc命令,例如: inc var ,但是我发现您在代码中使用了此命令无济于事。 如果inc对您不起作用,那么还会add destination, source

希望能有所帮助。

如果将所有与进位相关的内容留给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;

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM