Jamie Ford
Jamie Ford

Reputation: 31

C to Assembly conversion problems

I'm trying to convert my C code into assembly without using gcc -S function since I want to practice assembly myself. For some reason I can't get my assembly file to match my c code and I can't figure out why. Here is what I've written, the C code is correct but the assembly code doesn't compile. This is sparc assembly btw.

#include <stdio.h>

int charCompare( const void *ptr1, const void *ptr2 )
{
    char i = *((char *)ptr1);
    char j = *((char *)ptr2);

    if (i > j)
        return (1);
    if (i < j)
        return (-1);
    return (0);
}

Here is the supposedly equivalent assembly code.

.global        charCompare      !makes the function globally usable
.section       ".text"

charCompare:
save    %sp, -96, %sp !save caller's window; if different than -96
cmp i1, i2     !compare i1 with i2
bg  returnpos !if i1 is greater than i2 jump to returntrue
cmp i1, i2     !compare i1 with i2
bl returnneg


returnneg:
set -1, %o0
ba end

returnpos:
set 1, %o0
ba end

end:
ret 
restore

Upvotes: 3

Views: 252

Answers (1)

Matthew Mellott
Matthew Mellott

Reputation: 176

Try using this command:

gcc -S -O0 myfile.c

That extra gcc option will turn off all optimizations (like ones that can rearrange your code).

Upvotes: 1

Related Questions