Solved

# what does this C code do?

Posted on 2007-10-08
253 Views
Last Modified: 2010-04-15
I think this C code tries to have the numbers in a linked list in order, from largest to smallest.
plz let me know if I am correct.

Node* fun6(Node *node)
{
Node *p, *q, *r;

p = node->next;

node->next = 0;

while (p) {
r = q = node;

while (r && r->value > p->value) {
q = r;
r = r->next;
}

if (r == q)
node = p;
else
q->next = p;

q = p->next;
p->next = r;
p = q;
}

return node;
}
0
Question by:eternity5219
8 Comments

LVL 53

Expert Comment

>> I think this C code tries to have the numbers in a linked list in order, from largest to smallest.

You are very close ... Just take a closer look at the ordering ... ;)
0

LVL 7

Expert Comment

Even though my ability to understand even the easiest snippets of C code is very limited, I'd say that your assumption is correct. The code appears to be going through a list of nodes and swapping their positions based on their values. I believe the algorithm used is bubble sorting, but as I said, my C vocabulary is very limited.
0

LVL 53

Expert Comment

>> You are very close ... Just take a closer look at the ordering ... ;)

Oops ignore that ... I misread what you said ... it is indeed from largest to smallest heh. For some reason my brain swapped the two words around ;)
0

Author Comment

Infinity, If you know the answer pls tell me,
I ve wrote down this C code from the equivalent assembly code I had.
and Belive me I really cant look any closer to it.
this is the assembly code for the C code I wrote.
which I hope is correct.
but all I need to know is that what does this C code exactly do.
and all of this code is just one function in phase 6 of another big assembly program. which I have already solved 5 phase of it and this is the last phase.
I am telling you all these things to just let you know that I ve been working on it a whole week, and I am so screwed with it.
Dump of assembler code for function fun6:
0x08048c61 <fun6+0>: push %ebp
0x08048c62 <fun6+1>: mov %esp,%ebp
0x08048c64 <fun6+3>: push %esi
0x08048c65 <fun6+4>: push %ebx
0x08048c66 <fun6+5>: mov 0x8(%ebp),%ecx
0x08048c69 <fun6+8>: mov 0x8(%ecx),%ebx
0x08048c6c <fun6+11>: movl \$0x0,0x8(%ecx)
0x08048c73 <fun6+18>: mov %ecx,%esi
0x08048c75 <fun6+20>: mov %ecx,%eax
0x08048c77 <fun6+22>: mov %ecx,%edx
0x08048c79 <fun6+24>: test %ebx,%ebx
0x08048c7b <fun6+26>: jne 0x8048ca7 <fun6+70>
0x08048c7d <fun6+28>: jmp 0x8048cb3 <fun6+82>
0x08048c7f <fun6+30>: mov %edx,%eax
0x08048c81 <fun6+32>: mov 0x8(%edx),%edx
0x08048c84 <fun6+35>: test %edx,%edx
0x08048c86 <fun6+37>: je 0x8048c8c <fun6+43>
0x08048c88 <fun6+39>: cmp %ecx,(%edx)
0x08048c8a <fun6+41>: jg 0x8048c7f <fun6+30>
0x08048c8c <fun6+43>: cmp %edx,%eax
0x08048c8e <fun6+45>: jne 0x8048c94 <fun6+51>
0x08048c90 <fun6+47>: mov %ebx,%esi
0x08048c92 <fun6+49>: jmp 0x8048c97 <fun6+54>
0x08048c94 <fun6+51>: mov %ebx,0x8(%eax)
0x08048c97 <fun6+54>: mov 0x8(%ebx),%eax
0x08048c9a <fun6+57>: mov %edx,0x8(%ebx)
0x08048c9d <fun6+60>: mov %eax,%ebx
0x08048c9f <fun6+62>: mov %esi,%edx
0x08048ca1 <fun6+64>: mov %esi,%eax
0x08048ca3 <fun6+66>: test %ebx,%ebx
0x08048ca5 <fun6+68>: je 0x8048cb3 <fun6+82>
0x08048ca7 <fun6+70>: test %edx,%edx
0x08048ca9 <fun6+72>: je 0x8048c8c <fun6+43>
0x08048cab <fun6+74>: mov (%ebx),%ecx
0x08048cad <fun6+76>: cmp %ecx,(%edx)
0x08048caf <fun6+78>: jg 0x8048c7f <fun6+30>
0x08048cb1 <fun6+80>: jmp 0x8048c8c <fun6+43>
---Type <return> to continue, or q <return> to quit---
0x08048cb3 <fun6+82>: mov %esi,%eax
0x08048cb5 <fun6+84>: pop %ebx
0x08048cb6 <fun6+85>: pop %esi
0x08048cb7 <fun6+86>: pop %ebp
0x08048cb8 <fun6+87>: ret
End of assembler dump.
0

LVL 53

Expert Comment

>> Infinity, If you know the answer pls tell me,

Keeping my earlier mix-up in mind ... you have interpreted the C code entirely correctly.

However, I noticed one small translation problem from assembler to C code. Take a closer look at these two lines, and the way you interpreted them :

0x08048c88 <fun6+39>: cmp %ecx,(%edx)
0x08048c8a <fun6+41>: jg 0x8048c7f <fun6+30>

and the same here :

0x08048cad <fun6+76>: cmp %ecx,(%edx)
0x08048caf <fun6+78>: jg 0x8048c7f <fun6+30>

Think about how the combination of cmp and jg works.
0

Author Comment

if first number is greater than the second number go to the line fun6+30.
right?
0

LVL 53

Accepted Solution

>> if first number is greater than the second number go to the line fun6+30.

Correct. That statement decides the ordering ...

Now, compare that to how you translated that in C code :

while (r && r->value > p->value) {

r->value corresponds to (%edx) and p->value to %ecx.
0

Author Comment

thanx infinity, I find the value I needed.
0

## Join & Write a Comment Already a member? Login.

### Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

#### 754 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!