오늘은 입출력문,반복문,제어문에 대해 공부하였다.
우선 scanf,printf를 공부하였는데 우선
/* Type your code here, or load an example. */
#include <stdio.h>
int main() {
int m = 0;
scanf("%d",&m);
printf("%d", m);
}
이 코드를 넣고 gcc를 돌렸어야 했으나 귀찮아 Compiler Explorer Website를 활용하여 assembly로 변경하였다.
.LC0:
.string "%d"
"main":
push rbp
mov rbp, rsp
sub rsp, 16
mov DWORD PTR [rbp-4], 0
lea rax, [rbp-4]
mov rsi, rax
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call __isoc23_scanf
mov eax, DWORD PTR [rbp-4]
mov esi, eax
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call "printf"
mov eax, 0
leave
ret
우선 LC0에서는 string을 다룬다 어셈블리에서는 C와 동일하게 String을 언어내에서 지원하지않는다 기계어에서도 string은 몇바이트라는 약속이 되어있지않는다. 그래서 어셈블리에서 string을 다룰때에는 연속된 바이트로써 string을 저장하는데 예를 들어 0x1000부터 string을 저장한다고 가정하자. 그럼 0x1000~0x1004까지 string을 선언하면 이런 string이 어디에 저장되어있는지 식별하기위에서 Label을 활용한다.
그다음은 rbp인데 이는 어셈블리의 기본시작이므로 깊게 이야기는 안할려고 한다. 함수 스택이 쌓일 적에 스택을 쌓는 base를 rbp(Base pointer)라고 한다. 그리고 rsp(stack pointer)를 rbp로 옮긴다.
그 다음줄인 mov DWORD PTR [rbp-4],0 인데 이는 mov(복사(이동)하라) DWORD PTR(4바이트 만큼)[rbp-4](rbp에서 4바이트만큼 내린 공간에서),0(0을)이라는 말이 된다. 즉 int m = 0;인 셈이다.
scnaf 함수 공간인데 보통 assembly에서 함수를 부를때에는 함수호출규약을 따른다. call은 함수를 부르는 역할이고 함수들의 인자는 rax,eax등에 규약으로 정해준 순서대로 들어간뒤 함수를 부른다. 여기서 scanf 함수를 봐보자.
/* Copyright (C) 1991-2025 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <stdarg.h>
#include <stdio.h>
#include <libioP.h>
/* Read formatted input from stdin according to the format string FORMAT. */
int
__isoc23_scanf (const char *format, ...)
{
va_list arg;
int done;
va_start (arg, format);
done = __vfscanf_internal (stdin, format, arg,
SCANF_ISOC99_A | SCANF_ISOC23_BIN_CST);
va_end (arg);
return done;
}
여기서 scanf를 밭고 이것들을 가지고 _vfscanf_internal을 부른다. 즉 _isoc23_scanf는 wrraper class인것이다.
internal이 움직이는 모양이 진짜 scanf의 움직임인것이다.
'모각코(코딩스터디)' 카테고리의 다른 글
| 2026 하계 모각코 목표 (0) | 2026.06.27 |
|---|---|
| 2026-02-09 동계모각코 6차 회고 (0) | 2026.02.09 |
| 2026-02-09 동계모각코 6차 목표 (0) | 2026.02.09 |
| 2026-02-07 동계 모각코 5차 목표 (0) | 2026.02.09 |
| 2027-02-07 동계모각코 5차회고 (0) | 2026.01.30 |