0

I'm trying to convert the following C++ code into Assembly (MASM, Irvine32):

const int SIZE = 10;

int numbers[SIZE] = {10,60,20,33,72,89,45,65,72,18};
int limit = 50;
int index = 0;
int sum = 0;

while( index < SIZE )
{
    if( numbers[index] <= limit )
    {
        sum = sum + numbers[index];         // sum += array[index];
    }
    index++;
}

If anyone could clear up where I'm going wrong -- I'm getting errors at L1: it's just spouting out "+10". I believe it's because I cannot translate sum=sum+numbers[index] into Assembly. If anyone could help me do that it would be fantastic. My attempt at translating it (lines starting from "total: mov esi, offset numbers" to "inc index") is obviously incorrect.

.data
SYZ = 10
numbers DWORD 10, 60, 20, 33, 72, 89, 45, 65, 72, 18
limit DWORD 50
index DWORD 0
sum DWORD 0

.code
main PROC
mov eax, index
mov ebx, SYZ
top: cmp eax, ebx
jae next
jb total

total: mov esi, OFFSET numbers
mov ecx, limit

cmp [esi], ecx

jbe L1

L1: add eax, ebx

inc index

jmp top

next: mov edx, sum

call WriteInt



exit
main ENDP
END main
2
  • 1
    Kind of unrelated to the actual question, but I don't think the "jb total" instruction is necessary, if "jae next" doesn't jump it will fall through to the next instruction (which has the label total) anyways, meaning it's a redundant check. Commented May 20, 2020 at 0:13
  • thank you for that check, you make sense it's redundant Commented May 20, 2020 at 0:15

2 Answers 2

2

Your conditional branch that implements the if is wrong. It should look like:

top:
...
    cmp [esi], ecx
    ja L1               ; conditional jump *over* an ADD instruction
    add eax, [esi]      ; [esi] is array[index] if you increment ESI properly...
L1: inc index
    jmp top

In your C++, you can see that if numbers[index] <= limit then you want to update the sum, otherwise just increment the index and go back to the "top"; aka recheck the stopping condition.

Your original asm code was doing a condition check and then continuing regardless of the result.

    cmp [esi], ecx
    jbe L1               ; jump or fall-through to L1, condition irrelevant
L1: add eax, ebx

The C++ equivalent of your original asm is:

if( numbers[index] <= limit )
{
}

    sum += ebx;
    index++;

I'm not sure if this will solve all of your problems, but it will definitely solve one of them.

Sign up to request clarification or add additional context in comments.

4 Comments

Your proposed new fragment of the loop still doesn't add another memory access to the loop besides the cmp, so it's not accessing numbers[index]. From what I can tell from the mess in the OP's loop, it's accessing numbers[0] every iteration. You've correctly identified on problem, which is that the conditional branch needs to jump over something, not just to the next instruction whether it falls through or is taken, but add eax, ebx certainly doesn't implement sum += numbers[index]; because nothing puts the array value into EBX.
Also it's not otherwise increment the index. You always increment the index regardless of the branch condition (add esi, 4 to move the pointer). Perhaps you meant "otherwise just increment the index (without doing anything else)".
Oh, I missed the last line of your answer where you say this might not be a complete solution. Sorry, didn't notice that until after downvoting. Saying "try this" is usually a bad way to start an answer, especially when it doesn't actually solve all the problems, so that gave me a bad impression of this answer before I got to the part where you eventually explain why.
I made an edit to make your answer more readable, IMO (and so I could remove my hastily cast downvote). Anyway yeah, this is just one of multiple showstopper problems in the code in the question; I added a hint at another one in my edit to your first code block.
0
.data
SYZ = 10
numbers DWORD 10, 60, 20, 33, 72, 89, 45, 65, 72, 18
limit DWORD 50
index DWORD 0
sum DWORD 0

.code
main PROC
mov eax, index
mov ebx, SYZ
mov esi, OFFSET numbers
mov ecx, limit
mov edx, 0

top: cmp eax, ebx
jae next
cmp [esi], ecx
ja L1
add edx, [esi]
L1: inc index
mov eax, index
add esi, 4
jmp top

next: mov sum, edx
      mov eax, sum
      call WriteInt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.