Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 ([email protected]) Bobby Nazief ([email protected]) bahan kuliah: http://www.cs.ui.ac.id/kuliah/POK/ 1 Penggunaan Tools: GCC, NASM, EDEBUG32 2 C Compiler: GCC ° Fungsi • menerjemahkan program dalam bahasa ‘C’ ke bahasa mesin ° Kompilasi • gcc [OPSI] <nama-file> - –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 <other-object-files> • Cara #2: - gcc –o myprog.exe myprog.c <other-object-files> ° 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 3 hello.c int main() { printf("hello world\n"); } 4 Assembler: NASM ° Fungsi • menerjemahkan program dalam bahasa rakitan ke bahasa mesin ° Perakitan • nasm [OPSI] <nama-file> - –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 <other-object-files> 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 5 hello.asm extern _printf segment .data the_str db "hello world", 10, 0 segment .bss segment .text global _asm_main _asm_main: enter 0,0 push call pop dword the_str _printf eax ; printf(“hello world\n”) leave ret 6 driver.c int asm_main( void ); int main() { int ret_status; ret_status = asm_main(); return ret_status; } 7 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 <file-exe> 8 EDEBUG32 Commands ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° go <v> cont step next list dump print break status regs set npx where whereis cls help quit g c s n lu d p b go, stop at <v> continue execution step through current instruction step to next instruction list instructions (takes addr, count) dump memory (takes addr, count) print value of expression (takes expr) set breakpoint (takes which, addr) breakpoint status r print registers set register/memory display 80387 contents display list of active functions find a symbol/location (takes wildcard or value) clear screen h,? print help q quit 9 tugas0a.asm (1/2) 1. 2. 3. 4. 5. 6. segment .data data1 data2 data3 datatmp data4 db 11h dw 2222h dd 33333333h times 9 db 0ffh times 16 db 0 7. 8. segment .bss stacks resd 1 9. 10. 11. 12. 13. segment .text global _asm_main _asm_main: enter 0,0 pusha 14. 15. 16. mov eax,10 mov ebx,10b mov ecx,10h ; decimal number, value = 10 ; binary number, value = 2 ; hexadecimal number, value = 16 17. mov edx,eax ; register-to-register transfer 10 tugas0a.asm (2/2) 18. 19. 20. 21. 22. 23. xor eax,eax xor ebx,ebx xor ecx,ecx xor edx,edx xor esi,esi xor edi,edi 24. 25. 26. 27. 28. 29. 30. mov esi,data1 mov al,[esi] mov bx,[esi] mov ecx,[esi] mov edx,[data1] mov esi,[data2] mov edi,[data3] ; esi points to data1 ; load 1 byte ; load 1 word (2 bytes) ; load 1 double-word (2 words = 4 bytes) 31. 32. 33. mov [data4],dl mov [data4],dx mov [data4],edx ; store 1 byte ; store 1 word ; store 1 double-word 34. 35. 36. popa leave ret 11 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 000015e4: 60 000015e5: b80a000000 000015ea: bb02000000 000015ef: b910000000 000015f4: 89c2 000015f6: 31c0 000015f8: 31db 000015fa: 31c9 enter 0,0 pusha mov eax,0xa mov ebx,0x2 mov ecx,0x10 mov edx,eax xor eax,eax xor ebx,ebx xor ecx,ecx 12 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 mov ebx,0x2 >> s 000015ea: bb02000000 >> 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 13 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 14 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 ke12 14 15 16 EAX 0 0xA EBX ... 0xC8E0 ... 0 0x2 15 tugas0b.asm (1/3) 1. %include "asm_io.inc" 2. 3. segment .data [same as tugas0a.asm] 4. 5. segment .bss [same as tugas0a.asm] 6. segment .text 7. global _asm_main 8. _asm_main: 9. enter 0,0 10. pusha 11. 12. 13. 14. 15. 16. dump_regs 1 mov eax,10 mov ebx,10b mov ecx,10h mov edx,eax dump_regs 2 ; initial condition ; watch changes in eax, ebx, ecx, & edx 16 tugas0b.asm (2/3) 17. 18. 19. 20. 21. 22. xor eax,eax xor ebx,ebx xor ecx,ecx xor edx,edx xor esi,esi xor edi,edi 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. dump_regs 3 ; eax, ebx, ecx, edx, esi, & edi should be 0 dump_mem 1,data1,0 ; initial condition of [data1] dump_mem 2,data2,0 ; [data2] dump_mem 3,data3,0 ; [data3] mov esi,data1 mov al,[esi] mov bx,[esi] mov ecx,[esi] mov edx,[data1] mov esi,[data2] mov edi,[data3] dump_regs 4 ; watch changes in eax, ebx, ecx, edx, esi, & edi 17 tugas0b.asm (3/3) 35. dump_mem 4,data4,0 36. mov [data4],dl 37. dump_mem 5,data4,0 38. mov [data4],dx 39. dump_mem 6,data4,0 40. mov [data4],edx 41. dump_mem 7,data4,0 42. popa 43. mov 44. leave 45. ret ; initial condition of [data4] ; watch changes in [data4] ; watch changes in [data4] ; watch changes in [data4] eax, 0 18 REVIEW: Stored Program Computer 19 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 2N sesuatu 20 Review: Bit Instruksi ° Instruksi (Operasi) dapat direpresentasikan oleh bit. • Contoh: - 0 => tepuk tangan - 1 => bersiul - Eksekusi Instruksi: 1. 2. 3. 4. 5. 6. 7. 0 1 1 0 0 1 0 21 Review: Kumpulan bit disimpan di memori Alamat 00000 data 101101100110 01110 ° 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. 11111 = 2k - 1 • Alamat dapat direpresen-tasikan oleh bit • Alamat juga sebagai “bilangan” (yang dapat dimanipulasikan) 22 Review: Stored-program Computer operasi yang dilakukan oleh komputer ditentukan oleh instruksi & data yang tersimpan di memori IP IP IP 0 2 4 6 8 10 12 14 16 18 0 8 4 6 1 6 8 6 0061 001 67 1 007 08 0 0000 0000 0000 0000 0000 0846 1686 0061 0061 0017 0078 0078 Processor (active) Control (“brain”) Datapath (“brawn”) komputer dapat diprogram untuk memenuhi kebutuhan pengguna dengan jalan mengisi memori dengan instruksi & data yang sesuai 23 Representasi Data 24 Bilangan Biner ° Simbol: 0,1 ° Harga/Nilai suatu bilangan biner: 1011010 = 1x26 + 0x25 + 1x24 + 1x23 + 0x22 + 1x21 + 0x20 = 64 + 16 + 8 +2 = 90 Penulisan: 1011010b ° Konversi: Desimal Biner 90 / 2 = 45 45 / 2 = 22 sisa 0 sisa 1 22 / 2 = 11 11 / 2 = 5 5/2=2 2/2=1 1/2=0 sisa 0 sisa 1 sisa 1 sisa 0 sisa 1 25 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 = 5x161 + 10x160 = 80 + 10 = 90 Penulisan: 5Ah atau 0x5A ° Konversi: Desimal Heksa-desimal 90 / 16 = 5 5 / 16 = 0 sisa 10 (A) sisa 5 ° Konversi: Heksa-desimal Biner 5A = 101 1010 ° Konversi: Biner Heksa-desimal 1011010 = 101 1010 = 5 A = 5A 26 Tabel Bilangan Desimal Biner Heksa Desimal Biner Heksa 0 1 2 3 4 5 6 7 0000 0001 0010 0011 0100 0101 0110 0111 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1000 1001 1010 1011 1100 1101 1110 1111 8 9 A B C D E F 2k 210 211 212 216 220 Nilai Sebutan 1.024 1K 2.048 2K 4.096 4K 65.536 64K 1.048.576 1M 27 Pengelompokkan Bit ° Bit String: • • • • 4 bit 8 bit 16 bit 32 bit • 64 bit INTEL nibble byte word double-word quad-word MIPS nibble byte half-word word double-word ° Alamat lokasi memori • umumnya dinyatakan dengan bilangan heksa desimal • contoh: - - lokasi memori 90 pada memori dengan ruang memori sebesar 64K (65536 = 216) dinyatakan dengan alamat: 0x005A jika ruang memori sebesar 232 (4G) 0x0000005A 28 Penyimpanan data multi-byte (Little Endian) Alamat (32 bit) int i = 90; 90 = 0x5A = i 0000 0000 0000 0000 0000 0000 0101 1010 j int j = 987700; 00000000 00000001 00000002 00000003 00000004 00000005 00000006 00000007 0101 1010 0000 0000 0000 0000 0000 0000 0011 0100 0001 0010 0000 1111 0000 0000 987700 = 0x000F1234 = 0000 0000 0000 1111 0001 0010 0011 0100 FFFFFFFF 29 Addition of Positive Numbers 30 One-Bit Full Adder (1/2) ° Example Binary Addition: a: 0 0 1 1 b: 0 1 0 1 Sum: 1 0 0 0 Carries ° Thus for any bit of addition: • The inputs are ai, bi, CarryIni • The outputs are Sumi, CarryOuti ° Note: CarryIni+1 = CarryOuti 31 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 CarryIn A + B Sum CarryOut 32 Ripple-Carry Adders: adding n-bits numbers CarryIn0 A0 B0 A1 B1 A2 B2 A3 B3 1-bit Sum0 FA CarryIn1 CarryOut0 1-bit Sum1 FA CarryIn2 CarryOut1 1-bit Sum2 FA CarryIn3 CarryOut2 1-bit FA Sum3 CarryOut3 ° Kinerja operasi penjumlahan (dan juga operasioperasi aritmatika lainnya) akan bergantung pada “besar” unit data dan konfigurasi Adder (Arithmetic & Logical Unit) yang digunakan 33 Signed Numbers 34 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 35 Shortcomings of sign and magnitude? ° Arithmetic circuit more complicated • Special steps depending whether signs are the same or not ° Also, Two zeros • 0x00000000 = +0ten • 0x80000000 = -0ten • What would it mean for programming? ° Sign and magnitude abandoned 36 Another try: complement the bits ° Example: 710 = 001112 -710 = 110002 ° Called one’s Complement ° Note: postive numbers have leading 0s, negative numbers have leadings 1s. 00000 00001 ... 01111 10000 ... 11110 11111 ° What is -00000 ? ° How many positive numbers in N bits? ° How many negative ones? 37 Shortcomings of ones complement? ° Arithmetic not too hard ° Still two zeros • 0x00000000 = +0ten • 0xFFFFFFFF = -0ten • What would it mean for programming? ° One’s complement eventually abandoned because another solution was better 38 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 39 Two’s Complement Number line 00000 00001 11111 ° 2N-1 non-negatives N-1 negatives 11110 ° 2 00010 -1 0 1 2 -2 ° one zero . . . . . . ° how many positives? ° comparison? ° overflow? -15 -16 15 10001 10000 01111 40 Two’s Complement Numbers 0000 ... 0000 0000 0000 0000two = 0000 ... 0000 0000 0000 0001two = 0000 ... 0000 0000 0000 0010two = ... 0111 ... 1111 1111 1111 1101two = 0111 ... 1111 1111 1111 1110two = 0111 ... 1111 1111 1111 1111two = 1000 ... 0000 0000 0000 0000two = 1000 ... 0000 0000 0000 0001two = 1000 ... 0000 0000 0000 0010two = ... 1111 ... 1111 1111 1111 1101two = 1111 ... 1111 1111 1111 1110two = 1111 ... 1111 1111 1111 1111two = 0ten 1ten 2ten 2,147,483,645ten 2,147,483,646ten 2,147,483,647ten –2,147,483,648ten –2,147,483,647ten –2,147,483,646ten –3ten –2ten –1ten °One zero, 1st bit is called sign bit • but one negative with no positive –2,147,483,648ten 41 Two’s Complement Formula ° Can represent positive and negative numbers in terms of the bit value times a power of 2: • d31 x -231 + d30 x 230 + ... + d2 x 22 + d1 x 21 + d0 x 20 ° Example 1111 1111 1111 1111 1111 1111 1111 1100two = 1x-231 +1x230 +1x229+... +1x22+0x21+0x20 = -231 + 230 + 229 + ... + 22 + 0 + 0 = -2,147,483,648ten + 2,147,483,644ten = -4ten ° Note: need to specify width: we use 32 bits 42 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...111two 111...111two= -1ten 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 1100two x’: 0000 0000 0000 0000 0000 0000 0000 0011two +1: 0000 0000 0000 0000 0000 0000 0000 0100two ()’: 1111 1111 1111 1111 1111 1111 1111 1011two +1: 1111 1111 1111 1111 1111 1111 1111 1100two 43 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 -4ten to 32-bit: 1111 1111 1111 1100two 1111 1111 1111 1111 1111 1111 1111 1100two 44 Addition & Subtraction of Signed Numbers 45 Addition & Subtraction Operations ° Subtraction: ° Addition: • Just add the two numbers • Ignore the Carry-out from MSB • Result will be correct, provided there’s no overflow • Form 2’s complement of the subtrahend • Add the two numbers as in Addition 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) 0 0 1 0 (+2) 0 1 0 0 (+4) 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) 1 1 1 0 (-2) 1 0 1 1 (-5) 0010 +1 1 0 0 (-4) 1 1 1 0 (-2) 1110 +0 1 0 1 (+5) 10 0 1 1 (+3) 46 Overflow Binary Decimal 2’s Complement 0 0000 0 0000 1 0001 -1 1111 2 0010 -2 1110 3 0011 -3 1101 4 0100 -4 1100 5 0101 -5 1011 6 0110 -6 1010 7 0111 -7 1001 -8 1000 Decimal ° Examples: 7 + 3 = 10 but ... -4 – 5 = -9 ° 0 + but ... 1 1 1 1 0 1 1 1 7 0 0 1 1 3 1 0 1 0 –6 + 1 1 0 0 –4 1 0 1 1 –5 0 1 1 1 7 47 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 0 + 1 1 0 1 1 1 7 0 0 1 1 3 1 0 1 0 –6 + 0 1 1 0 0 –4 1 0 1 1 –5 0 1 1 1 7 48 Arithmetic & Branching Conditions 49 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 0 1 (+5) +0 1 0 0 (+4) 1 0 0 1 (-7) 0 1 1 1 (+7) +1 1 0 1 (-3) 10 1 0 0 (+4) 0 0 1 1 (+3) +1 1 0 1 (-3) 10 0 0 0 (0) 50