CS计算机代考程序代写 Memory and the CPU

Memory and the CPU
Given an instruction: C = A + B
 Memory: Stores add instruction, and variables A, B, and C.
 CPU: reads the instruction and values, performs the addition
operation on the values and writes the result to C in memory. We can think of memory as an array of bytes; we will access memory
4-bytes at a time. Each byte is 8 bits (1 or 0)
How does the CPU interact with memory?
Typically the CPU reads a number of bytes from memory (to access a value), or writes to memory (to initialize or update a variable value).
When working with a sequence (multiple) bytes, we need 2 things:
1. The address of the first byte
2. The number of bytes in the sequence
Variables and memory
In this class, we will use 32-bit (4-byte) integers. Range of values in 1 byte: 00000000 to 11111111
More on signed vs unsigned later.
Last day we saw two different types of variables:
1. Variables: have an address, size, and a value.
2. Pointers: whose value is a memory address.
Binary <-> Decimal <-> Hexidecimal
A hexit is a hexadecimal digit.
Typically, we write a hexadecimal value with a preceding 0x.
Convert 0x4c3 to binary (base-2) and decimal (base-10):
4C3 010011000011
1024+128+64+2+1 = 1219
Subtracting Hexadecimal Numbers
You can subtract in hex the same way we do in decimal. Keep in mind we have 16-digits to work with, so the carry is 16 instead of 10.
DEA9 -4 F B D 8EEC
Twos compliment notation:
-x = ~x + 1
Negate a number by complimenting it and incrementing it by 1.
Assume we have an integer (4 byte) variable with address 200 and value 8.
Assume we also have a pointer at address 208 pointing the variable with value 8 above.
Addr … 200 204 208 …
Value 8
200
What is the negative value in binary for 6?
00000110
11111001
1
11111010
Big and Little Endian
Associated with how processors interpret data.
Big endian eat numbers from big part of number first. Little endian eat numbers starting at little end.
Draw the memory contents for value 0x12345678
Big Endian:
0x12 0x34 0x56 0x78
Little Endian:
0x78 0x56 0x34 0x12
In memory, both of these are just values (8 and 200), whether the type is a variable or a pointer. It is up to us to define how we interpret these values.

Address Alignment
We will align our addresses so that each address is aligned to the size of the data type mod zero.
This means that a 4-byte value will be aligned to every memory location with an address divisible by 4 (0, 4, 8, 12, 16, etc), whereas a 10-byte value will be aligned to only memory addresses divisible by 10.
Bit Shifts
Shifting left b bits is the same as _multiplying_ by 2b. Shifting right b bits is the same as _ dividing_ by 2b.
00101100 (44) shifted right one bit: _00010110_ 00101100 (44) shifted left one bits: _ 01011000_
For signed values, the sign bit (far-left) remains unchanged. -6 == 11111010 shifted right: 01111101 = _125_
-6 == 11111010 shifted right: 11111101 = _-3_
Why?
Remember that the left-most bit is the sign bit, which determines whether the value is positive or negative. It does not contribute to the value itself.
Extension and Truncation
 Extension: Increase the number of bytes to store int
 Truncation: Decrease the number of bytes
What value will be printed in each of the following print statements:
0x34 (52) 0x87 (-121)
What value will be printed in each of the following print statements:
… 4 5 6 7 8 9
byte b = 0x12; //18 int i = b; out.printf(“%x\n”, i);
int i = 0x8B << 16; out.printf("%x\n", i); ... 4 5 6 7 8 int i = ((byte) 0x8b)<< 16; out.printf("%x\n", i); i = 0xff8b0000 & 0x00ff0000; out.printf("%x\n", i); 0x12 (18) 0x008B0000 0xff8b0000 0x008b0000 ... 4 5 6 ... int i = 0x1234; //4660 byte b = (byte) i; out.printf("%x\n", b); ... 4 5 6 ... int i = 0x87; //135 byte b = (byte) i; out.printf("%x\n", b);