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 Bobby Nazief bahan kuliah:

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 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 h 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 = mov ebx,10b; binary number, value = mov ecx,10h; hexadecimal number, value = 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= ebx=000002a5 ecx= edx= f esi= 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(): e0: c enter 0,0 >> l _asm_main(): e0: c enter 0, e4: 60 pusha e5: b80a mov eax,0xa ea: bb mov ebx,0x ef: b mov ecx,0x f4: 89c2 mov edx,eax f6: 31c0 xor eax,eax f8: 31db xor ebx,ebx fa: 31c9 xor ecx,ecx

13 13 Tugas #0 (2/4) °Debug tugas0a.exe (lanjutan...) >> s e4: 60 pusha >> r eax= ebx=000002a5 ecx= edx= f esi= 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 e4: 60 pusha >> s e5: b80a mov eax,0xa >> s ea: bb mov ebx,0x2 >> r eax= a ebx=000002a5 ecx= edx= f esi= 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 ea: bb mov ebx,0x2

14 14 Tugas #0 (3/4) °Debug tugas0a.exe (lanjutan...) >> s >>... eax= ebx= ecx= edx= esi= edi= ebp=00090b10 UP IE PL ZR PE NC ds=01f7 es=01f7 fs=01e7 gs=0207 ss:esp=01f7:00090af0 cs=01ef : 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: 0x x x x >> s : e0c80000 mov [__what_size_app_think_thinks_it_is+26],dx... >> d 0xc8e0 0x0000c8e0: 0x x x x

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...0xC8E xA0 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, 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, 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:

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) = 2 k Alamat data

23 Review: Stored-program Computer Processor (active) Control (“brain”) Datapath (“brawn”) 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: = 1x x x x x x x2 0 = = 90 Penulisan: b °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 = 5x x16 0 = = 90 Penulisan: 5Ah atau 0x5A °Konversi: Desimal  Heksa-desimal 90 / 16 = 5sisa 10 (A) 5 / 16 = 0sisa 5 °Konversi: Heksa-desimal  Biner 5A = °Konversi: Biner  Heksa-desimal = = 5 A = 5A

27 27 Tabel Bilangan DesimalBinerHeksaDesimalBinerHeksa A B C D E F 2k2k NilaiSebutan K K K K M

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) 0x A

29 29 Penyimpanan data multi-byte (Little Endian) int j = ; = 0x000F1234 = FFFFFFFF Alamat (32 bit) int i = 90; 90 = 0x5A = 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: b: Sum:

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 0x = +0 ten 0x = -0 ten What would it mean for programming? °Sign and magnitude abandoned

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

38 38 Shortcomings of ones complement? °Arithmetic not too hard °Still two zeros 0x = +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 xxx is >=0, 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?

41 41 Two’s Complement Numbers two = 0 ten two = 1 ten two = 2 ten two = 2,147,483,645 ten two = 2,147,483,646 ten two = 2,147,483,647 ten two = –2,147,483,648 ten two = –2,147,483,647 ten two = –2,147,483,646 ten two =–3 ten two =–2 ten 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 d 30 x d 2 x d 1 x d 0 x 2 0 °Example two = 1x x x x2 2 +0x2 1 +0x2 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 two 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 : two x’: two +1: two ()’: two +1: 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: two 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 (+5) (+2) (+7) (+5) (-6) (-1) (-5) (-2) (-7) (+7) (-3) (+4) (+2)  (+4) (-4) (-2) (-2)  (-5) (+5) (+3) °Subtraction: Form 2’s complement of the subtrahend Add the two numbers as in Addition

47 47 Overflow °Examples: = 10 but... ° - 4 – 5 = - 9 but... 2’s ComplementBinaryDecimal Decimal – 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 – 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 (+5) (-6) (-1) (+7) (-3) (+4) (+5) (+4) (-7) (+3) (-3) (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