iki10230 pengantar organisasi komputer kuliah no. 03: sistem bilangan

of 50 /50
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 ([email protected]) Bobby Nazief ([email protected]) bahan kuliah: http://www.cs.ui.ac.id/kuliah/POK/

Author: kayla

Post on 15-Jan-2016

78 views

Category:

Documents


0 download

Embed Size (px)

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 Presentation

TRANSCRIPT

Pengantar Organisasi KomputerSumber:
2. Hamacher. Computer Organization, ed-5
3. Materi kuliah CS61C/2000 & CS152/1997, UCB
25 Februari 2004
Kompilasi
Cara #1:
Cara #2:
Contoh: hello.c hello.exe
Perakitan
digunakan oleh DJGPP
–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
IKI20210
segment .bss
segment .text
global _asm_main
pop eax
eksekusi program sampai dengan instruksi tertentu (breakpoint)
evaluasi register & memori
Cara Menjalankan:
edebug32 <file-exe>
cont c continue execution
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
whereis find a symbol/location (takes wildcard or value)
cls clear screen
segment .bss
mov edx,eax ; register-to-register transfer
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]
popa
leave
ret
IKI20210
Debug tugas0a.exe
edebug32 tugas0a.exe
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():
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
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
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
>> d __what_size_app_thinks_it_is+26
>> s
>> d 0xc8e0
IKI20210
Eksekusi tugas0b.exe
Laporan
hasil perbandingan hasil pengamatan dengan ‘print-out’ tugas0b
Baris ke-
IKI20210
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 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
IKI20210
mov [data4],dl
mov [data4],dx
mov [data4],edx
popa
Bits dapat merepresentasikan apapun!
berapa simbol huruf?
Logical values?
Alamat? (berapa karakter alfabet ..)
IKI20210
Contoh:
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
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
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
Processor
(active)
Control
(“brain”)
Datapath
(“brawn”)
IKI20210
= 64 + 16 + 8 + 2 = 90
Penulisan: 1011010b
IKI20210
5 / 16 = 0 sisa 5
Konversi: Heksa-desimal Biner
5A = 101 1010
Konversi: Biner Heksa-desimal
1011010 = 101 1010
= 5 A = 5A
contoh:
lokasi memori 90 pada memori dengan ruang memori sebesar 64K (65536 = 216) dinyatakan dengan alamat:
0x005A
0x0000005A
IKI20210
int j = 987700;
int i = 90;
i
j
00000000
00000001
00000002
00000003
00000004
00000005
00000006
00000007
FFFFFFFF
The outputs are Sumi, CarryOuti
Note: CarryIni+1 = CarryOuti
IKI20210
implement gates for Sum
implement gates for CarryOut
Sum
A
B
CarryIn
CarryOut
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
IKI20210
So far, unsigned numbers
0 => +, 1 => -
Representation called sign and magnitude
IKI20210
Arithmetic circuit more complicated
Special steps depending whether signs are the same or not
Also, Two zeros
Sign and magnitude abandoned
Called one’s Complement
Note: postive numbers have leading 0s, negative numbers have leadings 1s.
What is -00000 ?
How many negative ones?
One’s complement eventually abandoned because another solution was better
IKI20210
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
IKI20210
2N-1 non-negatives
2N-1 negatives
one zero
. . .
. . .
One zero, 1st bit is called sign bit
but one negative with no positive –2,147,483,648ten
IKI20210
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
Example
= 1x-231 +1x230 +1x229+... +1x22+0x21+0x20
= -231 + 230 + 229 + ... + 22 + 0 + 0
= -2,147,483,648ten + 2,147,483,644ten
IKI20210
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
IKI20210
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
IKI20210
IKI20210
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 0
0 1 0 0 (+4) +1 1 0 0 (-4)
1 1 1 0 (-2)
1 1 1 0 (-2) 1 1 1 0
1 0 1 1 (-5) +0 1 0 1 (+5)
10 0 1 1 (+3)
Subtraction:
IKI20210
- 4 – 5 = - 9 but ...
Binary
Decimal
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
IKI20210
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)
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:
Overflow can be detected by evaluating:
Carry into MSB Carry out of MSB
0
1
1
1
0
0
1
1
0
1
1
1
1
1
0
7
3
– 6
–4
– 5
7
0
IKI20210
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)
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)
IKI20210