#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int num1 = 5;
int num2 = 7;
int result;
result = add(num1, num2);
printf("The sum is: %d\n", result);
return 0;
}
section .data
format db "The sum is: %d", 10, 0
section .text
global main
extern printf
main:
push ebp
mov ebp, esp
sub esp, 8 ; allocate space for local variables
; Initialize local variables
mov dword [ebp-4], 5 ; num1
mov dword [ebp-8], 7 ; num2
; Call the add function
mov eax, [ebp-4]
mov ebx, [ebp-8]
push ebx
push eax
call add
add esp, 8 ; cleanup the stack
; Store the result in the 'result' variable
mov dword [ebp-12], eax ; result
; Prepare arguments and call printf
push dword [ebp-12]
push format
call printf
add esp, 8 ; cleanup the stack
; Cleanup and exit
mov esp, ebp
pop ebp
ret
section .text
global add
add:
push ebp
mov ebp, esp
; Function prologue
; Get function parameters
mov eax, [ebp+8] ; a
mov ebx, [ebp+12] ; b
; Add the parameters
add eax, ebx
; Function epilogue
mov esp, ebp
pop ebp
ret