After it got converted, store the answer in the sum variable
So I have to convert it into x86 assembly language, and this is what I got so far
.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode: DWORD
.data
sum DWORD 0
array_list DWORD 10,11,13,18,21,23,24,17,45
array_size = ($ - array_list) / TYPE array_list
.code
main PROC
mov eax, 0 ; sum
mov esi, 0 ; index
mov ecx, array_size
L1:
dec ecx
cmp esi, ecx
jl L2
jmp L5
L2:
cmp esi, ecx
jl L3
jmp L4
L3:
cmp array_list[esi], array_list[esi%2]
add eax, array_list[esi]
jmp L4
L4:
add esi, 1
jmp L1
L5:
mov sum, eax
INVOKE ExitProcess, 0
main ENDP
END main
For the array_size, I was trying to make sure that it is 40/4 = 10 I do not understand or know how to the for loop, so the first loop must be wrong what I wrote there. Also, how do you do the if, where it said array_list[index] is even. Do I also need to declare the sample? Cuz it is used for the array_size. I really need help because I did not understand :(
CodePudding user response:
The basic pattern equivalence for a for-loop is as follows:
C for-loop
for ( init; test; incr ) {
<loop-body>
}
C while-loop
init;
while ( test ) {
<loop-body>
incr;
}
That first loop in the assembly is following some different basic form: it has relocated the incr;
portion in relation to the <loop-body>
.
Surely you can see that in comparison with the following pattern:
init;
while ( test ) {
incr;
<loop-body>
}
that <loop-body>
will see a different value for current_size
each iteration (off by 1 increment, here -1) than in the C code, so it won't run the same as the C code, if that variable is ever consulted in the <loop-body>
.
The test
condition to continue the outer for in the assembly doesn't reflect the > 0 in the C code.
The % in the assembly code is doing array[index%2] whereas in the C code it is doing array[index]%2, which are clearly different concepts, so that wouldn't run the same (even if it were allowed like that).
Of course, x86 cannot do that compare instruction, because of the dual memory references. x86 requires one operand in register in order for the other to be a memory operand.
But that C code doesn't even require two array references there. Just one and than an even test on its value.
The C code requires an if-then which is missing in the assembly.