Pejman Moghadam / Assembly

Linux Assembly - Create a sum function for C

Public Domain


sum.asm

; nasm -f elf sum.asm -o sum.o
section .text
global sum
sum:
        push    ebp                ; function prologue
        mov     ebp, esp

        push    ebx                ; backup register

        mov     ebx, [ebp+8]       ; first parameter
        add     ebx, [ebp+12]      ; second parameter

        mov     eax, ebx           ; return value

        pop     ebx                ; restore register

        mov     esp,ebp            ; function epilogue
        pop     ebp
        ret

sum.c

/* gcc sum.c sum.o -o sum */

#include <stdio.h>

int sum(int a, int b);

int main()
{
        int a = 1575;
        int b = 3619;
        int result = sum(a, b);

        printf("%d + %d = %d\n", a, b, result);

        return(0);
}

BY: Pejman Moghadam
TAG: asm
DATE: 2013-01-12 19:35:55


Pejman Moghadam / Assembly [ TXT ]