Translating an assembly to C

I am currently reviewing the practice. the question gives some of the assembly code (IA32) and instructs to write the C equivalent. Just want to make sure I'm doing it right. Thank!

Specified build program:

.global _someOperation
_someOperation:
    pushl %ebp
    movl %esp, %ebp
    movl 8(%ebp), %ebx
    movl 12(%ebp), %edx
    decl %edx
    xorl %esi, %esi
    movl (%ebx, %esi, 4), %eax
continue:
    incl %esi
    cmpl (%ebx, %esi, 4), %eax
    jl thelabel
    movl (%ebx, %esi, 4), %eax
thelabel:
    cmp %esi, %edx
    jne continue
    movl %ebp, %esp
    popl %ebp
    ret

      

This is the code I wrote:

void someOperation(int *num, int count)  //Given
{
    int k; //Given
    count--;
    int i = 0;
    k = num[i];
    i++;
    while(count != i)
    {
        if(k >= num[i]
            k = num[i];
        i++;
    }
    return (k);
 }

      

+3


source to share


1 answer


Looks pretty close to me, although in ASM the increment is only at the beginning of the loop and the condition is not checked the first time. Consider DO ... WHILE instead.



EDIT: also, your assignment is wrong. The MOV command copies from the second parameter to the first. You have a different path in C code.

+2


source







All Articles