简体   繁体   中英

Translating Assembly to C

I'm reviewing a practice midterm at the moment. the question gives a piece of assembly code (IA32) and instructs to write the C equivalent of it. Just want to make sure I'm doing it correctly. Thanks!

Assembly program given:

.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've written:

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);
 }

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

EDIT: also, your assignment is wrong. MOV instruction copies from the 2nd parameter to the first. You have it going the other way in your C code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM