MASM字符串中的Ascii总和

时间:2013-01-17 14:02:33

标签: assembly ascii masm irvine32

INCLUDE Irvine32.inc
.data
fullName BYTE "Bob Johnson",0
nameSize = ($ - fullName) - 2
.code
main PROC
    mov ECX,nameSize
    mov ESI,OFFSET fullName
Sum:
    mov EBX,[ESI+ECX]
    add EAX,EBX
    loop Sum
    exit
main ENDP
END main

所以我遇到了一个问题我只想一次从字符串中读取一个字符并将其存储在EBX寄存器中,然后获取该字符的值并在EAX中保持运行总和。

使用8位无符号算术将字符串字符的所有ASCII代码加在一起。溢出被忽略。 最终值是校验和。例如,如果字符串是“Joe”,则ASCII值为4A,6F,65。总和为11E。

1 个答案:

答案 0 :(得分:0)

INCLUDE Irvine32.inc
.data

fullName BYTE "Bob Johnson",0       ; String storing name
nameSize = ($ - fullName)   ; Variable storing length of name

.code
main PROC

    mov ECX,nameSize        ; Set counter for loop
    mov ESI,OFFSET fullName ; Set pointer at fullName variable
    mov EAX,0               ; Clear the EAX register
    mov EBX,0               ; Clear the EBX register

Sum:                        ; Loop 

    mov bl,[ESI + ECX - 1]  ; Use the bl (8 bit register) to point at characters in the string.
    add EAX,EBX         ; Add the two registers together

    loop Sum                ; Loop  

    call DumpRegs           ; Display results

    exit
main ENDP

END main

解决了问题!!