가장 간단히 Assembly 언어를 만드는 방법은 다음과 같이 C언어에서 Assembly 언어를 추출해 내는 방법이다.
다음은 hello.c 이다.
#include <stdio.h>
void main()
{
printf("Hello World\n");
}
다음은 C 소스에서 Assembler 소스를 얻는 방법이다.
% gcc -S hello.c
다음은 이렇게 해서 얻는 Assembler 소스이다.
.file 1 "hello.c"
.version "01.01"
.set noat
gcc2_compiled.:
__gnu_compiled_c:
.section .rodata
.align 3
$C32:
.ascii "Hello World\12\0"
.text
.align 3
.globl main
.ent main
main:
ldgp $29,0($27)
main..ng:
lda $30,-16($30)
.frame $15,16,$26,0
stq $26,0($30)
stq $15,8($30)
.mask 0x4008000,-16
bis $30,$30,$15
.prologue 1
lda $16,$C32
jsr $26,printf
ldgp $29,0($26)
$33:
bis $15,$15,$30
ldq $26,0($30)
ldq $15,8($30)
addq $30,16,$30
ret $31,($26),1
.end main
.ident "GCC: (GNU) 2.7.2.1"
그러면 hello.s라는 assembly 파일이 만들어 지는데, 이것을 GNU Assembler인 as로 Object 파일을 만든다.
% as -o hello.obj hello.s
이제 Loader(ld)로 라이브러리를 합쳐 실행 파일을 만들어 주면 된다. 이때 gcc로 컴파일해주면 자동으로 모든것을 알아서 해준다.
% gcc -o hello hello.o
Assembly 코드 작성
가장 간단히 Assembly 언어를 만드는 방법은 다음과 같이 C언어에서 Assembly 언어를 추출해 내는 방법이다.
다음은 hello.c 이다.
#include <stdio.h>
void main()
{
printf("Hello World\n");
}
다음은 C 소스에서 Assembler 소스를 얻는 방법이다.
% gcc -S hello.c
다음은 이렇게 해서 얻는 Assembler 소스이다.
.file 1 "hello.c"
.version "01.01"
.set noat
gcc2_compiled.:
__gnu_compiled_c:
.section .rodata
.align 3
$C32:
.ascii "Hello World\12\0"
.text
.align 3
.globl main
.ent main
main:
ldgp $29,0($27)
main..ng:
lda $30,-16($30)
.frame $15,16,$26,0
stq $26,0($30)
stq $15,8($30)
.mask 0x4008000,-16
bis $30,$30,$15
.prologue 1
lda $16,$C32
jsr $26,printf
ldgp $29,0($26)
$33:
bis $15,$15,$30
ldq $26,0($30)
ldq $15,8($30)
addq $30,16,$30
ret $31,($26),1
.end main
.ident "GCC: (GNU) 2.7.2.1"
GAS로 컴파일 하기
그러면 hello.s라는 assembly 파일이 만들어 지는데, 이것을 GNU Assembler인 as로 Object 파일을 만든다.
% as -o hello.obj hello.s
실행 파일 만들기
이제 Loader(ld)로 라이브러리를 합쳐 실행 파일을 만들어 주면 된다. 이때 gcc로 컴파일해주면 자동으로 모든것을 알아서 해준다.
% gcc -o hello hello.o