Can help? I totally cannot do assembly code. It's part of my lab tutorial.

I wanted to convert this C++ code into assembly:

for (a = x; a <= y; a++) {
prime = 1;
for (b = 2; b <= a / 2; b++) {
if (a % b == 0) {
prime = 0;
break;
}
}
if (prime == 1)
count++;
}

x is the first number, y is the second number. This will find the value of prime numbers between the 2 given numbers. (eg. if x = 4, y = 25, the value of prime numbers between the 2 given numbers is 7)

And what I end up having this:

mov eax, x // a = x
cmp ebx, y // a <= y
jg end // stop if x > y

mov edx, 1 // prime = 1
push edx

mov ecx, 2 // b = 2
div eax // a / 2
cmp ecx, eax // b <= a / 2
inc ecx // b++

div eax, ecx
mov edh, eah

cmp edh, 0
mov edx, 0

Recommended Answers

All 2 Replies

It's already a week, I totally cannot do.

push ebp ;Prepare the stack for "local" variables
mov ebp, esp
mov dword ptr ds:[ebp-4], 10 ;Store the x value
mov dword ptr ds:[ebp-8], 90 ;Store the y value
mov dword ptr ds:[ebp-10], 2 ;Store the initial b value
xor esi, esi ;ESI = count
@1:
mov eax, dword ptr ds:[ebp-4]
cmp eax, dword ptr ds:[ebp-8]
jge end ;x <= y
mov edi, 1 ;EDI = prime
@6:
mov ebx, dword ptr ds:[ebp-10]
mov ecx, 2
div ecx
cmp ebx, eax
jge @3
mov eax, dword ptr ds:[ebp-4]
mov ecx, dword ptr ds:[ebp-10]
div ecx
cmp edx, 0
je @4
inc dword ptr ds:[ebp-10]
jmp @6
@4:
mov edi, 0
@3:
cmp edi, 0
jne @5
inc esi
@5:
inc dword ptr ds:[ebp-4]
jmp @1
end:
mov esp, ebp ;Return to the original stack
pop ebp ;Restore old ebp

I believe this should work(didn't test it), any bug tell me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.