I have the following assembly line(I also added a C code implementation how I would do it in C):
.................
A: MOVSX EAX, BYTE PTR DS:[ECX] | while(ecx != NULL){
ADD EDX, EAX |
INC ECX | char *eaxPtr = *ecx;
CMP BYTE PTR DS:[ECX], 0x0 | int edxValue += *(int *)eaxPtr;
JNZ SHORT A | ecx++;
ADD EDX,0xB |
MOV EAX,EDX | }
SUB EAX,0xB | int eaxValue = edxValue;
RETN |
The thing is that I have stored the string "30;,;1,73,:2" in the register ECX. What the routine above does is to go through the string and add the components of the string like 30 or the ANSCII representation of for example ; and so on in the register EDX. As result, I have the value 00000264 in EDX. What I do not understand is the end of the assembly line. Especially the lines:
ADD EDX,0xB
MOV EAX,EDX
SUB EAX,0xB
Why we first add 0xB to the register edx, then store it to eax, and then subtract 0xB from eax ? The whole is equivalent to the line:
MOV EAX,EDX
Does somebody know why the compiler do it like that ? Because of optimization ? Or there any other reasons?
By the way: Is my C code implementation correct ?