Tugas 3 oganisasi komputer 23510310

648 views
562 views

Published on

Pemrograman C dan Assembly di Linux.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
648
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Tugas 3 oganisasi komputer 23510310

  1. 1. Tugas 3 11516 Organisasi Sistem KomputerMachine Level Programming and Assembly Language oleh : I Putu Agus Eka Pratama 23510310 Dosen : Dr. Kusprasapta Mutijarsa Magister Teknologi InformasiSekolah Teknik Elektro dan Informatika Institut Teknologi Bandung 2011
  2. 2. 1. Soal :Practice problem 3.1Assume the following values are stored at the indicated memory addresses and registers : Address Value 0x100 0xFF 0x104 0xAB 0x108 0x13 0x10C 0x11 Address Value %eax 0x100 %ecx 0x1 %edx 0x3Fill the following table showing the values for the indicated operands : Operand Value %eax 0x104 0xAB $0x108 (%eax) 4(%eax) Operand Value 9(%eax,%edx) 260(%ecx,%edx) 0xFC(, %ecx,4) (%eax,%edx,4)Jawab : Operand Value Keterangan%eax 0x100 Nilai dari %eax adalah 0x100 (register).0x104 0xAB Nilai dari register 0x104 adalah 0xAB (absolut address).$0x108 0x108 Tanda $ menyatakan bahwa data integer konstan sehingga data tetap (immediate).(%eax) 0xFF %eax = 0x100, sehingga value 0xFF (address 0x100).
  3. 3. 4(%eax) 0xAB 0x104 = 0x100 + 4 dan value 0xAB (address 0x104). Operand Value Keterangan9(%eax,%edx) 0x11 0x10C = (0x9 + 0x3) + 0x100 = C + 0x100 , value 0x11 (address 0x10C).260(%ecx,%edx) 0x13 260 = 0x104. 0x104 + 0x1 + 0x3 = 0x108, value 0x13 (adress 0x108).0xFC(, %ecx,4) 0xFF 0xFC + (1*4). 0xFC + 0x4 = 0x100 (address 0x100).(%eax,%edx,4) 0x11 (0x1 * 4) + 0x100 = 0x10C, value 0x11 (address 0x10C). 2. Soal :[Practice Problem 3.2]You are given the following information. A function with prototype void decode1(int *xp, int *yp, int*zp); is compiled into assembly code. The body of the code is as follows:1. movl 8(%ebp), %edi2. movl 12(%ebp), %ebx3. movl 16(%ebp), %esi4. movl (%edi), %eax5. movl (%ebx), %edx6. movl (%esi), %ecx7. movl %eax, (%ebx)8. movl %edx, (%esi)9. movl %ecx, (%edi)Parameters xp, yp, and zp are stored at memory locations with offsets 8, 12, and 16, respectively,relative to the address in register %ebp.Write C code for decode1 that will have an effect equivalent to the assembly code above.You can test your answer by compiling your code with the –S switch.Your compiler may generate code that differs in the usage of registers or the ordering of memoryreferences, but is should still be functionally equivalent.Jawab :Code Assembly :movl 8(%ebp), %edimovl 12(%ebp), %ebx
  4. 4. movl 16(%ebp), %esimovl (%edi), %eaxmovl (%ebx), %edxmovl (%esi), %ecxmovl %eax, (%ebx)movl %edx, (%esi)movl %ecx, (%edi)void decode1(int *xp, int *yp, int *zp);Code C :void decode1(int *xp, int *yp, int *zp){int tx = *xp;int ty = *yp;int tz = *zp;*yp = tx;*zp = ty;*xp = tz;}Pembuktian :Membuat file jawabansoalno2.cputu-shinoda@my-machine:~$ touch jawabansoalno2.cMengisikan script C ke dalam file jawabansoalno2.cputu-shinoda@my-machine:~$ nano jawabansoalno2.cKode C yang dimasukkanvoid decode1(int *xp, int *yp, int *zp){int tx = *xp;int ty = *yp;int tz = *zp;*yp = tx;*zp = ty;*xp = tz;}Ditampilkan pada gambar berikut
  5. 5. Kemudian dilanjutkan mencompile file jawabansoalno2.c dengan opsi -Sputu-shinoda@my-machine:~$ gcc -O -S jawabansoalno2.cTerbentuk file jawabansoalno2.s dengan isi file sebagai berikut
  6. 6. 3. Soal :[Practice Problem 3.3]Suppose register %eax holds value x and %ecx holds value y. Fill in the table below with formulasindicating the value that will be stored in register %edx for each of the following assembly codeinstructions. Expression Result leal 6(%eax), %edx leal (%eax, %ecx), %edx leal (%eax, %ecx,4) Expression Result leal 7(%eax, %eax,8), %edx leal 0xA(,%ecx, 4), %edx leal 9(%eax, %ecx,2),%edxJawab : Expression Result Keteranganleal 6(%eax), %edx 6+x %edx = 6 + x.leal (%eax, %ecx), %edx X+y %edx = x + y.leal (%eax, %ecx,4), %edx X + 4y %edx = x + 4y. Expression Result Keteranganleal 7(%eax, %eax,8), %edx 7 + 9x %edx = 7 + x + (8*x) = 7 + 9x.leal 0xA(,%ecx, 4), %edx 10 + 4y 0xA = 10. %edx = 10 + (y*4) = 10 + 4y.leal 9(%eax, %ecx,2), %edx 9 + x + 2y %edx = 9 + x + (2*y) = 9 + x + 2y. 4. Soal :[Practice Problem 3.4]Assume the following values are stored at the indicated memory addresses and registers : Address Value 0x100 0xFF 0x104 0xAB 0x108 0x13
  7. 7. 0x10C 0x11 Address Value %eax 0x100 %ecx 0x1 %edx 0x3Fill in the following table showing the effects of the following instructions, both in terms of theregister or memory location that will be updated and the resulting value. Instruksi Tujuan Nilaiadd1 %ecx, (%eax) 0x100sub1 %edx, 4 (%eax)Imu11 $16 (%eax, %edx, 4)Inc1 8 (%eax)dec1 %ecxsub1 %edx, %eaxJawab : Instruksi Tujuan Nilaiadd1 %ecx, (%eax) 0x100 0x100sub1 %edx, 4 (%eax) 0x104 0xA8Imu11 $16 (%eax, %edx, 4) 0x10C 0x110Inc1 8 (%eax) 0x108 0x14dec1 %ecx %ecx 0x0sub1 %edx, %eax %eax 0xFD 5. Soal :[Problem 3.31]You are given the information that follows. A function with prototype void decode2(int x, int y, intz); is compiled into assembly code. The body of the code is as follows:1.movl 16(%ebp), %eax2.movl 12(%ebp), %edx3.sub %eax, %edx;4.movl %edx, %eax5.imull 8(%ebp), %edx6.sall $31, %eax7.sarl $31, %eax8.xorl %edx, %eax
  8. 8. #C Codeint decode2(int x, int y, int z){int t1 = y - z;int t2 = x * t1;int t3 = (t1 << 31) >> 31;int t4 = t3 ˆ t2;return t4;}Parameters x, y, and z are stored at memory locations with offsets 8, 12, and 16 relative to theaddress in register %ebp. The code stores the return value in register %eax.Write C code for decode2 that will have an effect equivalent to the assembly code. You can test youranswer by compiling your code with the –S switch. Your compiler may not generate identical code,but it should be functionally equivalent.Jawab :Membuat file jawabansoalno5.cputu-shinoda@my-machine:~$ touch jawabansoalno5.cMengisikan script C ke dalam file jawabansoalno5.cputu-shinoda@my-machine:~$ nano jawabansoalno5.cKode C yang dimasukkanint decode2(int x, int y, int z){int t1 = y - z;int t2 = x * t1;int t3 = (t1 << 31) >> 31;int t4 = t3 ˆ t2;return t4;}Ditampilkan pada gambar berikut
  9. 9. Kemudian dilanjutkan mencompile file jawabansoalno5.c dengan opsi -Sputu-shinoda@my-machine:~$ gcc -O -S jawabansoalno5.cTerbentuk file jawabansoalno5.s dengan isi file sebagai berikut

×