iki10230 pengantar organisasi komputer kuliah no. 03: sistem bilangan
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
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 ([email protected])Bobby Nazief ([email protected])
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.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
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_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
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)
1. segment .data
2. data1 db 11h
3. data2 dw 2222h
4. data3 dd 33333333h
5. datatmp times 9 db 0ffh
6. data4 times 16 db 0
7. segment .bss
8. stacks resd 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
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
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=0000033fesi=00000054 edi=00010b50 ebp=00090b30 UP IE PL NZ AC PE NCds=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,0000015e4: 60 pusha000015e5: b80a000000 mov eax,0xa000015ea: bb02000000 mov ebx,0x2000015ef: b910000000 mov ecx,0x10000015f4: 89c2 mov edx,eax000015f6: 31c0 xor eax,eax000015f8: 31db xor ebx,ebx000015fa: 31c9 xor ecx,ecx
13
Tugas #0 (2/4)
° Debug tugas0a.exe (lanjutan ...)>> s
000015e4: 60 pusha
>> reax=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
>> s000015e5: b80a000000 mov eax,0xa
>> s000015ea: bb02000000 mov ebx,0x2
>> reax=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+260x0000c8e0: 0x00000000 0x00000000 0x00000000 0x00000000
>> s00001626: 668915e0c80000 mov [__what_size_app_think_thinks_it_is+26],dx ...
>> d 0xc8e00x0000c8e0: 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
Baris ke- EAX EBX ... 0xC8E0 ...12 0
14 0xA 0
15 0x2
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
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
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
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 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
° 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 = 2k - 1
01110
Alamat
data
23
0 0 8 4 62 1 6 8 64 0 0 6 16 0 0 1 78 0 0 0 010 0 0 0 012 0 0 0 014 0 0 0 016 0 0 0 018 0 0 0 0
0 0 8 4 62 1 6 8 64 0 0 6 16 0 0 1 78 0 0 7 810 0 0 0 012 0 0 0 014 0 0 0 016 0 0 0 018 0 0 0 0
Review: Stored-program Computer
Processor (active)
Control(“brain”)
Datapath(“brawn”)
0846
006100170078
0 0 8 4 62 1 6 8 64 0 0 6 16 0 0 6 18 0 0 7 810 0 0 0 012 0 0 0 014 0 0 0 016 0 0 0 018 0 0 0 0
1686
007800170061
IP
IP
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
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 Biner90 / 2 = 45sisa 0
45 / 2 = 22sisa 1
22 / 2 = 11sisa 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-desimal90 / 16 = 5sisa 10 (A)
5 / 16 = 0sisa 5
° Konversi: Heksa-desimal Biner5A = 101 1010
° Konversi: Biner Heksa-desimal1011010 = 101 1010
= 5 A = 5A
27
Tabel Bilangan
Desimal Biner Heksa Desimal Biner Heksa0 0000 0 8 1000 81 0001 1 9 1001 92 0010 2 10 1010 A3 0011 3 11 1011 B4 0100 4 12 1100 C5 0101 5 13 1101 D6 0110 6 14 1110 E7 0111 7 15 1111 F
2k Nilai Sebutan210 1.024 1K211 2.048 2K212 4.096 4K216 65.536 64K220 1.048.576 1M
28
Pengelompokkan Bit
° Bit String: 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)
0x0000005A
29
Penyimpanan data multi-byte (Little Endian)
int j = 987700;
987700 = 0x000F1234 =
0000 0000 0000 1111 0001 0010 0011 0100
0000000000000001000000020000000300000004000000050000000600000007
FFFFFFFF
0101 1010
Alamat (32 bit)
0000 00000000 00000000 00000011 01000001 00100000 11110000 0000
int i = 90;
90 = 0x5A =
0000 0000 0000 0000 0000 0000 0101 1010
i
j
30
Addition of Positive Numbers
31
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
a: 0 0 1 1
b: 0 1 0 1
Sum: 1 0 0 0
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
SumA
B
CarryIn
CarryOut
+
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-bitFA
Sum0
CarryIn0
CarryOut0
A1
B1
1-bitFA
Sum1
CarryIn1
CarryOut1
A2
B2
1-bitFA
Sum2
CarryIn2
CarryOut2
A3
B3
1-bitFA
Sum3
CarryIn3
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
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...
111111111010000 ...
° 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
° 2N-1 non-negatives
° 2N-1 negatives
° one zero
° how many positives?
° comparison?
° overflow?
00000 0000100010
11111
11110
10000 0111110001
0 12
-1-2
-15-16 15
.
.
.
.
.
.
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
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 00 1 0 0 (+4) +1 1 0 0 (-4)
1 1 1 0 (-2)
1 1 1 0 (-2) 1 1 1 01 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
Overflow
° Examples: 7 + 3 = 10 but ...
° - 4 – 5 = - 9 but ...
2’s ComplementBinaryDecimal
0 0000
1 0001
2 0010
3 0011
0000
1111
1110
1101
Decimal
0
-1
-2
-3
4 0100
5 0101
6 0110
7 0111
1100
1011
1010
1001
-4
-5
-6
-7
1000-8
0 1 1 1
0 0 1 1+
1 0 1 0
1
1 1 0 0
1 0 1 1+
0 1 1 1
110
7
3
1
– 6
– 4
– 5
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
0 1 1 1
0 0 1 1+
1 0 1 0
1 1 0 0
1 0 1 1+
0 1 1 1
110
7
3
– 6
–4
– 5
7
0
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)+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)