90 likes | 240 Views
IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.x: Tugas No. 1. Sumber : 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization , ed-5 3. Materi kuliah CS61C/2000 & CS152/1997, UCB 4. Intel Architecture Software Developer’s Manual. 17 Maret 2004
E N D
IKI10230Pengantar Organisasi KomputerKuliah no. 05.x: Tugas No. 1 Sumber:1. Paul Carter, PC Assembly Language2. Hamacher. Computer Organization, ed-53. Materi kuliah CS61C/2000 & CS152/1997, UCB4. Intel Architecture Software Developer’s Manual 17 Maret 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/
Tugas Pemrograman #1: Vector Add • CVEC = AVEC + Const * BVEC for (i = 0; i < vlength; i++) { CVEC[i] = AVEC[i] + Const * BVEC[i] } • AVEC, BVEC, CVEC, Const, vlength disimpan di memori • Ukuran elemen vektor (AVEC[i], BVEC[i], CVEC[i]), Const, vlength: 4-byte (integer)
Instruksi: ADD & MUL • ADD DEST,SRC ; DEST [SRC] + [DEST] • DEST, SRC: register-/memory-operand • salah satu dari DEST atau SRC harus berupa register-operand • MUL SRC32 ; EDX:EAX [SRC32] * [EAX] • MUL DWORD [DATA] • MUL EBX
Instruksi: JNZ mov ecx,[Counter] ; ecx serves as a counter. L1: ... dec ecx jnz L1; if (ecx != 0) then jump to L1
vector_add.asm %include "asm_io.inc“ segment .data ; initialized data is put in the data segment here segment .bss ; uninitialized data is put in the bss segment segment .text global _asm_main _asm_main: enter 0,0 ; setup routine pusha ; code is put in the text segment. Do not modify the code before ; or after this comment. popa mov eax, 0 ; return back to C leave ret
Kompilasi • nasm –f coff vector_add.asm • gcc –o vector_add.exe driver.c vector_add.o asm_io.o
(Salah Satu) Solusi • %include "asm_io.inc" • segment .data • AVEC dd 0x00111111,0x00222222,0x00333333,0 • BVEC dd 0x01000000,0x02000000,0x03000000,0 • CVEC dd 0,0,0,0 • Const dd 16 • N dd 3 • segment .text • global _asm_main • _asm_main: • enter 0,0 • pusha • mov esi,AVEC ; esi points to vector A. • mov ebx,BVEC ; ebx points to vector B. • mov edi,CVEC ; edi points to vector C. • mov ecx,[N] ; ecx serves as a counter.
(Salah Satu) Solusi • L1: • mov eax,[ebx] ; EAX = BVEC[i] • mul dword [Const] ; EDX:EAX = Const * BVEC[i] • add eax,[esi] ; EAX = Const*BVEC[i] + AVEC[i] • mov [edi],eax ; CVEC[i] = EAX • add ebx,4 ; EBX BVEC[i++] • add esi,4 ; ESI AVEC[i++] • add edi,4 ; EDI CVEC[i++] • dec ecx ; Decrement the counter. • jnz L1 ; Loop again if not done. • popa • mov eax,0 • leave • ret
Pengamatan & Diskusi • Alokasi vektor AVEC, BVEC, dan CVEC • Penggunaan register • sebagai pointer (esi, edi, ebx) • sebagai variabel sementara (eax, ecx, edx) • alokasi Const di memori tidak optimal!