简体   繁体   中英

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. 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. So much for the theory. But the line add eax, byte[ebx] brings the error "mismatch in operand sizes". 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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