외로운 Nova의 작업실

Assembly 언어 공부 - 7(demo2,demo3) 본문

Programming/Assembly

Assembly 언어 공부 - 7(demo2,demo3)

Nova_ 2022. 5. 24. 21:47

안녕하세요. 오늘은 저번시간에 이어서 tutorial-demo2와3에대해서 코드 리뷰와 직접 실행까지 해보도록 하겠습니다.

 

<demo2>

어제와 마찬가지로 masm32\tutorial\console 파일에 들어가 줍시다.

어제 demo1을 공부했으니 오늘은 demo2에 들어가보겠습니다.

proc.asm 파일을 MASM32 Editor로 열어줍시다.

해당 코드를 긁어와보겠습니다.

; カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?

;                 Build this with the "Project" menu using
;                       "Console Assemble and Link"

; カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?

;   Procedures are a fundamental building block of programs that are
;   build directly into the processor using CALL and RET instructions.
;   This shows how simple it is to do in MASM.

; カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?

    .486                                    ; create 32 bit code
    .model flat, stdcall                    ; 32 bit memory model
    option casemap :none                    ; case sensitive
 
    include \masm32\include\windows.inc     ; always first
    include \masm32\macros\macros.asm       ; MASM support macros

  ; -----------------------------------------------------------------
  ; include files that have MASM format prototypes for function calls
  ; -----------------------------------------------------------------
    include \masm32\include\masm32.inc
    include \masm32\include\gdi32.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc

  ; ------------------------------------------------
  ; Library files that have definitions for function
  ; exports and tested reliable prebuilt code.
  ; ------------------------------------------------
    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\gdi32.lib
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\kernel32.lib

    .code                       ; Tell MASM where the code starts

; カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?

start:                          ; The CODE entry point to the program

    call main                   ; branch to the "main" procedure

    exit

; カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?

main proc

    print chr$("Hi, I am in the 'main' procedure",13,10)

    ret                         ; return to the next instruction after "call"

main endp

; カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?

end start                       ; Tell MASM where the program ends

어제와 마찬가지로 한글로 번역해서 다시한번 코드를 써보겠습니다.

; カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?

;                 conole Assembly와 Link를 사용하여
;		프로젝트 메뉴를 빌드하세요.

; カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?

;   프로시저는 CAL 및 RET 명령을 사용하여 프로세서에
;	직접 빌드되는 프로그램의 기본 구성 요소입니다.
;   이것은 MASM에서 하는 것이 얼마나 간단한지 보여줍니다.

; カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?

    .486                                    ; 32bit 코드를 생성합니다.
    .model flat, stdcall                    ; 32 bit 메모리 모델을 사용합니다.
    option casemap :none                    ; 대소문자를 구분합니다.
 
    include \masm32\include\windows.inc     ; 항상 처음에 써줘야합니다.
    include \masm32\macros\macros.asm       ; MASM를 도와주는 메크로입니다.

  ; -----------------------------------------------------------------
  ; 함수 호출을 위한 MASM 형식 프로토타입을 포함하는 파일
  ; -----------------------------------------------------------------
    include \masm32\include\masm32.inc
    include \masm32\include\gdi32.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc

  ; ------------------------------------------------
  ; 함수에 대한 정의가 있고 신뢰할 수 있는 사전 빌드 코드를
  ; 테스트한 라이브러리 파일입니다.
  ; ------------------------------------------------
    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\gdi32.lib
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\kernel32.lib

    .code                       ; MASM에게 코드의 시작점을 알려줍니다.

; カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?

start:                          ; 프로그램의 엔트리포인트 코드입니다.

    call main                   ; main 프로시저로 분기합니다.

    exit

; カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?

main proc						; main 프로시저 코드입니다.

    print chr$("Hi, I am in the 'main' procedure",13,10)

    ret                         ; call이후 다음 명령어로 리턴합니다.(44번줄 다음으로 가라는 뜻)

main endp

; カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?

end start                       ; MASM에게 프로그램이 끝났다고 알려줍니다.

주석을 천천히 읽었다면 이번에는 어셈블리어의 프로시저에대해서 배우는 코드였다는 것을 알 수 있습니다.

추가로 어셈블리어의 구성요소중에서 .code는 segment이며 start는 Label이며 main은 procedure 입니다.

Lable과 segment는 call명령어로 호출 할 수 없지만 procedure은 call명령어로 호출 할 수 있습니다.

마치 c언어에서 함수와 같은 존재입니다.

위 코드에서는 main프로시저로 print 함수를 실행시키고 있습니다.

또한 프로시저는 꼭 ret을 사용해서 끝을 내줘야합니다.(안그러면 프로그램이 끝이 안남)

위 코드로 컴파일 및 링크된 proc응용프로그램을 실행시켜보겠습니다.

잘 실행이 되는 것을 알 수 있습니다.

어제와 마찬가지로 ""안에 있는 문장을 바꾸면 콘솔에 출력되는 문장이 바뀌게 될 것 입니다.

 

<demo3>

demo2를 봤으니 이번에는 demo3를 봐보도록 하겠습니다.

방금처럼 demo3파일을 열어보도록 하겠습니다.

data.asm 파일을 MASM32 Editor로 열어보겠습니다.

해당 코드를 긁어와보도록 하겠습니다.

; カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?

;                 Build this with the "Project" menu using
;                       "Console Assemble and Link"

comment * カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?

A normal component of almost all programs is data. It can be either numeric
data or text data. In MASM you have the distinction between data that is
initialised with a value or UNinitialised data that just reserves the
space to write data to.

Initialised data has this form.

.data
  var1  dd  0                           ; 32 bit value initialised to zero
  var2  dd  125                         ; 32 bit value initialised to 125
  txt1  db  "This is text in MASM",0    ; A zero terminated sequence of TEXT

  array dd 1,2,3,4,5,6,7,8              ; 8 x 32 bit values in sequence

Uninitialised data has this form.

.data?
  udat1 dd ?                            ; Uninitialised single 32 bit space
  buffa db 128 dup (?)                  ; 128 BYTES of uninitialised space

カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?*

    .486                                    ; create 32 bit code
    .model flat, stdcall                    ; 32 bit memory model
    option casemap :none                    ; case sensitive
 
    include \masm32\include\windows.inc     ; always first
    include \masm32\macros\macros.asm       ; MASM support macros

  ; -----------------------------------------------------------------
  ; include files that have MASM format prototypes for function calls
  ; -----------------------------------------------------------------
    include \masm32\include\masm32.inc
    include \masm32\include\gdi32.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc

  ; ------------------------------------------------
  ; Library files that have definitions for function
  ; exports and tested reliable prebuilt code.
  ; ------------------------------------------------
    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\gdi32.lib
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\kernel32.lib


    .data
      txtmsg db "I am data in the initialised data section",0

    .code                       ; Tell MASM where the code starts

; カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?

start:                          ; The CODE entry point to the program

    call main                   ; branch to the "main" procedure

    exit

; カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?

main proc

    print OFFSET txtmsg         ; the "OFFSET" operator tells MASM that the text 
                                ; data is at an OFFSET within the file which means
                                ; in this instance that it is in the .DATA section

    ret                         ; return to the next instruction after "call"

main endp

; カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?

end start                       ; Tell MASM where the program ends

demo2와 같이 영어부분을 한국어로 변경해보도록 하겠습니다.

; カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?

;                     콘솔 어셈블과 링크를 사용하여
;                   프로젝트 메뉴를 사용하여 빌드하세요

comment * カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?

;거의 모든 프로그램의 일반적인 구성 요소는 데이터입니다.
;데이터는 숫자 데이터 또는 텍스트 데이터일 수 있습니다.
;MASM에서는 값으로 초기화된 데이터와 데이터를 쓸 공간을 
;초기화가 안된 데이터와 구분합니다.

초기화된 데이터의 형식은 다음과 같습니다.

.data
  var1  dd  0                           ; 32 bit 값으로 0으로 초기화 하고있습니다.
  var2  dd  125                         ; 32 bit 값으로 125로 초기화 하고있습니다.
  txt1  db  "This is text in MASM",0    ; 0으로 종료된 텍스트 문장입니다.

  array dd 1,2,3,4,5,6,7,8              ; 8 x 32 bit 값의 순차적인 배열입니다.

초기화되지 않은 데이터의 형식은 다음과 같습니다.

.data?
  udat1 dd ?                            ; 초기화되지 않은 단일 32비트 공간을 선언합니다.
  buffa db 128 dup (?)                  ; 초기화되지 않은 128바이트 공간을 선언합니다.

カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?*

    .486                                    ; 32비트 코드입니다.
    .model flat, stdcall                    ; 32 bit 메모리 모델입니다.
    option casemap :none                    ; 대소문자를 구분합니다.
 
    include \masm32\include\windows.inc     ; 항상 처음에 써줍니다.
    include \masm32\macros\macros.asm       ; MASM을 도와주는 메크로입니다.

  ; -----------------------------------------------------------------
  ; 함수 호출을 위한 MASM 형식 프로토타입을 포함하는 파일
  ; -----------------------------------------------------------------
    include \masm32\include\masm32.inc
    include \masm32\include\gdi32.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc

  ; ------------------------------------------------
  ; 함수에 대한 정의가 있고 신뢰할 수 있는 사전 빌드 코드를
  ; 테스트한 라이브러리 파일입니다.
  ; ------------------------------------------------
    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\gdi32.lib
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\kernel32.lib


    .data
      txtmsg db "I am data in the initialised data section",0

    .code                       ; MASM에게 코드의 시작점을 알려줍니다.

; カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?

start:                          ; 프로그램의 엔트리포인트 코드입니다.

    call main                   ; main프로시져로 분기합니다.

    exit

; カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?

main proc

    print OFFSET txtmsg         ; "OFFSET" 연산자는 MASM에 텍스트 데이터가 파일 내의
                                ; .DATA 섹션에 있음을 알려줍니다.(56번줄에 있음)

    ret                         ; call 다음 명령어를 수행하도록 합니다.(64번줄 밑의 명령어를 실행하도록 합니다)

main endp

; カカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカカ?

end start                       ; MASM에게 코드의 끝을 알려줍니다.

코드를 주석과 함께 읽으셧다면 해당 튜토리얼은 데이터에대해서 알려주는 튜토리얼임을 알 수 있습니다.

MASM에서 데이터는 두가지로 나뉘게됩니다.

하나는 초기화된 데이터, 다른 하나는 초기화되지 않은 데이터입니다.

초기화된 데이터의 경우 .data 세그먼트에 씁니다.

초기화가 되지않은 데이터의 경우 .data? 세그먼트에씁니다.

또한 OFFSET명령어는 해당 데이터가 .data 세그먼트 안에 있음을 알려줍니다.

이제 data.exe를 실행 시켜보도록 하겠습니다.

아마 txtmsg데이터의 값인 I am data in the initialised data section 이 출력될 것입니다.

실제로 실행시켜보면 예상한대로 해당 문장이 뜨는 것을 알 수 있습니다.

Comments