![article thumbnail image](https://blog.kakaocdn.net/dn/cWwPfu/btqX6glzE9i/JADGdhZQN6ed2zqks7GkLk/img.png)
실행 환경
- cpu : intel(32bit)
- 컴파일러 : nasm
- 리눅스 : ubuntu 20.04 LTS
설치방법
- sudo apt-get install build-essential gcc-multiplib nasm
mul-test.c
#include <stdio.h>
void mul(int n)
{
for (int i = 1; i < = 9; i++)
printf("%d * %d = %d \n", n, i, n * i);
return;
}
int main(void)
{
int num_array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
for (int i = 1; i < 9; i++)
mul(num_array[i]);
return 0;
}
mul-test.asm
extern printf
section .data
msg db "%d * %d = %d", 0x0A
section .text
global main
mul_func:
push ebp
mov ebp,esp
sub esp,4 ; i(지역변수 공간 확보)
mov DWORD [ebp-4], 1 ; i = 1
jmp S2 ; for loop starts
S1:
mov eax, DWORD [ebp-4]
mul DWORD [ebp+8]
push eax; n*i
push DWORD [ebp-4]; i
push DWORD [ebp+8]; n
push msg; "%d * %d = %d \n"
call printf ; printf();
add esp, 16 ; clear arguments of printf
add DWORD [ebp-4], 1 ; i++
S2:
cmp DWORD [ebp-4], 0x9 ; compare i and 9
jle S1 ; jump if less or equal
leave ; function epilogue starts
ret ; function epilogue ends
main:
push ebp
mov ebp,esp
sub esp,44 ; int num_array
mov DWORD [ebp-4],0
mov DWORD [ebp-8],9
mov DWORD [ebp-12],8
mov DWORD [ebp-16],7
mov DWORD [ebp-20],6
mov DWORD [ebp-24],5
mov DWORD [ebp-28],4
mov DWORD [ebp-32],3
mov DWORD [ebp-36],2
mov DWORD [ebp-40],1 ; int num_array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
mov DWORD [ebp-44],1 ; i = 1
jmp L2
L1:
mov eax, [ebp-44] ; i
push DWORD [ebp-44+eax*4] ; send argument : num_array[i] char * ptr; (array+ptr*4)
call mul_func ; mul_func(int n);
add esp, 4 ; clear arguments of mul_func
add DWORD [ebp-44], 1 ; i++
L2:
cmp DWORD [ebp-44], 0x9 ; compare i and 9
jl L1 ; jump if less
leave ; function epilogue starts
ret ; function epilogue ends
-
__cdecl 함수 호출 규약을 따름.
-
printf 함수를 extern하여 사용함. -> gcc로 컴파일 하였다.
'Programming Languages > Assembly' 카테고리의 다른 글
[Assembly] 어셈블리 Hello world! 100번 출력 (0) | 2021.02.23 |
---|---|
[Assembly] 어셈블리 Hello World 출력 (0) | 2021.02.23 |