1 / 50

IKI10230 Pengantar Organisasi Komputer Kuliah no. 03: Sistem Bilangan

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

kayla
Télécharger la présentation

IKI10230 Pengantar Organisasi Komputer Kuliah no. 03: Sistem Bilangan

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. IKI10230Pengantar Organisasi KomputerKuliah no. 03: Sistem Bilangan Sumber:1. Paul Carter, PC Assembly Language2. Hamacher. Computer Organization, ed-53. 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. Penggunaan Tools: GCC, NASM, EDEBUG32

  3. 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.exemyprog.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

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

  5. 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

  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 push dword the_str call _printf ; printf(“hello world\n”) pop eax leave ret

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

  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 <file-exe>

  9. EDEBUG32 Commands • go <v> g go, stop at <v> • cont c continue execution • step s step through current instruction • next n step to next instruction • list l u list instructions (takes addr, count) • dump d dump memory (takes addr, count) • print p print value of expression (takes expr) • break b set breakpoint (takes which, addr) • status breakpoint status • regs r print registers • set set register/memory • npx display 80387 contents • where display list of active functions • whereis find a symbol/location (takes wildcard or value) • cls clear screen • help h,? print help • quit q quit

  10. tugas0a.asm (1/2) • segment .data • data1 db 11h • data2 dw 2222h • data3 dd 33333333h • datatmp times 9 db 0ffh • data4 times 16 db 0 • segment .bss • stacks resd 1 • segment .text • global _asm_main • _asm_main: • enter 0,0 • pusha • mov eax,10 ; decimal number, value = 10 • mov ebx,10b ; binary number, value = 2 • mov ecx,10h ; hexadecimal number, value = 16 • mov edx,eax ; register-to-register transfer

  11. tugas0a.asm (2/2) • xor eax,eax • xor ebx,ebx • xor ecx,ecx • xor edx,edx • xor esi,esi • xor edi,edi • mov esi,data1 ; esi points to data1 • mov al,[esi] ; load 1 byte • mov bx,[esi] ; load 1 word (2 bytes) • mov ecx,[esi] ; load 1 double-word (2 words = 4 bytes) • mov edx,[data1] • mov esi,[data2] • mov edi,[data3] • mov [data4],dl ; store 1 byte • mov [data4],dx ; store 1 word • mov [data4],edx ; store 1 double-word • popa • leave • ret

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

  16. tugas0b.asm (1/3) • %include "asm_io.inc" • segment .data • [same as tugas0a.asm] • segment .bss • [same as tugas0a.asm] • segment .text • global _asm_main • _asm_main: • enter 0,0 • pusha • dump_regs 1 ; initial condition • mov eax,10 • mov ebx,10b • mov ecx,10h • mov edx,eax • dump_regs 2 ; watch changes in eax, ebx, ecx, & edx

  17. tugas0b.asm (2/3) • xor eax,eax • xor ebx,ebx • xor ecx,ecx • xor edx,edx • xor esi,esi • xor edi,edi • 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

  18. tugas0b.asm (3/3) • dump_mem 4,data4,0 ; initial condition of [data4] • mov [data4],dl • dump_mem 5,data4,0 ; watch changes in [data4] • mov [data4],dx • dump_mem 6,data4,0 ; watch changes in [data4] • mov [data4],edx • dump_mem 7,data4,0 ; watch changes in [data4] • popa • mov eax, 0 • leave • ret

  19. REVIEW: Stored Program Computer

  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 2Nsesuatu

  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. Review: Kumpulan bit disimpan di memori Alamat • 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) 00000 data 01110 101101100110 11111 = 2k - 1

  23. 0 0 8 4 6 2 1 6 8 6 4 0 0 6 1 6 0 0 1 7 8 0 0 0 0 10 0 0 0 0 12 0 0 0 0 14 0 0 0 0 16 0 0 0 0 18 0 0 0 0 0 0 8 4 6 2 1 6 8 6 4 0 0 6 1 6 0 0 1 7 8 0 0 7 8 10 0 0 0 0 12 0 0 0 0 14 0 0 0 0 16 0 0 0 0 18 0 0 0 0 0 0 8 4 6 2 1 6 8 6 4 0 0 6 1 6 0 0 6 1 8 0 0 7 8 10 0 0 0 0 12 0 0 0 0 14 0 0 0 0 16 0 0 0 0 18 0 0 0 0 Processor (active) Control (“brain”) Datapath (“brawn”) Review: Stored-program Computer • operasi yang dilakukan oleh komputer ditentukan oleh instruksi & data yang tersimpan di memori 0846 IP 1686 IP 0061 IP 0061 0017 0017 0078 0078 • komputer dapat diprogram untuk memenuhi kebutuhan pengguna dengan jalan mengisi memori dengan instruksi & data yang sesuai

  24. Representasi Data

  25. 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 sisa 0 45 / 2 = 22 sisa 1 22 / 2 = 11 sisa 0 11 / 2 = 5 sisa 1 5 / 2 = 2 sisa 1 2 / 2 = 1 sisa 0 1 / 2 = 0 sisa 1

  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 = 5x161+ 10x160 = 80 + 10 = 90 Penulisan: 5Ah atau 0x5A • Konversi: Desimal  Heksa-desimal 90 / 16 = 5 sisa 10 (A) 5 / 16 = 0 sisa 5 • Konversi: Heksa-desimal  Biner 5A = 101 1010 • Konversi: Biner  Heksa-desimal 1011010 = 101 1010 = 5 A = 5A

  27. Tabel Bilangan

  28. Pengelompokkan Bit • Bit String: INTELMIPS • 4 bit nibble nibble • 8 bit byte byte • 16 bit word half-word • 32 bit double-word word • 64 bit quad-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

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

  30. Addition of Positive Numbers

  31. a: 0 0 1 1 b: 0 1 0 1 Sum: 1 0 0 0 One-Bit Full Adder (1/2) • Example Binary Addition: Carries • Thus for any bit of addition: • The inputs are ai, bi, CarryIni • The outputs are Sumi, CarryOuti • Note: CarryIni+1 = CarryOuti

  32. CarryIn A + Sum B CarryOut 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

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

  34. Signed Numbers

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

  37. 00000 00001 ... 01111 11111 10000 ... 11110 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. • What is -00000 ? • How many positive numbers in N bits? • How many negative ones?

  38. 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

  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. Two’s Complement Number line 00000 11111 • 2N-1 non-negatives • 2N-1 negatives • one zero • how many positives? • comparison? • overflow? 00001 11110 00010 0 -1 1 2 -2 . . . . . . 15 -15 -16 01111 10001 10000

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

  42. 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 • Example1111 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

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

  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 -4ten to 32-bit: 1111 1111 1111 1100two 1111 1111 1111 1111 1111 1111 1111 1100two

  45. Addition & Subtraction of Signed Numbers

  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 • Subtraction: • 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 0 1 00 1 0 0 (+4) +1 1 0 0 (-4) 1 1 1 0 (-2) 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 1 1 01 0 1 1 (-5) +0 1 0 1 (+5)10 0 1 1 (+3)

  47. Decimal 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 Overflow • Examples: 7 + 3 = 10 but ... • - 4 – 5 = - 9 but ... 0 1 1 1 1 0 1 1 1 7 1 1 0 0 – 4 3 – 5 + 0 0 1 1 + 1 0 1 1 1 0 1 0 – 6 0 1 1 1 7

  48. 0 1 1 0 0 1 1 1 7 1 1 0 0 –4 3 – 5 + 0 0 1 1 + 1 0 1 1 1 0 1 0 – 6 0 1 1 1 7 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 • Overflowcan be detected by evaluating: • Carry into MSB  Carry out of MSB

  49. Arithmetic & Branching Conditions

  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)+0 1 0 0 (+4) 1 0 0 1 (-7) 0 1 0 1 (+5)+1 0 1 0 (-6)1 1 1 1 (-1) 0 0 1 1 (+3)+1 1 0 1 (-3)10 0 0 0 (0) 0 1 1 1 (+7)+1 1 0 1 (-3)10 1 0 0 (+4)

More Related