• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

what does this C code do?

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
eternity5219
Asked:
eternity5219
  • 4
  • 3
1 Solution
 
Infinity08Commented:
>> 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
 
DiscoNovaCommented:
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
 
Infinity08Commented:
>> 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
eternity5219Author Commented:
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
 
Infinity08Commented:
>> 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
 
eternity5219Author Commented:
if first number is greater than the second number go to the line fun6+30.
right?
0
 
Infinity08Commented:
>> 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
 
eternity5219Author Commented:
thanx infinity, I find the value I needed.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now