140 likes | 151 Views
Bits and Bytes. COMP 21000 Comp Org & Assembly Lang. Topics Why bits? Representing information as bits Binary / Hexadecimal Byte representations Numbers Characters and strings Instructions Bit-level manipulations Boolean algebra Expressing in C. Converting. main() {
E N D
Bits and Bytes COMP 21000 Comp Org & Assembly Lang Topics • Why bits? • Representing information as bits • Binary / Hexadecimal • Byte representations • Numbers • Characters and strings • Instructions • Bit-level manipulations • Boolean algebra • Expressing in C
Converting main() { octal_print(0); /* result: 00 */ octal_print(-1); /* result: -01 */ octal_print(123456); /* result: 0361100 */ octal_print(-123456); /* result: -0361100 */ octal_print(0123456); /* result: 0123456 since 0 at */ /* beginning indicates octal num */ octal_print(-0123456); /* result: -0123456 */ octal_print(999); /* result: 01747 */ octal_print(-999); /* result: -01747 */ octal_print(1978); /* result: 03672 */ }
Converting #define DIG 20 void octal_print(int x) { int i = 0, od; char s[DIG]; /* up to DIG octal digits */ if ( x < 0 ) /* treat negative x */ { putchar ('-'); /* output a minus sign */ x = - x; } do { od = x % 8; /* next higher octal digit */ s[i++] = (od + '0'); /* octal digits in char form */ } while ( (x = x/8) > 0); /* quotient by integer division */ putchar('0'); /* octal number prefix */ do { putchar(s[--i]); /* output characters on s s*/ } while ( i > 0 ); putchar ('\n'); putchar ('\n'); } See /home/barr/Student/comp210/examples/octal.c
Memory • What is memory (RAM)? • To the processor, it’s just a big array of cells • Each cell has an address • Each cell contains a specific value 0 1 2 3 4 Address Contents
Memory • Of course, addresses and content are actually in binary Memory is byte addressable i.e., every byte has an address. Address 000 001 010 011 100 Contents
Memory • When we look at memory in a debugger (like gdb), usually use hex notation Address 000 001 002 003 004 Contents
Memory What we see (gdb) x/20b 0x7fffffffe45c 0x7fffffffe45c:0x5 0x0 0x0 0x0 0x1 0x0 0x0 0x0 0x7fffffffe464:0x0 0x0 0x0 0x0 0xffffffffffffffdd 0x50x40 0x0 0x7fffffffe46c:0x0 0x0 0x0 0x0
Addressing Memory CPU must send an address to RAM how many bits in the address? depends on how many wires there are! 32-bit machine means 32 wires on each bus
Memory • How do we talk about the size of memory? • in terms of powers of 2 (addresses are binary) Bus must have 3 wires Memory size limited by number of bits in address: 3 bits so 8 cells
Range An address has a fixed number of bits. The range is the numbers that will fit in that many bits. Examples: 0 0 0 = 0 1 1 1 = 7 range is 0 to 7 0 0 0 0 = 0 1 1 1 1 = 15 range is 0 to 15
Range In general range is 0 to 2n – 1 where there are n bits in the representation Intuition: • 111 = 1 x 22 + 1 x 21 + 1 x 20 Which is 1 less than 1000 = 1 x 23 + 0 x 22 + 0 x 21 + 0 x 20 Number of numbers represented: • n bits can represent 2n different numbers • i.e., from 0 to 2n – 1
Size In general Size is 2n where there are n bits in the representation Intuition: • Range of 3 bits: 0 0 0 = 0 1 1 1 = 7 range is 0 to 7 number of numbers is 8 = 23 or 1 more than the largest number! Number of numbers represented: • n bits can represent 2n different numbers • i.e., from 0 to 2n – 1
Memory • How do we talk about the size of memory? • in terms of powers of 2 (addresses are binary) based on the number of bits in the address
Memory • How do we talk about the size of memory? • in bytes (how may bytes of memory do you have?) • in terms of powers of 2 (addresses are binary) 1 bit 1 byte 1 Kilobyte (Kb) 1 Megabyte (Mb) 1 Gigabyte (Gb) 1 Terabyte (Tb)