Transcript
Page 1: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

1

IKI10230Pengantar Organisasi Komputer

Kuliah no. 08: Multi-module ProgramsSumber:1. Paul Carter, PC Assembly Language2. Hamacher. Computer Organization, ed-53. Materi kuliah CS61C/2000 & CS152/1997, UCB

7 April 2004

L. Yohanes Stefanus ([email protected])Bobby Nazief ([email protected])

bahan kuliah: http://www.cs.ui.ac.id/kuliah/POK/

Page 2: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

2

REVIEW: Loop (2)° the following pseudo-code:

sum = 0;for ( i=0; i<10; i++ ) sum += i;

° could be translated into assembly as:1. mov eax, 0 ; eax is sum2. mov ecx, 0 ; ecx is i3. loop_start:4. add eax, ecx5. inc ecx6. cmp ecx,107. jne loop_start

° implementasi dgn loop: (salah satu alternatif)

1. mov eax, 0 ; eax is sum2. mov ecx, 10 ; ecx is (10-i)3. mov edx, 0 ; edx is i4. loop_start:5. add eax, edx6. inc edx7. loop loop_start

Page 3: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

3

KOREKSI: sub3.asm (Passing Parameters via Stack Frame)

push edx ; save i on stack push dword input ; push address on input on stack call get_int add esp, 8 ; remove i and &input from stack; subprogram get_int...; Parameters (in order pushed on stack); number of input (at [ebp + 12]); address of word to store input into (at [ebp + 8]); Notes:; values of eax and ebx are destroyedget_int: push ebp mov ebp, esp

mov eax, [ebp + 12] call print_int... call read_int mov ebx, [ebp + 8] mov [ebx], eax ; store input into memory

pop ebp ret ; jump back to caller

[ESP] [EBP]

[ESP+4] [EIP]

[ESP+8] [input]

[ESP+12] [edx]

Page 4: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

4

SubPrograms/SubRoutines/Functions

Page 5: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

5

Subroutine° Contoh dalam program bahasa C:

int add_scale(int x, int y, int sc){ int result;

result = x + sc * y; return result;}

main(){ int avec[3], bvec[3], cvec[3], scale;... for (i = 0; i < 3; i++) cvec[i] = add_scale(avec[i], bvec[i], scale);...}

Page 6: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

6

Use of CALL & RET in Subroutine Invocation_add_scale:...

mov eax,[ebp+16]imul eax,[ebp+12]add eax,[ebp+8]

...ret

_main:...L4:

cmp [ebp-44],2 ; cmp(i, 2)jle L7 ; i < 2jmp L5

L7:mov eax,[ebp-40] ; scalepush eax

...mov eax,[eax+edx] ; bvec[i]push eax

...mov eax,[eax+edx] ; avec[i]push eaxcall _add_scale

...mov [edx+ecx],eax ; cvec[i]

...

Page 7: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

7

Parameter Passing

between Calling- & Callee-subroutines

Page 8: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

8

Passing Parameters

1. By Value

2. By Reference

Page 9: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

9

Passing Parameters By Value

int add_scale(int x, int y, int scale){ int result;

result = x + scale * y; return result;}

main(){ int avec[3], bvec[3], cvec[3], scale;... for (i = 0; i < 3; i++) cvec[i] = add_scale(avec[i], bvec[i], scale);...}

Page 10: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

10

Passing Parameters By Reference

void v_add_scale(int x[ ], int y[ ], int z[ ], int scale){ for (i = 0; i < 3; i++) z[i] = add_scale(x[i], y[i], scale);}

main(){ int avec[3], bvec[3], cvec[3], scale;... v_acc_scale(avec, bvec, cvec, scale); /* cvec change after the call! */...}

Page 11: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

11

Implementation of Passing Parameters

° Via Register

° Via Stack

Page 12: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

12

Passing Values via Registers_main:

mov esi,[Scale]mov edx,0

L1: mov ecx,AVEC mov eax,[ecx+4*edx] add ecx,4*3 mov ebx,[ecx+4*edx]

call _add_scale add ecx,4*3 mov [ecx+4*edx],edi inc edx cmp edx,3 jb L1

_add_scale: mov edi,esi imul edi,ebx add edi,eax ret

Page 13: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

13

Passing References via Registers_main: mov eax,AVEC mov ebx,BVEC mov ecx,CVEC

mov esi,[Scale]call _v_add_scale

..._v_add_scale: mov edx,0L1: mov edi,esi imul edi,[ebx+4*edx] add edi,[eax+4*edx] mov [ecx+4*edx],edi inc edx cmp edx,3 jb L1 ret

Page 14: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

14

Use of Stack° To pass parameters

• due to limited number of registers• most HLLs use it as “standard” mechanism, known also

as calling conventions- assembly programs/modules need to use these

conventions to be able to communicate with HLL programs

• data are accessed from the stack directly instead of via POP instruction:

mov eax,[esp+4] ; esp as pointeror, Intel has provided EBP register for this purpose mov ebx,[ebp+4] ; ebp as pointer

° To store local variablesfunc(){ int x, y; ...}

Page 15: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

15

Stacks as Storage

for

Local Variables

Page 16: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

16

Stack as Storage for Local Variables

° Local variables live only as long as its function/ subroutine, where these variables reside, live (i.e., from the time the function/subroutine is called, until it returns execution to the calling program)

° Accordingly, memory allocation for local variables does not need to be for the duration of the whole program:

Stack is the convenient location for this temporary storage

Local variables are allocated on the stack, by extending the stack-frame used whenever a subroutine-call takes place (where return-address is placed)

Page 17: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

17

General Form of a Subroutinesubroutine_label:

push ebp ; save original EBP value on stack

mov ebp,esp ; new EBP = ESP, needed to access

; parameters passed by the calling program

sub esp,LOCAL_BYTES ; = # bytes needed by locals

; subprogram code

mov esp,ebp ; deallocate locals

pop ebp ; restore original EBP value

ret

ESP LocVar-m

... ...

EBP-4 LocVar-1

EBP [EBP]

[EIPcaller ]

EBP+8 Par-1

... ...

... Par-n

Page 18: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

18

Example: calc_sum.c

void calc_sum( int n, int *sump )

{

int i , sum = 0;

for ( i=1; i <= n; i++ )

sum += i;

*sump = sum;

}

Page 19: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

19

Example: calc_sum.asmcal_sum:

push ebpmov ebp, espsub esp, 4mov dword [ebp - 4], 0 ; sum = 0mov ebx, 1 ; i = 1

for_loop:cmp ebx, [ebp+12] ; is i >= n?jnle end_foradd [ebp-4], ebx ; sum += iinc ebx ; i++jmp short for_loop

end_for:mov ebx, [ebp+8] ; ebx = sumpmov eax, [ebp-4] ; eax = summov [ebx], eax ; *sump = summov esp, ebppop ebpret

ESP = EBP-4 Sum

EBP [EBP]

[EIPcaller ]

EBP+8 sump

EBP+12 N

Page 20: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

20

General Form of a Subroutine using Enter/Leavesubprogram_label:

enter LOCAL_BYTES, 0 ; = # bytes needed by locals

; subprogram code

leave

ret

° bandingkan dengan:

subroutine_label:

push ebp ; save original EBP value on stack

mov ebp,esp ; new EBP = ESP, needed to access

; parameters passed by the calling program

sub esp,LOCAL_BYTES ; = # bytes needed by locals

; subprogram code

mov esp,ebp ; deallocate locals

pop ebp ; restore original EBP value

ret

Page 21: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

21

Passing Parameters

via

Stack Frames

Page 22: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

22

Passing Values via Stack Frames (1/2)

int add_scale(int x, int y, int scale){ int result;

result = x + scale * y; return result;}

main(){ int avec[3], bvec[3], cvec[3], scale;... for (i = 0; i < vec_length; i++) cvec[i] = add_scale(avec[i], bvec[i], scale);...}

Page 23: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

23

LEA Instruction

° Load Effective Address

° LEA REG,MEMLOC ; REG MEMLOC• LEA EAX,[VAR1] ; EAX VAR1

- MOV EAX,VAR1

• LEA EAX,[EBX+ESI] ; EAX [EBX] + [ESI]

° Bandingkan dengan:• MOV EAX,[VAR1] ; EAX [VAR1]• MOV EAX,[EBX+ESI] ; EAX [[EBX] + [ESI]]

Page 24: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

24

Passing Values via Stack Frames (2/2)_main:L4: cmp [ebp-44],2 ;

cmp(i, 2)jle L7 ; i < 2jmp L5

L7:mov eax,[ebp-40] ; scalepush eax

...lea eax,[4*edx]lea edx,[ebp-24] ; bvecmov eax,[eax+edx] ; bvec[i]push eax

...lea eax,[4*edx]lea edx,[ebp-12] ; avecmov eax,[eax+edx] ; avec[i]push eaxcall _add_scale

...lea edx,[4*ecx]lea ecx,[ebp-36] ; cvecmov [ecx+edx],eax ; cvec[i]

...inc [ebp-44] ; i += 1jmp L4

L5:

_add_scale:

push ebp

mov ebp,esp

sub esp,24

mov eax,[ebp+16]

imul eax,[ebp+12]

mov edx,[ebp+8]

add edx,eax

mov [ebp-4],edx

mov edx,[ebp-4]

mov eax,edx

mov esp,ebp

pop ebp

retEBP [EBP]

[EIP]

EBP+8 Avec[i]

EBP+12 Bvec[i]

EBP+16 Scale

Page 25: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

25

Passing References via Stack Frames (1/2)

void v_add_scale(int x[ ], int y[ ], int z[ ], int scale){ for (i = 0; i < vec_length; i++) z[i] = add_scale(x[i], y[i], scale);}

main(){ int avec[3], bvec[3], cvec[3], scale;... v_acc_scale(avec, bvec, cvec, scale);...}

Page 26: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

26

Passing References via Stack Frames (2/2)_main:

mov eax,[ebp-40]

push eax

lea eax,[ebp-36]

push eax

lea eax,[ebp-24]

push eax

lea eax,[ebp-12]

push eax

call _v_add_scale

add esp,16

_v_add_scale:...L4:

cmp [ebp-4],2jle L7jmp L5

L7:mov eax,[ebp+20] ; scalepush eaxmov eax,[ebp-4]lea edx,[4*eax]mov eax,[ebp+12] ; BVECmov edx,[eax+edx] ; BVEC[i]push edx

...mov eax,[ebp+8] ; AVECmov edx,[eax+edx] ; AVEC[i]push edxcall _add_scale

...mov edx,[ebp+16]mov [edx+ecx],eaxinc [ebp-4]jmp L4

L5:...

ret

Page 27: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

27

Multi-module Programs

Page 28: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

28

Multi-module Programs

° A multi-module program is one composed of more than one object file.

• tugas0a.exe: driver.o tugas0a.o asm_io.o {C library}

° Combined by the linker• gcc –o tugas0a.exe driver.o tugas0a.o asm_io.o {C

library}

° The linker must match up references made to each label in one module (i.e. object file) to its definition in another module

• Calling-module declare the called-label “extern”• Called-module declare the called-label “global”

Page 29: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

29

Example° AddScale.asm:

global _add_scale_add_scale:...

mov eax,[ebp+16]imul eax,[ebp+12]add eax,[ebp+8]

...ret

° Main.asm:global _mainextern _add_scale

_main:...L4:...

mov eax,[eax+edx]; bvec[i]push eax

...mov eax,[eax+edx]; avec[i]push eaxcall _add_scale

Page 30: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

30

On Pop-Quiz #1

Page 31: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

31

No. 3° Jika diketahui variabel X adalah

sebuah integer (4 byte) dengan alamat 0xBFFE. Sistem komputernya menggunakan format Little-Endian untuk menyimpan data multi-byte dalam memori yang byte-addressable. Berapa nilai X?

0xBFFD 00h

0xBFFE 34h

0xBFFF 12h

0xC000 00h

0xC001 43h

0xC002 12h

0xC003 00h

0xC004 00h

Page 32: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

32

No. 4° Var i = 1001b (integer 4-bit) dan j = 00010000b (integer 8-

bit), keduanya menggunakan representasi komplemen-2. Hitung k = i + j! (nyatakan dalam biner)

i = 1001 = 11111001 = -7

j = 00010000 = 16

k = 00001001 = 9

Page 33: 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

33

No. 5

° Perhatikan bagian program berikut:

segment .data

var_a dd 44332211h

var_a db 11h,22h,33h,44h

segment .text

mov esi,var_a

mov al,[esi] ; esi = 1000 , al = 11

inc esi ; esi = 1001

add [esi],al ; [esi]lama = 22 , al = 11

; [esi]b aru = 33

add esi,2 ; esi = 1003

sub [esi],al ; [esi]lama = 44 , al = 11

; [esi]baru = 33

; [var_a] = 11, 33333311

Var_a = 0x1000 11h

0x1001 33h

0x1002 33h

0x1003 33h


Top Related