CPSC 213
Introduction to Computer Systems
Unit 1a
Numbers and Memory
1
CPSC 213 Introduction to Computer Systems Unit 1a Numbers and - - PowerPoint PPT Presentation
CPSC 213 Introduction to Computer Systems Unit 1a Numbers and Memory 1 The Big Picture Build machine model of execution for Java and C programs by examining language features and deciding how they are implemented by the
Introduction to Computer Systems
Unit 1a
Numbers and Memory
1The Big Picture
Languages and Tools
Lab/Assignment 1
The Main Memory Class
CPU fetch execute MainMemory isAligned bytesToInteger integerToBytes get set read readInteger write writeInteger
5The Code You Will Implement
/** * Determine whether an address is aligned to specified length. * @param address memory address * @param length byte length * @return true iff address is aligned to length */ protected boolean isAccessAligned (int address, int length) { return false; }
6/** * Convert an sequence of four bytes into a Big Endian integer. * @param byteAtAddrPlus0 value of byte with lowest memory address * @param byteAtAddrPlus1 value of byte at base address plus 1 * @param byteAtAddrPlus2 value of byte at base address plus 2 * @param byteAtAddrPlus3 value of byte at base address plus 3 * @return Big Endian integer formed by these four bytes */ public int bytesToInteger (UnsignedByte byteAtAddrPlus0, UnsignedByte byteAtAddrPlus1, UnsignedByte byteAtAddrPlus2, UnsignedByte byteAtAddrPlus3) { return 0; } /** * Convert a Big Endian integer into an array of 4 bytes * @param i an Big Endian integer * @return an array of UnsignedByte */ public UnsignedByte[] integerToBytes (int i) { return null; }
7** * Fetch a sequence of bytes from memory. * @param address address of the first byte to fetch * @param length number of bytes to fetch * @return an array of UnsignedByte */ protected UnsignedByte[] get (int address, int length) throws ... { UnsignedByte[] ub = new UnsignedByte [length]; ub[0] = new UnsignedByte (0); // with appropriate value // repeat to ub[length-1] ... return ub; } /** * Store a sequence of bytes into memory. * @param address address of the first memory byte * @param value an array of UnsignedByte values * @throws InvalidAddressException if any address is invalid */ protected void set (int address, UnsignedByte[] value) throws ... { byte b[] = new byte [value.length]; for (int i=0; i<value.length; i++) b[i] = (byte) value[i].value(); // write b into memory ... }
8Reading
Alignment.
Numbers and Bits
10Binary, Hex, and Decimal Refresher
base 10
binary format is important
(0|1)x8 + (0|1)x4 + (0|1)x2 + (0|1)x1
B 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 H 1 2 3 4 5 6 7 8 9 a b c d e f D 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
11Bit Shifting
Masking
Two's Complement: Reminder
end up at -1
255 0xff 0x0 +127
0x0 0x7f 0x80 0xff
14Two's Complement: Byte
B 1111 1111 1111 1110 1111 1101 1111 1100 1111 1011 1111 1010 1111 1001 1111 1000 1111 0111 1111 0110 1111 0101 1111 0100 1111 0011 1111 0010 1111 0001 1111 0000 H 0xff 0xfe 0xfd 0xfc 0xfb 0xfa 0xf9 0xf8 0xf7 0xf6 0xf5 0xf4 0xf3 0xf2 0xf1 0xf0 Unsigned 255 254 253 252 251 250 249 248 247 246 245 244 243 242 241 240 Signed Decimal
Two's Complement: 32-Bit Integers
end up at -1
4,294,967,295 0xffffffff 0x0 2,147,483,647
0x0 0x7fffffff 0x80000000 0xffffffff
16Two's Complement and Sign Extension
Bit Shifting in Java
Numbers in Memory
19Memory and Integers
0 to N
byte addresses
1 2 3 4 5 6 7 8 9 . . . N
. . . 20Making Integers from Bytes
i 2
3 1
t
2 4
i + 1 2
2 3
t
1 6
i + 2 2
1 5
t
8
i + 3 2
7
t
i + 3 2
3 1
t
2 4
i + 2 2
2 3
t
1 6
i + 1 2
1 5
t
8
i 2
7
t
i i + 1 i + 2 i + 3 ... ...
✔
Memory
Register bits Register bits
21Making Integers from Bytes
i 2
3 1
t
2 4
i + 1 2
2 3
t
1 6
i + 2 2
1 5
t
8
i + 3 2
7
t
i + 3 2
3 1
t
2 4
i + 2 2
2 3
t
1 6
i + 1 2
1 5
t
8
i 2
7
t
i i + 1 i + 2 i + 3 ... ...
Memory
Register bits Register bits
21j / 2k == j >> k (j shifted k bits to right) word contains exactly two complete shorts address modulo chunk-size is always zero
✔ ✗
* disallowed on many architectures * allowed on Intel, but slowerj / 2k == j >> k (j shifted k bits to right) word contains exactly two complete shorts address modulo chunk-size is always zero
Computing Alignment
remainder
B 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 H 1 2 3 4 5 6 7 8 9 a b c d e f D 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
23In the Lab ... Revisited
Questions
0x1c04b673
0xc1406b37
0x376b40c1
none of these
I don’t know
0x0: 0xfe 0x1: 0x32 0x2: 0x87 0x3: 0x9a 0x4: 0x73 0x5: 0xb6 0x6: 0x04 0x7: 0x1c
Memory
28i = 0xff8b0000 & 0x00ff0000;
int i = (0x0000008b) << 16;
0x8b
0x0000008b
0x008b0000
0xff8b0000
None of these
I don’t know
30int i = (byte)(0x8b) << 16;
0x8b
0x0000008b
0x008b0000
0xff8b0000
None of these
I don’t know
31