Busan IT/Assembly2015. 8. 21. 17:31

==================================Outline====================================

디버깅

명령어의 표현과 구조

----------------------------------------------------------------------------

디버깅

 

asm 파일 컴파일 시 디버그 기능 추가

 

- 컴파일

ml /c /coff /Zi *.asm

 

- 링크

link /debug /subsystem:console /entry:start /out:*.exe *.obj kernel32.lib

 

 

디버그를 위한 툴 'windbg'

 

 

 

새창으로 windbg가 열리면 메뉴에서 File -> Open Excutable...

실행 파일을 선택한다.

 

실행 창과 command 창이 뜬다.

F8 또는 'step into' 아이콘을 눌러 프로그램을 진행 시킨다.

이 때 메모리와 레지스터의 상태를 보고 싶다면 메뉴->View에서 원하는 것을 선택한다.

 

 

프로그램이 실행되면 소스가 적힌 코드 창이 뜬다. 여기에 노란줄로 실행되고 있는 코드 영역이 표시 된다.

 

 

 

메모리를 볼 때 원하는 변수명을 앞에 ‘&’와 같이 붙혀 주면 해당 변수의 시작점부터 메모리를 볼 수 있다.

'step into' 버튼을 눌러가며 메모리와 레지스터의 변화를 살펴본다.

 

//EIP는 실행되고 있는 명령어 주소를 가리키는 레지스터이다.

 

 

 

 

명령어의 표현과 구조

 

 

컴파일 시 '/Fl' 옵션을 추가하면 ‘lts'파일이 생성된다.

 

lst파일은 가장 좌측에는 메모리 시작점에서 떨어진 주소를 16진수 8자리 숫자로 표시하고,

그 다음 숫자는 기계어를 나타낸다.

 

//어셈블리에서는 대소문자를 구분하지 않는다.

 

mask BYTE 7dh -> char mask = 125;


변수에 값을 직접적으로 넣는 것을 direct 방식, 간접적으로 넣는 방식을 indirect방식이라고 한다.

 

indirect 방식을 사용할 때 ‘[]’를 사용한다. C에서 포인터 방식이라고 생각하면 된다. indirect 방식은 레지스터에만 사용 가능하다.

 

데이터 복사 명령어

 

number of Byte는 컴파일 후 생성되는 명령어의 길이다.

//immediate는 레지스터 혹은 변수에 들어가는 실제 값


/*** lst 코드 ***/


Microsoft (R) Macro Assembler Version 6.11        08/21/15 16:17:23
sample1.asm                 Page 1 - 1


        ; Sample code for dummy

        .386
        .MODEL FLAT

        ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD

        INCLUDE io.h
            C ; IO.H -- header file for I/O macros
            C ; 32-bit version for flat memory model
            C ; R. Detmer   last revised 8/2000
            C .NOLIST     ; turn off listing
            C .LIST        ; begin listing
            C 

 = 0000000D      cr EQU 0dh
 = 0000000A      Lf EQU 0ah

        .STACK 4096

 00000000      .DATA
 00000000 00000000    number1   DWORD  ?
 00000004 00000000    number2   DWORD  ?
 00000008 45 674 65 72  prompt1   BYTE  "Enter first number: "0
     20 66 69 72 73
     74 20 675 6D
     62 65 72 320
     00
 000000145 674 65 72  prompt2   BYTE  "Enter second number: "0
     20 73 65 63 6F
     664 20 675
     662 65 72 3A
     20 00
 00000033  00000028 [    string    BYTE  40 DUP (?)
      00
     ]
 00000050054 68 65  labell    BYTE  cr, Lf, "The sum is "
     20 73 75 620
     69 73 20
 00000068  0000000B [    sum     BYTE  11 DUP (?)
      00
     ]
 00000073  0000        BYTE  cr, Lf, 0

 00000000      .CODE

        ;entry point
 00000000      _start:
          output  prompt1    ;a printf call in C, output is a function made from the provider
          input  string, 40  ;a scanf call in C, percieve as ASCII code
          atod  string    ;convert ASCII to DWORD and store to eax
 0000002E  A3 00000000 R    mov  number1, eax  ;number1 = eax

          output  prompt2
          input  string, 40
          atod  string
 00000061  A3 00000004 R    mov  number2, eax  

 00000066  A1 00000000 R    mov  eax, number1
 0000006B  03 05 00000004 R    add  eax, number2  ; eax = eax + number2 // result of algebric operation is stored at eax
          dtoa  sum, eax
          output  labell

          INVOKE  ExitProcess, 0

        PUBLIC _start

        END



Microsoft (R) Macro Assembler Version 6.11        08/21/15 16:17:23
sample1.asm                 Symbols 2 - 1




Macros:

                N a m e                 Type

atod . . . . . . . . . . . . . .  Proc
atoi . . . . . . . . . . . . . .  Proc
dtoa . . . . . . . . . . . . . .  Proc
input  . . . . . . . . . . . . .  Proc
itoa . . . . . . . . . . . . . .  Proc
output . . . . . . . . . . . . .  Proc


Segments and Groups:

                N a m e                 Size     Length   Align   Combine Class

FLAT . . . . . . . . . . . . . .  GROUP
STACK  . . . . . . . . . . . . .  32 Bit   00001000 DWord    Stack    'STACK'   
_DATA  . . . . . . . . . . . . .  32 Bit   00000076 DWord    Public  'DATA'  
_TEXT  . . . . . . . . . . . . .  32 Bit   00000097 DWord    Public  'CODE'  


Procedures,  parameters and locals:

                N a m e                 Type     Value    Attr

ExitProcess  . . . . . . . . . .  P Near   00000000 FLAT  Length= 00000000 External STDCALL


Symbols:

                N a m e                 Type     Value    Attr

@CodeSize  . . . . . . . . . . .  Number   00000000h   
@DataSize  . . . . . . . . . . .  Number   00000000h   
@Interface . . . . . . . . . . .  Number   00000000h   
@Model . . . . . . . . . . . . .  Number   00000007h   
@code  . . . . . . . . . . . . .  Text      _TEXT
@data  . . . . . . . . . . . . .  Text      FLAT
@fardata?  . . . . . . . . . . .  Text      FLAT
@fardata . . . . . . . . . . . .  Text      FLAT
@stack . . . . . . . . . . . . .  Text      FLAT
Lf . . . . . . . . . . . . . . .  Number   0000000Ah   
_start . . . . . . . . . . . . .  L Near   00000000 _TEXT  Public
atodproc . . . . . . . . . . . .  L Near   00000000 FLAT  External
atoiproc . . . . . . . . . . . .  L Near   00000000 FLAT  External
cr . . . . . . . . . . . . . . .  Number   0000000Dh   
dtoaproc . . . . . . . . . . . .  L Near   00000000 FLAT  External
inproc . . . . . . . . . . . . .  L Near   00000000 FLAT  External
itoaproc . . . . . . . . . . . .  L Near   00000000 FLAT  External
labell . . . . . . . . . . . . .  Byte   0000005B _DATA  
number1  . . . . . . . . . . . .  DWord   00000000 _DATA  
number2  . . . . . . . . . . . .  DWord   00000004 _DATA  
outproc  . . . . . . . . . . . .  L Near   00000000 FLAT  External
prompt1  . . . . . . . . . . . .  Byte   00000008 _DATA  
prompt2  . . . . . . . . . . . .  Byte   0000001D _DATA  
string . . . . . . . . . . . . .  Byte   00000033 _DATA  
sum  . . . . . . . . . . . . . .  Byte   00000068 _DATA  

     0 Warnings
     0 Errors

반응형
Posted by newind2000