0

The following is pseudo-code:

int array_list[] = {0, 11, 13, 18, 21, 23, 24, 17, 45}; 
int array_size = sizeof array_list/sizeof sample; 
int index = 0; // index for while loop 
int sum = 0;   // accumulate the result 
for (current_size = array_size ; current_size > 0; current_size--){ 
    while (index<current_size){
        if(array_list[index]is even){
           sum += array_list[index];
        }
        index += 1;
     }
}

My work is:

.data
sum DWORD 0
sample 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
mov edx,sample
mov esi,0
mov ecx,array_size


L1:
cmp esi,ecx
jl L2
jmp L5

L2:
cmp array_list[esi*4], edx
jg L3
jmp L4

L3:
add eax,array_list[esi*4]

L4:
inc esi
jmp L1

L5:
mov sum,eax

I'm confusing how to do for_while loops in .asm file. My question is how can I code

for (current_size = array_size ; current_size > 0; current_size--){

to my code?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
terry
  • 1
  • 2
  • What is your specific question? Please see [What questions are allowed](https://stackoverflow.com/help/on-topic). You are unlikely to get meaningful help if you ask people to do your work for you. – Casey Nov 14 '20 at 04:47
  • I just edited the question. – terry Nov 14 '20 at 05:12
  • Does this have something to do with MonetDB or is the tag a mistake? – Nate Eldredge Nov 14 '20 at 05:12
  • I can't answer the question, as I am unfamiliar with this particular asm language (can you blame me?), but, I can help you think about your problem better. Writing an "answer" right now. –  Nov 14 '20 at 05:16
  • It is assembly language. – terry Nov 14 '20 at 05:27
  • 1
    @terry There are multiple Asm instructions sets, each with their own differences and limitations. There is no all-encompassing, generic "assembly language." I personally recommend just looking at C compilers' emitted instructions. –  Nov 14 '20 at 05:36
  • @xxh I mean like the code need to be .asm file. not C language. I'm using visual studio 2019 – terry Nov 14 '20 at 05:38
  • Actually, I've decided to back off, sorry, without knowing the specific asm, I can't be of too much help. –  Nov 14 '20 at 05:48
  • 2
    It looks like this is x86 assembly language. It turns out that there's a program that converts C to assembly language. You can just [ask it to do the conversion for you](https://gcc.godbolt.org/z/6T59Md). – Raymond Chen Nov 14 '20 at 05:49
  • Like I had previously recommended, just using a compiler would be the easiest way to understand how C source maps to assembly languages. It seems that @RaymondChen agrees. –  Nov 14 '20 at 05:51
  • 2
    at L1 and L2 you demonstrate you understand how to do a conditional branch....a while loop no less, so what is the question? how is a for loop any different than a while loop? that while loop in particular? – old_timer Nov 14 '20 at 10:45

0 Answers0