0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C言語 関数をアセンブリ言語で実装する方法

Last updated at Posted at 2025-05-01

主関数

main関数はC言語で実装

main.c
#include <stdio.h>

extern int add(int a, int b);

int main() {
    int result = add(3, 4);
    printf("Result: %d\n", result);
    return 0;
}

アセンブリ言語

add.asm
section .text
global _add       ; Windowsは下線が必要

_add:             
    push ebp       ; スタック前処理
    mov ebp, esp

    mov eax, [ebp + 8]   ; 第1引数 a
    add eax, [ebp + 12]  ; 第2引数 b=a+b

    mov esp, ebp   ; スタック後処理
    pop ebp
    ret

動作結果

nasm -f win32 add.asm -o add.obj
gcc -m32 main.c add.obj -o main.exe

image.png

動作環境

windows7 Version6.1.7601
GCC-6.3.0-1
NASM version 2.11.08 compiled on Feb 21 2015
(帰省中につき一時的に昔のパソコンを使用している。windows7が懐かしい。。。)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?