Assembly 汇编语言如何递增多位数十进制ASCII字符串?
所以我有这部分代码Assembly 汇编语言如何递增多位数十进制ASCII字符串?,assembly,x86,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
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您应该使用
inc
命令,例如:inc var
,但是我发现您在代码中使用该命令无效。如果inc
不适合您,还有adddestination,source
希望这会有所帮助。如果将所有与进位相关的内容都留给CPU处理,会简单得多,我建议将输入的数字完全转换为整数,递增,然后转换回字符串并输出。我想让您考虑一下,所以我将给您一个类似C的伪代码,如果您需要更多帮助,我将帮助您将其转换为汇编;) 这是您应该在程序集中实现的代码。不要盲目地去做,想想你在做什么,为什么
提示:您可以避免所有这些乘法和除法,只需仔细查看您的除法或乘法:)
aam
<代码>aad?天哪,8086,蝙蝠侠!在AX中将两个输入数字转换为0-9个整数后,只增加低位数字,而不从AL进位到AH进位。因此,您的代码将执行39
->30
,而不是40
。处理3位数的结果会导致一个单独且更难的问题。另外,jne OTHER
也没用,因为分支的两边(掉下来或被取下)都在同一个地方。另外,前4条指令可以是movax、[INPUT+2]
/xchgal,ah
。(或者更有效地说,rol ax,8
,除非您需要与8086向后兼容,8086没有立即旋转计数>1)如果输入保证只有2位(或者更简单,正好是2位),那么'99'
是唯一的特例,您可以检查一下。
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;