Maciej L.
Maciej L.

Reputation: 9

Editing ASM result of an operation in C when compiling in GCC

me and my friend got a computer architecture project and we don't really know how to get to it. I hope you could at least point us in the right direction so we know what to look for. As our professor isn't really good at explaining what we really need to do and the subject is rather vague we'll start from the beginning.

Our task is to somehow "edit" GCC to treat some operations differently. For example when you add two char arguments in a .c program it uses addb. We need to change it to f.e. 16bit registers(addl), without using unnecessary parameters during compilation(just regular gcc p.c -o p). Why or will it work doesn't really matter at this point. We'd like to know how we could change something inside GCC, where we can even start looking as I can't find any information about similar tasks besides making plugins/extensions. Is there anything we could read about something like this or anything we could use?

Upvotes: 0

Views: 103

Answers (1)

user3710044
user3710044

Reputation: 2334

In C 'char' variables are normally added together as integers so the C compiler will already use addl. Except when it can see that it makes no difference to the result to use a smaller or faster form.

For example this C code

unsigned char a, b, c;
int i;

void func1(void) { a = b + c; }
void func2(void) { i = b + c; }

Gives this assembler for GCC.

    .file   "xq.c"
    .text
    .p2align 4,,15
    .globl  func1
    .type   func1, @function
func1:
    movzbl  c, %eax
    addb    b, %al
    movb    %al, a
    ret
    .size   func1, .-func1
    .p2align 4,,15
    .globl  func2
    .type   func2, @function
func2:
    movzbl  b, %edx
    movzbl  c, %eax
    addl    %edx, %eax
    movl    %eax, i
    ret
    .size   func2, .-func2
    .comm   i,4,4
    .comm   c,1,4
    .comm   b,1,4
    .comm   a,1,4
    .ident  "GCC: (Debian 4.7.2-5) 4.7.2"
    .section        .note.GNU-stack,"",@progbits

Note that the first function uses addb but the second uses addl because the high bits of the result will be discarded in the first function when the result is stored.

This version of GCC is generating i686 code so the integers are 32bit (addl) depending on exactly what you want you may need to make the result a short or actually get a compiler version that outputs 16bit 8086 code.

Upvotes: 2

Related Questions