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

Slides:



Advertisements
Similar presentations
1 IKI10230 Pengantar Organisasi Komputer Bab 5.3: On-line Storage 23 April 2003 Bobby Nazief Qonita Shahab bahan.
Advertisements

Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. A6: Bahasa Rakitan AVR I/O Instructions 4 April 2003 Bobby Nazief Qonita Shahab.
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.a: Data Transfer Instructions Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer.
Toward a Compiler Framework for Thread-Level Speculation Marcelo Cintra University of Edinburgh
1 IKI20210 Pengantar Organisasi Komputer Kuliah No. 19: I/O, Interupsi 20 November 2002 Bobby Nazief Johny Moningka
1 IKI20210 Pengantar Organisasi Komputer Kuliah No. 19: I/O, Interupsi pada AVR 22 November 2002 Bobby Nazief Johny Moningka
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. A7: Bahasa Rakitan AVR Interrupt Handler 25 April 2003 Bobby Nazief Qonita Shahab.
MKU BAHASA INGGRIS II TEKNIK INFORMATIKA, FTI YUSI ASNIDAR, S.Pd, M.Hum. MODUL 8 What do you see in this picture? Make a good paragraph! …………………………………………………………………………………………
Ch13QQ T F 1. The first step in the program development life cycle (PDLC) is problem analysis. T F 2. The use of structured programming typically.
IKI10230 Pengantar Organisasi Komputer Bab 10: Micro Architecture
COMP3221 lec23-decode.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 23: Instruction Representation; Assembly and Decoding.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Searching Prudence Wong
CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Lecture 8: MIPS Instruction Set
1 IKI20210 Pengantar Organisasi Komputer Kuliah Minggu ke-5c: Prosesor 11 Oktober 2002 Bobby Nazief Johny Moningka
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.c: Logical Operations Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 07: CALL, RET, Stacks Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
1 IKI10230 Pengantar Organisasi Komputer Bab 6: Aritmatika 7 & 14 Mei 2003 Bobby Nazief Qonita Shahab bahan kuliah:
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 11: Control Unit Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization, ed-5.
1 IKI20210 Pengantar Organisasi Komputer Kuliah no. 25: Pipeline 10 Januari 2003 Bobby Nazief Johny Moningka
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 09: Compiling-Assembling-Linking Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer.
1 IKI10230 Pengantar Organisasi Komputer Bab 7: Control Unit 28 Mei 2003 Bobby Nazief Qonita Shahab bahan kuliah:
Chapter 2- Visual Basic Schneider1 Chapter 2 Problem Solving.
Chapter 2- Visual Basic Schneider1 Chapter 2 Problem Solving.
1 Algorithm…. 2 Algorithm: Is a planned set of steps to solve certain problem Ex.: Assume for instance that the gross pay of an employee is to be calculated.
1 IKI10230 Pengantar Organisasi Komputer Bab 4.1: Input/Output & Interrupt 19 Maret 2003 Bobby Nazief Qonita Shahab
1 IKI20210 Pengantar Organisasi Komputer Kuliah No. 21: Peripheral 29 Nopember 2002 Bobby Nazief Johny Moningka
ChE 117 Motivation Lots of Tools Can’t always use “Canned Programs”
1 IKI20210 Pengantar Organisasi Komputer Kuliah No. 24: Aritmatika 3 Januari 2003 Bobby Nazief Johny Moningka
computer
Lecture 15 Today’s lecture MARIE programming Assembler
Pre-Algebra Unit 1 Review. Unit 1 Review 1)Name the property demonstrated. a) a + b = b + a b) a(b + c) = ab + ac.
1 IKI10230 Pengantar Organisasi Komputer Bab 4.2: DMA & Bus 26 Maret 2003 Bobby Nazief Qonita Shahab bahan kuliah:
1 IKI20210 Pengantar Organisasi Komputer Kuliah No. 20: DMA & Standard I/O Bus 27 November 2002 Bobby Nazief Johny Moningka
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.b: Arithmetic Operations Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 06: Control Structures Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
1 IKI20210 Pengantar Organisasi Komputer Kuliah No. 23: Aritmatika 18 Desember 2002 Bobby Nazief Johny Moningka
Lecture 0Slide 1 Welcome to IKI 10230I Introduction to Computer Organization Teacher: L. Yohanes Stefanus office: Fasilkom Building.
亚洲的位置和范围 吉林省白城市洮北区教师进修学校 郑春艳. Q 宠宝贝神奇之旅 —— 亚洲 Q 宠快递 你在网上拍的一套物理实验器材到了。 Q 宠宝贝打电话给你: 你好,我是快递员,有你的邮件,你的收货地址上面 写的是学校地址,现在学校放假了,能把你家的具体 位置告诉我吗? 请向快递员描述自己家的详细位置!
Chapter 2- Visual Basic Schneider1 Programming Languages: Machine Language Assembly Language High level Language.
Chapter Overview General Concepts IA-32 Processor Architecture
Entry Ticket: High and Low Level Languages
Challenges CPU performance Variable density Multi-thread computing
CS 301 Fall 2001 – Chapter 3 Slides by Prof. Hartman, following “IBM PC Assembly Language Programming” by Peter Abel 9/17/2018.
Computer Software CS 107 Lecture 2 September 1, :53 PM.
ECE 445 CS1251 Computer Organization Carl Hamacher
Pick up the handout on your way in!!
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms
Review for Test1.
Computer Science 210 Computer Organization
In Natural Language (loosely adopted from Behforooz, A.)
CS-401 Computer Architecture & Assembly Language Programming
University of Gujrat Department of Computer Science
MARIE: An Introduction to a Simple Computer
Overheads for Computers as Components 2nd ed.
CS-401 Assembly Language Programming
CS6020 Advanced Assembler Project
Overheads for Computers as Components 2nd ed.
CS 286 Computer Organization and Architecture
Engine Part ID Part 1.
Engine Part ID Part 2.
Engine Part ID Part 2.
LANGUAGE EDUCATION.
CPSC 171 Introduction to Computer Science
Copyright © 2013 Elsevier Inc. All rights reserved.
COS 151 Bootcamp – Week 4 Department of Computer Science
Computer Programming Tutorial
Presentation transcript:

1 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 L. Yohanes Stefanus Bobby Nazief bahan kuliah:

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 sum 2. mov ecx, 0; ecx is i 3.loop_start: 4. add eax, ecx 5. inc ecx 6. cmp ecx,10 7. jne loop_start °implementasi dgn loop: (salah satu alternatif) 1. mov eax, 0; eax is sum 2. mov ecx, 10; ecx is (10-i) 3. mov edx, 0; edx is i 4.loop_start: 5. add eax, edx 6. inc edx 7.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 [ESP][EBP] [ESP+4][EIP] [ESP+8][input] [ESP+12][edx]

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] add eax,[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 1.By Value 2.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 ESPLocVar-m... EBP-4LocVar-1 EBP[EBP] [EIP caller ] EBP+8Par-1... Par-n

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 ESP = EBP-4Sum EBP[EBP] [EIP caller ] EBP+8sump EBP+12N

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 °LEAREG,MEMLOC; REG  MEMLOC LEAEAX,[VAR1]; EAX  VAR1 -MOVEAX,VAR1 LEAEAX,[EBX+ESI]; EAX  [EBX] + [ESI] °Bandingkan dengan: MOVEAX,[VAR1]; EAX  [VAR1] MOVEAX,[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] 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 ret EBP  [EBP] [EIP] EBP+8  Avec[i] EBP+12  Bvec[i] EBP+16  Scale

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) _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],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

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

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 _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? 0xBFFD00h 0xBFFE34h 0xBFFF12h 0xC00000h 0xC00143h 0xC00212h 0xC00300h 0xC00400h

32 No. 4 °Var i = 1001b (integer 4-bit) dan j = b (integer 8- bit), keduanya menggunakan representasi komplemen-2. Hitung k = i + j! (nyatakan dalam biner) i = 1001 = = -7 j = = 16 k = = 9

33 No. 5 °Perhatikan bagian program berikut: segment.data var_add h var_adb 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 addesi,2; esi = 1003 sub[esi],al; [esi] lama = 44, al = 11 ; [esi] baru = 33 ; [var_a] = 11, Var_a = 0x100011h 0x100133h 0x100233h 0x100333h