Title: Pengantar Organisasi Komputer
1IKI10230Pengantar Organisasi KomputerKuliah no.
03 Sistem Bilangan
Sumber1. Paul Carter, PC Assembly Language2.
Hamacher. Computer Organization, ed-53. Materi
kuliah CS61C/2000 CS152/1997, UCB
25 Februari 2004 L. Yohanes Stefanus
(yohanes_at_cs.ui.ac.id)Bobby Nazief
(nazief_at_cs.ui.ac.id) bahan kuliah
http//www.cs.ui.ac.id/kuliah/POK/
2- Penggunaan Tools
- GCC, NASM, EDEBUG32
3C Compiler GCC
- Fungsi
- menerjemahkan program dalam bahasa C ke bahasa
mesin - Kompilasi
- gcc OPSI ltnama-filegt
- 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 ltother-object-filesgt
- Cara 2
- gcc o myprog.exe myprog.c ltother-object-filesgt
- 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
4hello.c
- int main()
-
- printf("hello world\n")
5Assembler NASM
- Fungsi
- menerjemahkan program dalam bahasa rakitan ke
bahasa mesin - Perakitan
- nasm OPSI ltnama-filegt
- 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 ltother-object-filesgt
- ? 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
6hello.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
7driver.c
- int asm_main( void )
- int main()
-
- int ret_status
- ret_status asm_main()
- return ret_status
8Debuger 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 ltfile-exegt
9EDEBUG32 Commands
- go ltvgt g go, stop at ltvgt
- 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
10tugas0a.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
11tugas0a.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
12Tugas 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
- gtgt
- gtgt g asm_main
- eax00000000 ebx000002a5 ecx00000000
edx0000033f - esi00000054 edi00010b50 ebp00090b30
UP IE PL NZ AC PE NC - ds01f7 es01f7 fs01e7 gs0207
ssesp01f700090b14 cs01ef - _asm_main()
- 000015e0 c8000000 enter 0,0
- gtgt l
- _asm_main()
- 000015e0 c8000000 enter 0,0
- 000015e4 60 pusha
- 000015e5 b80a000000 mov eax,0xa
- 000015ea bb02000000 mov ebx,0x2
13Tugas 0 (2/4)
- Debug tugas0a.exe (lanjutan ...)
- gtgt s
- 000015e4 60 pusha
- gtgt r
- eax00000000 ebx000002a5 ecx00000000
edx0000033f - esi00000054 edi00010b50 ebp00090b10
UP IE PL NZ AC PE NC - ds01f7 es01f7 fs01e7 gs0207
ssesp01f700090b10 cs01ef - 000015e4 60 pusha
- gtgt s
- 000015e5 b80a000000 mov eax,0xa
- gtgt s
- 000015ea bb02000000 mov ebx,0x2
- gtgt r
- eax0000000a ebx000002a5 ecx00000000
edx0000033f - esi00000054 edi00010b50 ebp00090b10
UP IE PL NZ AC PE NC - ds01f7 es01f7 fs01e7 gs0207
ssesp01f700090af0 cs01ef - 000015ea bb02000000 mov ebx,0x2
14Tugas 0 (3/4)
- Debug tugas0a.exe (lanjutan ...)
- gtgt s
- gtgt ...
- eax00000011 ebx00002211 ecx33222211
edx33222211 - esi33332222 edi33333333 ebp00090b10
UP IE PL ZR PE NC - ds01f7 es01f7 fs01e7 gs0207
ssesp01f700090af0 cs01ef - 00001620 8815e0c80000 mov
__what_size_app_thinks_it_is26,dl dl11 -
si2222 - ? mov data4,dl store 1 byte
- gtgt d __what_size_app_thinks_it_is26
- 0x0000c8e0 0x00000000 0x00000000 0x00000000
0x00000000 - gtgt s
- 00001626 668915e0c80000 mov
__what_size_app_think_thinks_it_is26,dx ... - gtgt d 0xc8e0
- 0x0000c8e0 0x00000011 0x00000000 0x00000000
0x00000000
15Tugas 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
16tugas0b.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
17tugas0b.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
18tugas0b.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
20Review Bit dapat mepresentasikan apa saja !!!
- Bits dapat merepresentasikan apapun!
- Karakter? Latin
- 26 huruf gt 5 bits
- Huruf besar/kecil tanda lain gt 7 bits, berapa
simbol huruf? - Karakter, bahasa lain gt 16 (unicode)
- Logical values?
- 0 -gt False, 1 gt True
- Warna ? Berapa banyak warna gt berapa bits?
- Alamat? (berapa karakter alfabet ..)
- .. Tapi N bits ? hanya dapat merepresentasikan
2N sesuatu
21Review Bit ? Instruksi
- Instruksi (Operasi) dapat direpresentasikan oleh
bit. - Contoh
- 0 gt tepuk tangan
- 1 gt bersiul
- Eksekusi Instruksi
- 1. 0
- 2. 1
- 3. 1
- 4. 0
- 5. 0
- 6. 1
- 7. 0
22Review 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
23Review 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 25Bilangan 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
26Bilangan 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
27Tabel Bilangan
28Pengelompokkan 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
29Penyimpanan data multi-byte (Little Endian)
int i 90 90 0x5A 0000 0000 0000 0000 0000
0000 0101 1010
i
j
int j 987700 987700 0x000F1234 0000 0000
0000 1111 0001 0010 0011 0100
30- Addition of Positive Numbers
31One-Bit Full Adder (1/2)
Carries
- Thus for any bit of addition
- The inputs are ai, bi, CarryIni
- The outputs are Sumi, CarryOuti
- Note CarryIni1 CarryOuti
32One-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
33Ripple-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 35How to Represent Negative Numbers?
- So far, unsigned numbers
- Obvious solution define leftmost bit to be sign!
- 0 gt , 1 gt -
- Rest of bits can be numerical value of number
- Representation called sign and magnitude
36Shortcomings 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
37Another try complement the bits
- Example 710 001112 -710 110002
- Called ones 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?
38Shortcomings of ones complement?
- Arithmetic not too hard
- Still two zeros
- 0x00000000 0ten
- 0xFFFFFFFF -0ten
- What would it mean for programming?
- Ones complement eventually abandoned because
another solution was better
39Search for Negative Number Representation
- Obvious solution didnt 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 gt0, 111111...xxx is lt 0
- This representation called twos complement
40Twos Complement Number line
00000
11111
00001
- 2N-1 non-negatives
- 2N-1 negatives
- one zero
- how many positives?
- comparison?
- overflow?
11110
00010
0
-1
1
2
-2
. . .
. . .
15
-15
-16
01111
10001
10000
41Twos 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,647ten100
0 ... 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
42Twos 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... 1x220x210x20
- -231 230 229 ... 22 0 0
- -2,147,483,648ten 2,147,483,644ten
- -4ten
- Note need to specify width we use 32 bits
43Twos complement shortcut Negation
- Invert every 0 to 1 and every 1 to 0, then add 1
to the result - Sum of number and its ones 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
44Twos comp. shortcut Sign extension
- Convert 2s complement number using n bits to
more than n bits - Simply replicate the most significant bit (sign
bit) of smaller to fill new bits - 2s comp. positive number has infinite 0s
- 2s 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
46Addition Subtraction Operations
- Subtraction
- Form 2s complement of the subtrahend
- Add the two numbers as in Addition
- Addition
- Just add the two numbers
- Ignore the Carry-out from MSB
- Result will be correct, provided theres 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)
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)
47Overflow
- Examples 7 3 10 but ...
- - 4 5 - 9 but ...
1
1
1
0
1
0
1
1
1
1
1
0
0
7
4
3
5
0
0
1
1
1
0
1
1
1
0
1
0
0
1
1
1
6
7
48Overflow Detection
- Overflow the result is too large (or too small)
to represent properly - Example - 8 lt 4-bit binary number lt 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
49- Arithmetic Branching Conditions
50Condition 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)