Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

IKI10230 Pengantar Organisasi Komputer Kuliah no. 03: Sistem Bilangan

Presentasi serupa


Presentasi berjudul: "IKI10230 Pengantar Organisasi Komputer Kuliah no. 03: Sistem Bilangan"— Transcript presentasi:

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 Penggunaan Tools: GCC, NASM, EDEBUG32

3 C Compiler: GCC Fungsi Kompilasi Contoh: hello.c  hello.exe
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 gcc –c hello.c  hello.o gcc –o hello.exe hello.o gcc –o hello.exe hello.c

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

5 Contoh: hello.asm  hello.exe
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_str db "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 Fungsi: men-debug program (.exe)
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 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 h 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 Debug tugas0a.exe
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=00090b UP IE PL NZ AC PE NC ds=01f7 es=01f7 fs=01e7 gs=0207 ss:esp=01f7:00090b14 cs=01ef _asm_main(): 000015e0: c enter 0,0 >> l 000015e0: c enter 0,0 000015e4: pusha 000015e5: b80a mov eax,0xa 000015ea: bb mov ebx,0x2 000015ef: b mov ecx,0x10 000015f4: 89c mov edx,eax 000015f6: 31c xor eax,eax 000015f8: 31db xor ebx,ebx 000015fa: 31c xor ecx,ecx

13 Debug tugas0a.exe (lanjutan ...)
000015e4: pusha >> r eax= ebx=000002a5 ecx= edx= f esi= edi=00010b50 ebp=00090b UP IE PL NZ AC PE NC ds=01f7 es=01f7 fs=01e7 gs=0207 ss:esp=01f7:00090b10 cs=01ef 000015e5: b80a mov eax,0xa 000015ea: bb mov ebx,0x2 eax= a ebx=000002a5 ecx= edx= f ds=01f7 es=01f7 fs=01e7 gs=0207 ss:esp=01f7:00090af0 cs=01ef 000015ea: bb mov ebx,0x2

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

15 Tugas #0 (4/4) Merakit tugas0b.asm Eksekusi tugas0b.exe Laporan
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- EAX EBX ... 0xC8E0 12 14 0xA 15 0x2 16

16 tugas0b.asm (1/3) %include "asm_io.inc" segment .data
[same as tugas0a.asm] segment .bss 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 => (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

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 11111 = 2k - 1

23 Review: Stored-program Computer
operasi yang dilakukan oleh komputer ditentukan oleh instruksi & data yang tersimpan di memori 0846 IP Processor (active) Control (“brain”) Datapath (“brawn”) 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 Harga/Nilai suatu bilangan biner:
Simbol: 0,1 Harga/Nilai suatu bilangan biner: = 1x26 + 0x25 + 1x24 + 1x23 + 0x x21 + 0x = = 90 Penulisan: b 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 = 5x x160 = = 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 = Konversi: Biner  Heksa-desimal = = A = 5A

27 Tabel Bilangan Desimal Biner Heksa 2k Nilai Sebutan 210 1.024 1K 211
0000 8 1000 1 0001 9 1001 2 0010 10 1010 A 3 0011 11 1011 B 4 0100 12 1100 C 5 0101 13 1101 D 6 0110 14 1110 E 7 0111 15 1111 F 2k Nilai Sebutan 210 1.024 1K 211 2.048 2K 212 4.096 4K 216 65.536 64K 220 1M

28 Pengelompokkan Bit Bit String: Alamat lokasi memori INTEL MIPS
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) 0x A

29 Penyimpanan data multi-byte (Little Endian)
FFFFFFFF Alamat (32 bit) int i = 90; 90 = 0x5A = i j int j = ; = 0x000F1234 =

30 Addition of Positive Numbers

31 Carries 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 a: b: Sum: 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 Sum A B CarryIn CarryOut +

33 Ripple-Carry Adders: adding n-bits numbers
CarryIn0 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 Kinerja operasi penjumlahan (dan juga operasi- operasi aritmatika lainnya) akan bergantung pada “besar” unit data dan konfigurasi Adder (Arithmetic & Logical Unit) yang digunakan

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

37 Another try: complement the bits
Example: 710 = = Called one’s Complement Note: postive numbers have leading 0s, negative numbers have leadings 1s. 00000 00001 01111 ... 11111 11110 10000 What is ? How many positive numbers in N bits? How many negative ones?

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

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

41 Two’s Complement Numbers
two = 0ten two = 1ten two = 2ten two = 2,147,483,645ten two = 2,147,483,646ten two = 2,147,483,647ten two = –2,147,483,648ten two = –2,147,483,647ten two = –2,147,483,646ten two = –3ten two = –2ten two = –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 d30 x d2 x 22 + d1 x 21 + d0 x 20 Example two = 1x x230 +1x x22+0x21+0x20 = = -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 two two= -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 : two x’: two +1: two ()’: two +1: two

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: two two

45 Addition & Subtraction of Signed Numbers

46 Addition & Subtraction Operations
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 (+5) (+2) (+7) (+5) (-6) (-1) (+2)  (+4) (-4) (-2) (-5) (-2) (-7) (+7) (-3) (+4) (-2)  (-5) (+5) (+3)

47 Overflow Examples: 7 + 3 = 10 but ... - 4 – 5 = - 9 but ...
2’s Complement Binary Decimal 0000 1 0001 2 0010 3 0011 1111 1110 1101 -1 -2 -3 4 0100 5 0101 6 0110 7 0111 1100 1011 1010 1001 -4 -5 -6 -7 1000 -8 Examples: = but ... - 4 – 5 = but ... Well so far so good but life is not always perfect. Let’s consider the case 7 plus 3, you will get 10. But if you perform the binary arithmetics on our 4-bit adder you will get 1010, which is negative 6. Similarly, if you try to add negative 4 and negative 5 together, you should get negative 9. But the binary arithmetics will give you 0111, which is 7. So what went wrong? The problem is overflow. The number you get are simply too big, in the positive 10 case, and too small in the negative 9 case, to be represented by four bits. +2 = 39 min. (Y:19) 1 1 1 1 1 1 1 7 1 1 – 4 3 – 5 + 1 1 + 1 1 1 1 1 – 6 1 1 1 7

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 Recalled from some earlier slides that the biggest positive number you can represent using 4-bit is 7 and the smallest negative you can represent is negative 8. So any time your addition results in a number bigger than 7 or less than negative 8, you have an overflow. Keep in mind is that whenever you try to add two numbers together that have different signs, that is adding a negative number to a positive number, overflow can NOT occur. Overflow occurs when you to add two positive numbers together and the sum has a negative sign. Or, when you try to add negative numbers together and the sum has a positive sign. If you spend some time, you can convince yourself that If the Carry into the most significant bit is NOT the same as the Carry coming out of the MSB, you have a overflow. +2 = 41 min. (Y:21) 1 + 7 3 – 6 –4 – 5

49 Arithmetic & Branching Conditions

50 CC Flags will be set/cleared by arithmetic operations:
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 (+5) (-6) (-1) (+5) (+4) (-7) (+7) (-3) (+4) (+3) (-3) (0)


Download ppt "IKI10230 Pengantar Organisasi Komputer Kuliah no. 03: Sistem Bilangan"

Presentasi serupa


Iklan oleh Google