Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 03: Sistem Bilangan Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,

Presentasi serupa


Presentasi berjudul: "1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 03: Sistem Bilangan Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,"— Transcript presentasi:

1 1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 03: Sistem Bilangan Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization, ed-5 3. Materi kuliah CS61C/2000 & CS152/1997, UCB 25 Februari 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 2 Penggunaan Tools: GCC, NASM, EDEBUG32

3 3 C Compiler: GCC °Fungsi menerjemahkan program dalam bahasa ‘C’ ke bahasa mesin °Kompilasi gcc [OPSI] -–c: menghasilkan.o (object-file) -–S: menghasilkan.s (assembly-file); format ≠ NASM -–g (default): menghasilkan informasi untuk debug Cara #1: -gcc –c myprog.c  menghasilkan myprog.o -gcc –o myprog.exe myprog.o Cara #2: -gcc –o myprog.exe myprog.c °Contoh: hello.c  hello.exe Cara #1: -gcc –c hello.c  hello.o -gcc –o hello.exe hello.o Cara #2: -gcc –o hello.exe hello.c

4 4 hello.c int main() { printf("hello world\n"); }

5 5 Assembler: NASM °Fungsi menerjemahkan program dalam bahasa rakitan ke bahasa mesin °Perakitan nasm [OPSI] -–f OBJ-TYPE –f coff: menghasilkan format COFF (.o)  digunakan oleh DJGPP –f obj (default): menghasilkan format OMF (.obj) -–g (default): menghasilkan informasi untuk debug nasm –f coff myprog.asm  myprog.o gcc –o myprog.exe myprog.o  memungkinkan pengintegrasian dengan object-file yang bersumber dari bahasa C °Contoh: hello.asm  hello.exe nasm –f coff hello.asm  hello.o gcc –o hello.exe driver.c hello.o

6 6 hello.asm extern _printf segment.data the_strdb "hello world", 10, 0 segment.bss segment.text global _asm_main _asm_main: enter 0,0 pushdword the_str call_printf; printf(“hello world\n”) popeax leave ret

7 7 driver.c int asm_main( void ); int main() { int ret_status; ret_status = asm_main(); return ret_status; }

8 8 Debuger: EDEBUG32 °Fungsi: men-debug program (.exe) eksekusi program secara ‘single-step’ (per instruksi) eksekusi program sampai dengan instruksi tertentu (breakpoint) evaluasi register & memori °Persyaratan sebelumnya program harus di-kompilasi/rakit dengan ‘–g’ °Cara Menjalankan: edebug32

9 9 EDEBUG32 Commands °go ggo, stop at °contccontinue execution °stepsstep through current instruction °nextnstep to next instruction °listl ulist instructions (takes addr, count) °dumpddump memory (takes addr, count) °printpprint value of expression (takes expr) °breakbset breakpoint (takes which, addr) °statusbreakpoint status °regsrprint registers °setset register/memory °npxdisplay 80387 contents °wheredisplay list of active functions °whereisfind a symbol/location (takes wildcard or value) °clsclear screen °helph,?print help °quitqquit

10 10 tugas0a.asm (1/2) 1.segment.data 2.data1db 11h 3.data2dw 2222h 4.data3dd 33333333h 5.datatmptimes 9 db 0ffh 6.data4times 16 db 0 7.segment.bss 8.stacksresd 1 9.segment.text 10. global _asm_main 11._asm_main: 12. enter 0,0 13. pusha 14. mov eax,10; decimal number, value = 10 15. mov ebx,10b; binary number, value = 2 16. mov ecx,10h; hexadecimal number, value = 16 17. mov edx,eax; register-to-register transfer

11 11 tugas0a.asm (2/2) 18.xor eax,eax 19.xor ebx,ebx 20.xor ecx,ecx 21.xor edx,edx 22.xor esi,esi 23.xor edi,edi 24. mov esi,data1; esi points to data1 25. mov al,[esi]; load 1 byte 26.mov bx,[esi]; load 1 word (2 bytes) 27. mov ecx,[esi]; load 1 double-word (2 words = 4 bytes) 28.mov edx,[data1] 29.mov esi,[data2] 30.mov edi,[data3] 31.mov [data4],dl; store 1 byte 32.mov [data4],dx; store 1 word 33.mov [data4],edx; store 1 double-word 34. popa 35.leave 36.ret

12 12 Tugas #0 (1/4) °Merakit tugas0a.asm nasm –f coff tugas0a.asm nasm –f coff asm_io.asm gcc –o tugas0a.exe driver.c tugas0a.o asm_io.o °Debug tugas0a.exe edebug32 tugas0a.exe >> >> g asm_main eax=00000000 ebx=000002a5 ecx=00000000 edx=0000033f esi=00000054 edi=00010b50 ebp=00090b30 UP IE PL NZ AC PE NC ds=01f7 es=01f7 fs=01e7 gs=0207 ss:esp=01f7:00090b14 cs=01ef _asm_main(): 000015e0: c8000000 enter 0,0 >> l _asm_main(): 000015e0: c8000000 enter 0,0 000015e4: 60 pusha 000015e5: b80a000000 mov eax,0xa 000015ea: bb02000000 mov ebx,0x2 000015ef: b910000000 mov ecx,0x10 000015f4: 89c2 mov edx,eax 000015f6: 31c0 xor eax,eax 000015f8: 31db xor ebx,ebx 000015fa: 31c9 xor ecx,ecx

13 13 Tugas #0 (2/4) °Debug tugas0a.exe (lanjutan...) >> s 000015e4: 60 pusha >> r eax=00000000 ebx=000002a5 ecx=00000000 edx=0000033f esi=00000054 edi=00010b50 ebp=00090b10 UP IE PL NZ AC PE NC ds=01f7 es=01f7 fs=01e7 gs=0207 ss:esp=01f7:00090b10 cs=01ef 000015e4: 60 pusha >> s 000015e5: b80a000000 mov eax,0xa >> s 000015ea: bb02000000 mov ebx,0x2 >> r eax=0000000a ebx=000002a5 ecx=00000000 edx=0000033f esi=00000054 edi=00010b50 ebp=00090b10 UP IE PL NZ AC PE NC ds=01f7 es=01f7 fs=01e7 gs=0207 ss:esp=01f7:00090af0 cs=01ef 000015ea: bb02000000 mov ebx,0x2

14 14 Tugas #0 (3/4) °Debug tugas0a.exe (lanjutan...) >> s >>... eax=00000011 ebx=00002211 ecx=33222211 edx=33222211 esi=33332222 edi=33333333 ebp=00090b10 UP IE PL ZR PE NC ds=01f7 es=01f7 fs=01e7 gs=0207 ss:esp=01f7:00090af0 cs=01ef 00001620: 8815e0c80000 mov [__what_size_app_thinks_it_is+26],dl dl=11 si=2222  mov [data4],dl; store 1 byte >> d __what_size_app_thinks_it_is+26 0x0000c8e0: 0x00000000 0x00000000 0x00000000 0x00000000 >> s 00001626: 668915e0c80000 mov [__what_size_app_think_thinks_it_is+26],dx... >> d 0xc8e0 0x0000c8e0: 0x00000011 0x00000000 0x00000000 0x00000000

15 15 Tugas #0 (4/4) °Merakit tugas0b.asm nasm –f coff tugas0b.asm gcc –o tugas0b.exe driver.c tugas0b.o asm_io.o °Eksekusi tugas0b.exe bandingkan hasil ‘print-out’ dengan evaluasi isi register & memori dengan menggunakan EDEBUG32! °Laporan tugas0a  rangkuman hasil pengamatan register & memori hasil perbandingan hasil pengamatan dengan ‘print-out’ tugas0b Baris ke-EAXEBX...0xC8E0... 120 140xA0 150x2 16

16 tugas0b.asm (1/3) 1.%include "asm_io.inc" 2.segment.data 3.[same as tugas0a.asm] 4.segment.bss 5.[same as tugas0a.asm] 6.segment.text 7. global _asm_main 8._asm_main: 9. enter 0,0 10. pusha 11. dump_regs 1; initial condition 12. mov eax,10 13. mov ebx,10b 14. mov ecx,10h 15. mov edx,eax 16. dump_regs 2; watch changes in eax, ebx, ecx, & edx

17 17 tugas0b.asm (2/3) 17.xor eax,eax 18.xor ebx,ebx 19.xor ecx,ecx 20.xor edx,edx 21.xor esi,esi 22.xor edi,edi 23.dump_regs 3; eax, ebx, ecx, edx, esi, & edi should be 0 24.dump_mem 1,data1,0; initial condition of [data1] 25.dump_mem 2,data2,0; [data2] 26.dump_mem 3,data3,0; [data3] 27. mov esi,data1 28. mov al,[esi] 29.mov bx,[esi] 30. mov ecx,[esi] 31. mov edx,[data1] 32.mov esi,[data2] 33.mov edi,[data3] 34.dump_regs 4; watch changes in eax, ebx, ecx, edx, esi, & edi

18 18 tugas0b.asm (3/3) 35.dump_mem 4,data4,0; initial condition of [data4] 36.mov [data4],dl 37.dump_mem 5,data4,0; watch changes in [data4] 38.mov [data4],dx 39.dump_mem 6,data4,0 ; watch changes in [data4] 40.mov [data4],edx 41.dump_mem 7,data4,0 ; watch changes in [data4] 42. popa 43. mov eax, 0 44. leave 45. ret

19 19 REVIEW: Stored Program Computer

20 20 Review: Bit dapat mepresentasikan “apa saja” !!! °Bits dapat merepresentasikan apapun! Karakter? Latin: -26 huruf => 5 bits -Huruf besar/kecil + tanda lain => 7 bits, berapa simbol huruf? -Karakter, bahasa lain => 16 (unicode) Logical values? -0 -> False, 1 => True Warna ? Berapa banyak warna => berapa bits? Alamat? (berapa karakter alfabet..) °.. Tapi N bits  hanya dapat merepresentasikan 2 N sesuatu

21 21 Review: Bit  Instruksi °Instruksi (Operasi) dapat direpresentasikan oleh bit. Contoh: -0 => tepuk tangan -1 => bersiul -Eksekusi Instruksi: 1.0 2.1 3.1 4.0 5.0 6.1 7.0

22 22 Review: Kumpulan bit disimpan di memori °Memori adalah tempat menyimpan kumpulan bit (instruksi/data) °Suatu “word” adalah sejumlah bit data tetap, (mis. 16, atau 32 bit) pada satu lokasi di memori Byte-addressable memory menyimpan data multi-byte pada lokasi memori yang berurutan °Alamat menunjuk ke lokasi “word” (byte-1) disimpan. Alamat dapat direpresen-tasikan oleh bit Alamat juga sebagai “bilangan” (yang dapat dimanipulasikan) 101101100110 00000 11111 = 2 k - 1 01110 Alamat data

23 23 00846 21686 40 0 6 1 60 0 1 7 80 0 0 0 100 0 0 0 120 0 0 0 140 0 0 0 160 0 0 0 180 0 0 0 00846 21686 40 0 6 1 60 0 1 7 80 0 7 8 100 0 0 0 120 0 0 0 140 0 0 0 160 0 0 0 180 0 0 0 Review: Stored-program Computer Processor (active) Control (“brain”) Datapath (“brawn”) 0846 0061 0017 0078 00846 21686 40 0 6 1 60 0 6 1 80 0 7 8 100 0 0 0 120 0 0 0 140 0 0 0 160 0 0 0 180 0 0 0 1686 0078 0017 0061 IP  komputer dapat diprogram untuk memenuhi kebutuhan pengguna dengan jalan mengisi memori dengan instruksi & data yang sesuai  operasi yang dilakukan oleh komputer ditentukan oleh instruksi & data yang tersimpan di memori

24 24 Representasi Data

25 25 Bilangan Biner °Simbol: 0,1 °Harga/Nilai suatu bilangan biner: 1011010 = 1x2 6 + 0x2 5 + 1x2 4 + 1x2 3 + 0x2 2 + 1x2 1 + 0x2 0 = 64 + 16 + 8 + 2 = 90 Penulisan: 1011010b °Konversi: Desimal  Biner 90 / 2 = 45sisa 0 45 / 2 = 22sisa 1 22 / 2 = 11sisa 0 11 / 2 = 5sisa 1 5 / 2 = 2sisa 1 2 / 2 = 1sisa 0 1 / 2 = 0sisa 1

26 26 Bilangan Heksa-Desimal °Simbol: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F °Harga/Nilai suatu bilangan heksa-desimal: 5A = 5x16 1 + 10x16 0 = 80 + 10 = 90 Penulisan: 5Ah atau 0x5A °Konversi: Desimal  Heksa-desimal 90 / 16 = 5sisa 10 (A) 5 / 16 = 0sisa 5 °Konversi: Heksa-desimal  Biner 5A = 101 1010 °Konversi: Biner  Heksa-desimal 1011010 = 101 1010 = 5 A = 5A

27 27 Tabel Bilangan DesimalBinerHeksaDesimalBinerHeksa 000000810008 100011910019 200102101010A 300113111011B 401004121100C 501015131101D 601106141110E 701117151111F 2k2k NilaiSebutan 2 10 1.0241K 2 11 2.0482K 2 12 4.0964K 2 16 65.53664K 2 20 1.048.5761M

28 28 Pengelompokkan Bit °Bit String: INTELMIPS 4 bitnibblenibble 8 bitbytebyte 16 bitwordhalf-word 32 bitdouble-wordword 64 bitquad-worddouble-word °Alamat lokasi memori umumnya dinyatakan dengan bilangan heksa desimal contoh: -lokasi memori 90 pada memori dengan ruang memori sebesar 64K (65536 = 2 16 ) dinyatakan dengan alamat: 0x005A -jika ruang memori sebesar 2 32 (4G) 0x0000005A

29 29 Penyimpanan data multi-byte (Little Endian) int j = 987700; 987700 = 0x000F1234 = 0000 0000 0000 1111 0001 0010 0011 0100 00000000 00000001 00000002 00000003 00000004 00000005 00000006 00000007 FFFFFFFF 0101 1010 Alamat (32 bit) 0000 0011 0100 0001 0010 0000 1111 0000 int i = 90; 90 = 0x5A = 0000 0000 0000 0000 0000 0000 0101 1010 i j

30 30 Addition of Positive Numbers

31 31 One-Bit Full Adder (1/2) °Example Binary Addition: Carries °Thus for any bit of addition: The inputs are a i, b i, CarryIn i The outputs are Sum i, CarryOut i °Note: CarryIn i+1 = CarryOut i a: 0 0 1 1 b: 0 1 0 1 Sum:1 0 0 0

32 32 One-Bit Full Adder (2/2) °To create one-bit full adder: implement gates for Sum implement gates for CarryOut connect all inputs with same name Sum A B CarryIn CarryOut +

33 33 Ripple-Carry Adders: adding n-bits numbers °Kinerja operasi penjumlahan (dan juga operasi- operasi aritmatika lainnya) akan bergantung pada “besar” unit data dan konfigurasi Adder (Arithmetic & Logical Unit) yang digunakan A0 B0 1-bit FA Sum0 CarryIn0 CarryOut0 A1 B1 1-bit FA Sum1 CarryIn1 CarryOut1 A2 B2 1-bit FA Sum2 CarryIn2 CarryOut2 A3 B3 1-bit FA Sum3 CarryIn3 CarryOut3

34 34 Signed Numbers

35 35 How to Represent Negative Numbers? °So far, unsigned numbers °Obvious solution: define leftmost bit to be sign! 0 => +, 1 => - Rest of bits can be numerical value of number °Representation called sign and magnitude

36 36 Shortcomings of sign and magnitude? °Arithmetic circuit more complicated Special steps depending whether signs are the same or not °Also, Two zeros 0x00000000 = +0 ten 0x80000000 = -0 ten What would it mean for programming? °Sign and magnitude abandoned

37 37 Another try: complement the bits °Example: 7 10 = 00111 2 -7 10 = 11000 2 °Called one’s Complement °Note: postive numbers have leading 0s, negative numbers have leadings 1s. 000000000101111... 11111 1111010000... °What is -00000 ? °How many positive numbers in N bits? °How many negative ones?

38 38 Shortcomings of ones complement? °Arithmetic not too hard °Still two zeros 0x00000000 = +0 ten 0xFFFFFFFF = -0 ten What would it mean for programming? °One’s complement eventually abandoned because another solution was better

39 39 Search for Negative Number Representation °Obvious solution didn’t work, find another °What is result for unsigned numbers if tried to subtract large number from a small one? Would try to borrow from string of leading 0s, so result would have a string of leading 1s With no obvious better alternative, pick representation that made the hardware simple: leading 0s  positive, leading 1s  negative 000000...xxx is >=0, 111111...xxx is < 0 °This representation called two’s complement

40 40 Two’s Complement Number line °2 N-1 non-negatives °2 N-1 negatives °one zero °how many positives? °comparison? °overflow? 00000 00001 00010 11111 11110 10000 01111 10001 0 1 2 -2 -15 -16 15............

41 41 Two’s Complement Numbers 0000... 0000 0000 0000 0000 two = 0 ten 0000... 0000 0000 0000 0001 two = 1 ten 0000... 0000 0000 0000 0010 two = 2 ten... 0111... 1111 1111 1111 1101 two = 2,147,483,645 ten 0111... 1111 1111 1111 1110 two = 2,147,483,646 ten 0111... 1111 1111 1111 1111 two = 2,147,483,647 ten 1000... 0000 0000 0000 0000 two = –2,147,483,648 ten 1000... 0000 0000 0000 0001 two = –2,147,483,647 ten 1000... 0000 0000 0000 0010 two = –2,147,483,646 ten... 1111... 1111 1111 1111 1101 two =–3 ten 1111... 1111 1111 1111 1110 two =–2 ten 1111... 1111 1111 1111 1111 two =–1 ten °One zero, 1st bit is called sign bit but one negative with no positive –2,147,483,648 ten

42 42 Two’s Complement Formula °Can represent positive and negative numbers in terms of the bit value times a power of 2: d 31 x -2 31 + d 30 x 2 30 +... + d 2 x 2 2 + d 1 x 2 1 + d 0 x 2 0 °Example 1111 1111 1111 1111 1111 1111 1111 1100 two = 1x-2 31 +1x2 30 +1x2 29 +... +1x2 2 +0x2 1 +0x2 0 = -2 31 + 2 30 + 2 29 +... + 2 2 + 0 + 0 = -2,147,483,648 ten + 2,147,483,644 ten = -4 ten °Note: need to specify width: we use 32 bits

43 43 Two’s complement shortcut: Negation °Invert every 0 to 1 and every 1 to 0, then add 1 to the result Sum of number and its one’s complement must be 111...111 two 111...111 two = -1 ten Let x’ mean the inverted representation of x Then x + x’ = -1  x + x’ + 1 = 0  x’ + 1 = -x °Example: -4 to +4 to -4 x : 1111 1111 1111 1111 1111 1111 1111 1100 two x’: 0000 0000 0000 0000 0000 0000 0000 0011 two +1: 0000 0000 0000 0000 0000 0000 0000 0100 two ()’: 1111 1111 1111 1111 1111 1111 1111 1011 two +1: 1111 1111 1111 1111 1111 1111 1111 1100 two

44 44 Two’s comp. shortcut: Sign extension °Convert 2’s complement number using n bits to more than n bits °Simply replicate the most significant bit (sign bit) of smaller to fill new bits 2’s comp. positive number has infinite 0s 2’s comp. negative number has infinite 1s Bit representation hides leading bits; sign extension restores some of them 16-bit -4 ten to 32-bit: 1111 1111 1111 1100 two 1111 1111 1111 1111 1111 1111 1111 1100 two

45 45 Addition & Subtraction of Signed Numbers

46 46 Addition & Subtraction Operations °Addition: Just add the two numbers Ignore the Carry-out from MSB Result will be correct, provided there’s no overflow 0 1 0 1(+5) +0 0 1 0(+2) 0 1 1 1(+7) 0 1 0 1(+5) +1 0 1 0(-6) 1 1 1 1(-1) 1 0 1 1(-5) +1 1 1 0(-2) 11 0 0 1(-7) 0 1 1 1(+7) +1 1 0 1(-3) 10 1 0 0(+4) 0 0 1 0(+2) 0 0 1 0  0 1 0 0(+4)+1 1 0 0(-4) 1 1 1 0 (-2) 1 1 1 0(-2) 1 1 1 0  1 0 1 1(-5)+0 1 0 1(+5) 10 0 1 1 (+3) °Subtraction: Form 2’s complement of the subtrahend Add the two numbers as in Addition

47 47 Overflow °Examples: 7 + 3 = 10 but... ° - 4 – 5 = - 9 but... 2’s ComplementBinaryDecimal 00000 10001 20010 30011 0000 1111 1110 1101 Decimal 0 -2 -3 40100 50101 60110 70111 1100 1011 1010 1001 -4 -5 -6 -7 1000-8 0111 0011+ 1010 1 1100 1011+ 0111 110 7 3 1 – 6 – 4 – 5 7

48 48 Overflow Detection °Overflow: the result is too large (or too small) to represent properly Example: - 8 < = 4-bit binary number <= 7 °When adding operands with different signs, overflow cannot occur! °Overflow occurs when adding: 2 positive numbers and the sum is negative 2 negative numbers and the sum is positive °Overflow can be detected by evaluating: Carry into MSB  Carry out of MSB 0111 0011+ 1010 1100 1011+ 0111 110 7 3 – 6 –4 – 5 7 0

49 49 Arithmetic & Branching Conditions

50 50 Condition Codes °CC Flags will be set/cleared by arithmetic operations: N (negative): 1 if result is negative (MSB = 1), otherwise 0 C (carry): 1 if carry-out(borrow) is generated, otherwise 0 V (overflow): 1 if overflow occurs, otherwise 0 Z (zero): 1 if result is zero, otherwise 0 0 1 0 1(+5) +1 0 1 0(-6) 1 1 1 1(-1) 0 1 1 1(+7) +1 1 0 1(-3) 10 1 0 0(+4) 0 1 0 1(+5) +0 1 0 0(+4) 1 0 0 1(-7) 0 0 1 1(+3) +1 1 0 1(-3) 10 0 0 0(0)


Download ppt "1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 03: Sistem Bilangan Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,"

Presentasi serupa


Iklan oleh Google