3

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); } 
3
  • 1
    Try compiling with gcc -m32 -S file.c to see what comes out. You'll notice your code has a syntax error, and a void function returning a value. Commented Nov 17, 2014 at 2:10
  • You're right. The assembly had %eax so I assumed I would put a return value but the "void someOperation" was given to us to begin the C code. Exclude return then I guess? Commented Nov 18, 2014 at 3:08
  • 1
    Yes, remove the return, and use a do-while loop. (The assembly code will mess up if the count passed in is 1. Your function should, too.) Commented Nov 18, 2014 at 3:59

1 Answer 1

2

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.

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

5 Comments

Garr, I think the argument order depends on AT&T vs Intel syntax. This code looks like AT&T syntax. But there are definitely a lot of things wrong with that function. It could very easily loop out of control if count was 0 or 1. It appears to check one too few elements, if count is the number to check. Also it clobbers ebx and esi, which should be preserved.
you may be right about the operand ordering, but I don't agree that ESI and EBX should be preserved. That is generally not the case. And it does check the correct number of elements because it checks when count==i, due to the inc coming before test.
but for sure, if count < 2 you'd have a problem.
You're right it does check the right amount of elements. It was just confusing me because it seemed so wrong. The reason I think ebx and esi should be preserved is because I'm used to the cdecl convention. Wikipedia article on x86 calling conventions. What calling convention are you used to?
@Garr: AFAIK, ESI and EBX must be preserved in most compilers. The free registers are EAX, EXC and EDX. And this is AT&T syntax, so the leftmost operand moves into the rightmost.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.