1 / 33

IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs

IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs. Sumber : 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization , ed-5 3. Materi kuliah CS61C/2000 & CS152/1997, UCB. 7 April 2004

brina
Download Presentation

IKI10230 Pengantar Organisasi Komputer Kuliah no. 08: Multi-module Programs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. IKI10230Pengantar Organisasi KomputerKuliah no. 08: Multi-module Programs Sumber:1. Paul Carter, PC Assembly Language2. Hamacher. Computer Organization, ed-53. Materi kuliah CS61C/2000 & CS152/1997, UCB 7 April 2004 L. Yohanes Stefanus (yohanes@cs.ui.ac.id)Bobby Nazief (nazief@cs.ui.ac.id) bahan kuliah: http://www.cs.ui.ac.id/kuliah/POK/

  2. REVIEW: Loop (2) • thefollowing pseudo-code: sum = 0; for ( i=0; i<10; i++ ) sum += i; • could be translated into assembly as: • mov eax, 0; eax is sum • mov ecx, 0; ecx is i • loop_start: • add eax, ecx • inc ecx • cmp ecx,10 • jne loop_start • implementasi dgn loop: (salah satu alternatif) • mov eax, 0; eax is sum • mov ecx, 10; ecx is (10-i) • mov edx, 0 ; edx is i • loop_start: • add eax, edx • inc edx • loop loop_start

  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 destroyed get_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

  4. SubPrograms/SubRoutines/Functions

  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); ... }

  6. Use of CALL & RET in Subroutine Invocation _add_scale: ... mov eax,[ebp+16] imul eax,[ebp+12] addeax,[ebp+8] ... ret _main: ... L4: cmp [ebp-44],2 ; cmp(i, 2) jle L7 ; i < 2 jmp L5 L7: mov eax,[ebp-40] ; scale push eax ... mov eax,[eax+edx] ; bvec[i] push eax ... mov eax,[eax+edx] ; avec[i] push eax call _add_scale ... mov [edx+ecx],eax; cvec[i] ...

  7. Parameter Passing between Calling- & Callee-subroutines

  8. Passing Parameters • By Value • By Reference

  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); ... }

  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! */ ... }

  11. Implementation of Passing Parameters • Via Register • Via Stack

  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

  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,0 L1: 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

  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 pointer or, Intel has provided EBP register for this purpose mov ebx,[ebp+4] ; ebp as pointer • To store local variables func() { int x, y; ... }

  15. Stacks as Storage for Local Variables

  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)

  17. General Form of a Subroutine 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

  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; }

  19. Example: calc_sum.asm cal_sum: push ebp mov ebp, esp sub esp, 4 mov dword [ebp - 4], 0; sum = 0 mov ebx, 1; i = 1 for_loop: cmp ebx, [ebp+12]; is i >= n? jnle end_for add [ebp-4], ebx; sum += i inc ebx; i++ jmp short for_loop end_for: mov ebx, [ebp+8]; ebx = sump mov eax, [ebp-4]; eax = sum mov [ebx], eax; *sump = sum mov esp, ebp pop ebp ret

  20. General Form of a Subroutine using Enter/Leave subprogram_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

  21. Passing Parameters via Stack Frames

  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); ... }

  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]]

  24. Passing Values via Stack Frames (2/2) _main: L4: cmp [ebp-44],2 ; cmp(i, 2) jle L7 ; i < 2 jmp L5 L7: mov eax,[ebp-40] ; scale push eax ... lea eax,[4*edx] lea edx,[ebp-24] ; bvec mov eax,[eax+edx] ; bvec[i] push eax ... lea eax,[4*edx] lea edx,[ebp-12] ; avec mov eax,[eax+edx] ; avec[i] push eax call _add_scale ... lea edx,[4*ecx] lea ecx,[ebp-36] ; cvec mov [ecx+edx],eax ; cvec[i] ... inc [ebp-44] ; i += 1 jmp L4 L5: _add_scale: push ebp mov ebp,esp sub esp,24 mov eax,[ebp+16] imuleax,[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 ret

  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); ... }

  26. Passing References via Stack Frames (2/2) _v_add_scale: ... L4: cmp [ebp-4],2 jle L7 jmp L5 L7: mov eax,[ebp+20] ; scale push eax mov eax,[ebp-4] lea edx,[4*eax] mov eax,[ebp+12] ; BVEC mov edx,[eax+edx] ; BVEC[i] push edx ... mov eax,[ebp+8] ; AVEC mov edx,[eax+edx] ; AVEC[i] push edx call _add_scale ... mov edx,[ebp+16] mov [edx+ecx],eax inc [ebp-4] jmp L4 L5: ... ret _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

  27. Multi-module Programs

  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 madeto each label in one module (i.e. object file) to its definition in anothermodule • Calling-module declare the called-label “extern” • Called-module declare the called-label “global”

  29. Example • AddScale.asm: global _add_scale _add_scale: ... mov eax,[ebp+16] imul eax,[ebp+12] addeax,[ebp+8] ... ret • Main.asm: global _main extern _add_scale _main: ... L4: ... mov eax,[eax+edx] ; bvec[i] push eax ... mov eax,[eax+edx] ; avec[i] push eax call _add_scale

  30. On Pop-Quiz #1

  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?

  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

  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

More Related