简体   繁体   English

如何在汇编器中计算ASCII字符串的数字总和

[英]How to calculate the digit sum of an ascii string in assembler

I am trying to calculate the digit sum of an ascii string with nasm. 我正在尝试使用nasm计算ascii字符串的数字总和。 In order to do so I tried to iterate over the single bytes and accumulate the value in eax till I am reaching the null byte. 为了做到这一点,我尝试遍历单个字节并在eax中累积值,直到到达空字节为止。 So much for the theory. 理论上就这么多。 But the line add eax, byte[ebx] brings the error "mismatch in operand sizes". 但是行add eax, byte[ebx]会带来错误“操作数大小不匹配”。 How can accumulate operands with different sizes? 如何累积不同大小的操作数?

Here's the code 这是代码

mov eax, 0
mov ebx, userInput; "abc"

readChar:
    cmp byte[ebx],0
    jz finished
    add eax, byte[ebx]
    inc ebx
    jmp readChar

thanks for your help. 谢谢你的帮助。

Short answer: you can't in one instruction. 简短的答案:您不能在一个指令中。

You need to grab the byte value, zero-extend it, then add that. 您需要获取字节值,将其零扩展,然后添加。 For example: 例如:

movzx ecx, byte [ebx]
add   eax, ecx

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

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