36
Copyright, 2005 © Multimedia Lab., UOS Multithreading Applications in Win32 (Introduction to Assembly Code and Calling Convention) Seong Jong Choi [email protected] Multimedia Lab. Dept. of Electrical and Computer Eng. University of Seoul Seoul, Korea

Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

Copyright, 2005 © Multimedia Lab., UOS

Multithreading

Applications

in Win32

(Introduction to Assembly Code and Calling Convention)

Seong Jong Choi

[email protected]

Multimedia Lab.

Dept. of Electrical and Computer Eng.

University of Seoul

Seoul, Korea

Page 2: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-2

MS VC++

• Integrated Development Environment– Language Sensitive Text Editor– Preprocessor– Compiler– Linker– Wizard

• Project (.dsp)– A collection of all the necessary information to build a binary

excutibles (.exe .dll)– Files (source, header)– Compile options– Link options

• Work Space (.dsw)– A collection of projects

Page 3: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-3

Build Process

Makefile Header filesSource

Editor

Preprocessor

Compiler

Object file

Object files Linker Libraries

Debug Ver. Release Ver

iostream.hhello.cpp

hello.obj

hello.exe hello.exe

mlibcewq.lib

개발 툴By MS VC++

사용자 정의

Page 4: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-4

Assembly Code

• Project Setting -> C/C++ -> Category -> Listing files -> Listing file type -> Assembly, Machine Code, and Source

• Then, compile

• You’ll see xxx.cod file in the debug directory

Page 5: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-5

Intel 80386 Registers

Page 6: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-6

Intel Fundamental Data Type

Page 7: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-7

Assembly code

• Instruction := operation [operand] [, operand]

• Examples– Data movement: mov destaddr, eax

– Stack operation: pop eax

– Arithmetic, logic, comparison, etc

Page 8: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-8

Operand: Addressing Mode

• Immediate: Instruction에포함

• Register Direct: Register의내용

• Register indirect: Register 내용을메모리주소로 사용

• Memory Direct: Memory의내용

• Memory indirect: Memory 내용을주소로사용

• Index: address +-

Page 9: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-9

Data Movement Instruction

• mov destination, source

_a$ = -4

mov dword ptr _a$[ebp], OAh

Above two lines are equivalent to:

mov dword ptr [ebp-4], 0Ah

operation

Operand: index + register indirect addressing

Operand: Immediate addressing

Page 10: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-10

Stack Instructions

• PUSH– Decrement the stack pointer (ESP)

– Then, transfer source to the stack indicated by ESP

– Push eax

• POP– Transfer data at the current top of stack (ESP)

– Then, increment ESP

– Pop eax

Page 11: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-11

Stack Instructions

Page 12: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-12

Assembly Debugging

• View -> Debug windows에서– Register

– Memory

– Disassembly

Page 13: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-13

An Example

/* simple.cppdemonstrating assembly language code generated by the compiler

*/#include <windows.h>int sum(int x, int y);int WINAPI wsum(int x, int y);

void main() {int a, b, c;a = 10;b = 20;c = a + b;c = sum(a,b);c = wsum(a,b);

}

int sum(int x, int y) {int z;z = x + y;return z;

}

int WINAPI wsum(int x, int y) {int z;z = x + y;return z;

}

Page 14: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-14

Simple.cod File

; 11 : a = 10;

00018 c7 45 fc 0a 00 00 00

mov DWORD PTR _a$[ebp], 10; 0000000aH

C source code

Translated machine code

Memory address for

machine code

Translated assembly code

Page 15: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-15

Disassembly Window

11: a = 10;

00401048 mov dword ptr [ebp-4],0Ah

C source code

Memory address for

machine code

(Relocated)

Translated assembly code (Disassemble)

Page 16: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-16

Function Call

Caller; 14 : c = sum(a,b);

mov ecx, DWORD PTR _b$[ebp]

push ecx

mov edx, DWORD PTR _a$[ebp]

push edx

call ?sum@@YAHHH@Z ; sum

add esp, 8

mov DWORD PTR _c$[ebp], eax

Callee; COMDAT ?sum@@YAHHH@Z_TEXT SEGMENT_x$ = 8_y$ = 12_z$ = -4?sum@@YAHHH@Z PROC NEAR ; sum, COMDAT

; 19 : int sum(int x, int y) {

push ebpmov ebp, espsub esp, 68 ; 00000044Hpush ebxpush esipush edilea edi, DWORD PTR [ebp-68]mov ecx, 17 ; 00000011Hmov eax, -858993460 ; ccccccccHrep stosd

; 20 : int z;; 21 : z = x + y;

mov eax, DWORD PTR _x$[ebp]add eax, DWORD PTR _y$[ebp]mov DWORD PTR _z$[ebp], eax

; 22 : return z;

mov eax, DWORD PTR _z$[ebp]

; 23 : }

pop edipop esipop ebxmov esp, ebppop ebpret 0

?sum@@YAHHH@Z ENDP ; sum_TEXT ENDS

Page 17: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-17

Before Function Call

• Assume:– esp = n + 4

– ebp = bbpp

– edi = ddii

– esi = ssii

– ebx = bbxx

• The above registers are used in the callee.

Page 18: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-18

Function Call - Caller

; 14 : c = sum(a,b); //esp = n+4

mov ecx, DWORD PTR _b$[ebp]

push ecx

mov edx, DWORD PTR _a$[ebp]

push edx

call ?sum@@YAHHH@Z ; sum

add esp, 8

mov DWORD PTR _c$[ebp], eax

2번째 argument를Stack에저장

b n

n-4

n-8

n-12

ESP

Page 19: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-19

Function Call - Caller

; 14 : c = sum(a,b);

mov ecx, DWORD PTR _b$[ebp]

push ecx

mov edx, DWORD PTR _a$[ebp]

push edx

call ?sum@@YAHHH@Z ; sum

add esp, 8

mov DWORD PTR _c$[ebp], eax

1번째 argument를Stack에저장

b n

a n-4

n-8

n-12

ESP

Page 20: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-20

Function Call - Caller

; 14 : c = sum(a,b);

mov ecx, DWORD PTR _b$[ebp]

push ecx

mov edx, DWORD PTR _a$[ebp]

push edx

call ?sum@@YAHHH@Z ; sum

add esp, 8

mov DWORD PTR _c$[ebp], eax

Return address를Stack에저장

b n

a n-4

return addr n-8

n-12

ESP

return addr:

Page 21: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-21

Function Call - Callee

; 19 : int sum(int x, int y) {

push ebp; [ebp] = bbppmov ebp, espsub esp, 68 ;00000044Hpush ebxpush esipush edilea edi, DWORD PTR [ebp-68]movecx, 17 ; 00000011Hmoveax, -858993460; ccccccccHrep stosd

함수에서 ebp를사용하기위해우선ebp를 Stack에저장

b n

a n-4

return addr n-8

bbpp n-12ESP

Page 22: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-22

Function Call - Callee

; 19 : int sum(int x, int y) {

push ebp; [ebp] = bbppmov ebp, espsub esp, 68 ;00000044Hpush ebxpush esipush edilea edi, DWORD PTR [ebp-68]mov ecx, 17 ; 00000011Hmov eax, -858993460; ccccccccHrep stosd

ebp = n-12

b n

a n-4

return addr n-8

bbpp n-12ESP

EBP

Page 23: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-23

Function Call - Callee

; 19 : int sum(int x, int y) {

push ebp; [ebp] = bbppmov ebp, espsub esp, 68 ;00000044Hpush ebxpush esipush edilea edi, DWORD PTR [ebp-68]mov ecx, 17 ; 00000011Hmov eax, -858993460; ccccccccHrep stosd

esp = n-80

b n

a n-4

return addr n-8

bbpp n-12

ESP

n-80

EBP

n-16

Page 24: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-24

Function Call - Callee

; 19 : int sum(int x, int y) {

push ebp; [ebp] = bbppmov ebp, espsub esp, 68 ;00000044Hpush ebxpush esipush edilea edi, DWORD PTR [ebp-68]mov ecx, 17 ; 00000011Hmov eax, -858993460; ccccccccHrep stosd

함수에서사용할 Register

의내용을 Stack에저장

b n

a n-4

return addr n-8

bbpp n-12

ESP

n-80

EBP

n-16

bbxx n-84

ssii n-88

ddii n-92

Page 25: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-25

Function Call - Callee

; 19 : int sum(int x, int y) {

push ebp; [ebp] = bbppmove bp, espsub esp, 68 ;00000044Hpush ebxpush esipush edilea edi, DWORD PTR [ebp-68]mov ecx, 17 ; 00000011Hmov eax, -858993460; ccccccccHrep stosd

Stack의내용을모두cch로저장4 x 17 = 68

b n

a n-4

return addr n-8

bbpp n-12

ESP

cccccccch

cccccccch n-80

EBP

cccccccch n-16

bbxx n-84

ssii n-88

ddii n-92

EDI

Page 26: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-26

Function Call - Callee

_x$ = 8

_y$ = 12

_z$ = -4

; 21 : z = x + y;

mov eax, DWORD PTR _x$[ebp]

add eax, DWORD PTR _y$[ebp]

mov DWORD PTR _z$[ebp], eax

a와 b를더해[ebp-4]에저장

b n

a n-4

return addr n-8

bbpp n-12

ESP

cccccccch

cccccccch n-80

EBP

z = ssssssssh n-16

bbxx n-84

ssii n-88

ddii n-92

EBP-4

EBP+8

EBP+12

Page 27: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-27

Function Call - Callee

; 22 : return z;

mov eax, DWORD PTR _z$[ebp]

Return할값을 eax에저장eax = ssssssssh

b n

a n-4

return addr n-8

bbpp n-12

ESP

cccccccch

cccccccch n-80

EBP

z = ssssssssh n-16

bbxx n-84

ssii n-88

ddii n-92

EBP-4

EBP+8

EBP+12

Page 28: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-28

Function Call - Callee

; 23 : }

pop edi

pop esi

pop ebx

mov esp, ebp

pop ebp

ret 0

함수에서사용하기전Register값복원

b n

a n-4

return addr n-8

bbpp n-12

ESP

cccccccch

cccccccch n-80

EBP

z = ssssssssh n-16

bbxx n-84

ssii n-88

ddii n-92

Page 29: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-29

Function Call - Callee

; 23 : }

pop edi

pop esi

pop ebx

mov esp, ebp

pop ebp

ret 0

esp값에 68 더함

b n

a n-4

return addr n-8

bbpp n-12ESP

cccccccch

cccccccch n-80

EBP

z = ssssssssh n-16

bbxx n-84

ssii n-88

ddii n-92

Page 30: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-30

Function Call - Callee

; 23 : }

pop edi

pop esi

pop ebx

mov esp, ebp

pop ebp

ret 0

ebp값을복원ebp = bbpp

b n

a n-4

return addr n-8

bbpp n-12

ESP

cccccccch

cccccccch n-80

z = ssssssssh n-16

bbxx n-84

ssii n-88

ddii n-92

Page 31: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-31

Function Call - Callee

; 23 : }

pop edi

pop esi

pop ebx

mov esp, ebp

pop ebp

ret 0

caller로다시가기위해eip값설정eip = return addr

b n

a n-4

return addr n-8

bbpp n-12

ESP

cccccccch

cccccccch n-80

z = ssssssssh n-16

bbxx n-84

ssii n-88

ddii n-92

Page 32: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-32

Function Call - Caller

; 14 : c = sum(a,b);

mov ecx, DWORD PTR _b$[ebp]

push ecx

mov edx, DWORD PTR _a$[ebp]

push edx

call ?sum@@YAHHH@Z ; sum

add esp, 8

mov DWORD PTR _c$[ebp], eax

esp를원래대로복원

b n

a n-4

return addr n-8

n-12

ESP

return addr:

n+4

Page 33: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-33

Function Call - Caller

; 14 : c = sum(a,b);

mov ecx, DWORD PTR _b$[ebp]

push ecx

mov edx, DWORD PTR _a$[ebp]

push edx

call ?sum@@YAHHH@Z ; sum

add esp, 8

mov DWORD PTR _c$[ebp], eax

eax에저장된계산결과를 c로저장

b n

a n-4

return addr n-8

n-12

ESP

return addr:

n+4

Page 34: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-34

Function Call: Summary

• Stack is used to transfer function arguments.

• Function arguments are pushed to the stack in the sequence from right.

• Stack is used as a storage for callee’s local variables.

• Registers are restored after returning from the callee.

• Return value (integer) is stored in eax.

• Caller sets up esp after the return (add esp, 8)

Page 35: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-35

Return Instruction

• The difference between sum() and wsum() is that who cleans the stack for input parameters.

• Compare between sum() return instruction (ret 0) and wsum() return instruction (ret 8).

• ret n– RET transfers control to a return address located on the stack. The

address is usually placed on the stack by a CALL instruction, and the return is made to the instruction that follows the CALL.

– The optional numeric parameter to RET gives the number of stack bytes to be released after the return address is popped. These items are typically used as input parameters to the procedure called.

Page 36: Multithreading Applications in Win32mmlab.uos.ac.kr/system programming/multithread/Assm.pdf · 24/09/2013  · Multimedia Lab. Dept. of Electrical and Computer Eng. University of

2013-09-24 Seong Jong Choi Assembly Language-36

Calling Convention

Keyword Stack cleanup Parameter passing

__cdecl

(C default)Caller

Pushes parameters on the stack, in reverse order (right to left)

__stdcall(#define WINAPI __stdcall)

Callee Pushes parameters on the stack, in reverse order (right to left)

__fastcall Callee Stored in registers, then pushed on stack

thiscall

(not a keyword)Callee

Pushed on stack; this pointer stored in ECX