34
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Embed Size (px)

Citation preview

Page 1: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

IT253: Computer Organization

Lecture 3:

Memory and Bit Operations

Tonga Institute of Higher Education

Page 2: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

A short divergence into memory

• Before we look at other topics we need to understand how memory works

• Things to learn in this chapter– What is computer memory?– What does it look like to a computer program?– How do we find things in memory?

Page 3: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

What is Memory

• It’s a bunch of bits!• Looks like a large linear array• Find things by “indexing” into the

array with an unsigned number• Most computers support 1 byte (8

bit) addressing– A byte will be at addresses

0x100, 0x101 and 0x102– A word will be at address 0x100

and 0x104 and 0x108• A “word” is 4 bytes

• 32 bits = 4 bytes = 1 word• 8 bits = 1 byte

Page 4: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Endianess

• When we looked at binary numbers we read them left to right so that 1101 = 13.

• The left most digit is the largest.– This is called Big Endianess

• If we switch 13 around, we get 1011 = 13– This is called Small Endianess

Page 5: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Endianess

• Big Endian – byte 0 contains most significant bits (used by Apple Mac computers)

• Little Endian – byte 0 contains least significant bits (used by microsoft computers)

• Remember, a byte contains 8 bits.

Page 6: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

More words - Alignment

• Alignment – If a piece of data is saved on an address that is a multiple of its size

• Example, a 32 bit integer– 1010 1010 1010 1111 1010 1010 1010 1111– This will be stored as 4 bytes in memory (8

bits in a byte)– This means it is stored in 4 separate spots in

memory

Byte 1 Byte 2 Byte 3 Byte 4

Page 7: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Alignment

• If those 4 bytes are stored in memory next to each other, then the memory is aligned.

• Imagine if two bytes were stored next to each other, but two were somewhere else.

• That is unaligned memory

Byte 3 stuff

stuff Byte 2stuff Byte 1

Address

0x001

0x002 stuff

UNALIGNED

Byte 4

Page 8: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Memory PartitionsThe memory that a program uses will be split into different parts

Page 9: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Memory Layout Example

x

result

Global variable

Localvariable

Page 10: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Pointers• Pointers are variables that are used to save memory

addresses. • They don’t save the real data inside of them. Pointers

just save the address of where the real data is stored• We often use pointers in programming • Examples:

• C++– object * newobject = new object();– string * str = new string();

• Java– String a = new String(“Hello”);

– A pointer works like normal variables except that the program will not allocate memory for that variable at the start of the program.

– Only when the program gets to that line of code will memory be used

Page 11: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Pointers

• When you make variables in programming, they can either be static or dynamic– Static

• string str;• int x;

– Dynamic• string * str = new string();• int * x = new int();

– In Java, almost all variables are Dynamic. The exception is if you use a “native type” like int.

Page 12: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Pointers

• Static variables take up memory when the program starts up. The program has already saved memory for that variable.

• The memory the program sees when it starts already has space taken up by static variables. This slows down the time it takes to start a program

• That space will now be reserved for that static variable for the entire time the program runs

• Dynamic variables are only created when the program gets to that line in code. Then the program will take the amount of memory it needs to use

Page 13: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Pointers

• Advantages of dynamic variables– Programs do not take up as much memory at start

time, which means they load faster– More ability to manage memory by the programmer

• Disadvantages– If the programmer forgets to delete this memory, the

code will “bloat.” – “Code bloat” mean the program will get slower and

slower because the program uses more and more memory

Page 14: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Pointers

• What is a pointer?– A pointer is a variable that saves an address

in memory– It "points" to a place in memory where the real

data is stored– int * x; - x is a variable and in memory it

contains a 32-bit address.– int x; - x is a variable and in memory it

contains an integer

Page 15: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Pointers

• What pointers look likeint x = 5;int * y = &x;cout << x << “ “ << y << endl;

• What is the result?– 5 0x4FA09E05

• The integer x displays the integer, the pointer y is the address of x. The “&” symbol will return the address of an object.

• The pointer "y" will hold that address

Page 16: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Pointers

• In conclusion– A pointer is a variable that contains the

address of data– The address of operator is “&” and the

pointer symbol is “*”

Page 17: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Vectors and Arrays

• Vectors are used to protect programmers from hurting themselves.– They resize automatically and check for to make sure

you don't put too many things into a vector

• Behind vector is an array. • Arrays are hard to work with though, and vectors

make things easier• When we talk about memory we will use the

word array. • An array is an indexed list.

Page 18: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Arrays

• In C++, you can get dynamic memory from the heap with a new operator and a pointer

new [] returns a block of memory from the heap. How much memory?That depends on the data type of the pointer

To delete the memory when you are done with it, and return it to the heap so that other variables can use that memory, use

delete [] a;

Page 19: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Address Calculation

• x is a pointer. • What is x+33 ?• It’s still a pointer, but it

points to a different place in memory now.

• The distance that it goes in memory depends on the type of data being pointed to.

• A int will point to 32 bits of data, but a double points to 64

Page 20: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Pointer Arithmetic

Page 21: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Pointers and Arrays

Page 22: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Java Notes

• Note: We can’t do this in Java (showing the addresses of actual variables)

• This is because Java only determines the address of variables when the program is run.

• This way makes it more secure. You cannot try to take over memory from other pieces of software

• But all Java variables and memory is done dynamically. • The Java Virtual Machine will make sure that all memory

is correctly used and deleted when it should be

Page 23: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Strings as arrays

• What are strings?– A list of characters put together in memory– The sentence: Hello World!!

! \0!DLROWO

LLEH

1310 141211987654321

•What’s the thing at the end?•It’s a null terminator, which tells the computer that the string is finished.

•Each character is one byte of data (in ASCII)

Page 24: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Bit Manipulations• You should have a general understanding about

how memory and data is organized within a computer.

• This data is all stored in binary and in 32 bit words (4 bytes).

• Sometimes, a program may need to look at in individual bit of data.

• For example, a mouse sends one word of data to the computer that describes the x and y coordinates

• What part of the 32 bit word is for x and y? • To find and use them, you need to be able to work

with individual bits

Page 25: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Bitwise AND (&) and OR (|)

• The “&” symbol in programming will do a bitwise AND operation.

• It means it will give a logical AND operation on two numbers by their individual bits

• The “|” symbol in programming will do a bitwise OR operation.

• Meaning, it will look at an integer, one bit at a time, and apply the OR binary operation

Page 26: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Bitwise

• OR and AND take two bits and output the result• AND will return a true if both inputs are true,

false otherwise• OR will return true if at least one input is true

Page 27: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Bitwise

• You can even take a bitwise AND or OR on a larger number

• Example

1001101 101011101

& 0101110 | 010110110

= =

Page 28: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Mouse example

• A mouse will send the computer 32 bits of data– Bits 0-7 are for the x coordiniate– Bits 8-15 are for y coordinate– Bits 16-17 are for what mouse button was

clicked– We can ignore bits 18-31

• What if we want to get the x coordinate from the data?

Page 29: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Mouse example

• To get only a few bits of data from a number, we can use the AND operation

• This is called an AND "bit mask." • It means we can block out and mask the numbers we don't want to

use• To get the x coordinate, use a

– 0x000ff bit mask (0000 0000 0000 1111 1111)– x_position = 0x1a34c & 0x000ff

Page 30: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Mouse example

• Can you create bit masks for the y coordinate and the button information?

• Notice the y coordinate is located in the middle of the number.

• What's needed next is a bit-wise operation that can move bits around

Page 31: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

The shift operator• What is this shifting?

– Make numbers bigger or smaller in value by moving bits left or right

– “>>” will shift bits right

– “<<“ will shift bits left

– ( 5 << 3 ) = 01012 << 0112 - means shift the one by three bits to the left

– ( 5 << 3) = 01012 << 0112 = 01010002 = 40

– 5 * 2^3 = 5 * 8 = 40

Page 32: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Shifting

• What happens if we shift the other direction?– 32 >> 2 = 1000002 >> 102

– Shift 32 to by 2 bits to the right– Need to move the numbers to the right =

0010002 = 8

• Shifting is really multiplying or dividing by a power of 2– 32 >> 2 = 8 2^5 / 2^2 = – 32 / 4 = 8 = 2^3

Page 33: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Back to the mouse• If you remember the mouse example, the

y coordinate and the button info were far away from the lowest order bit

You can get the real y position and button position with shifting and masks

Page 34: IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education

Summary

• What computer memory looks like

• What a pointer is

• Pointer arithmetic

• Bitwise operations