Virtual Memory Layout FFFF S t a c

  • Slides: 59
Download presentation
Virtual Memory Layout FFFF S t a c k Break (Heap) . text. bss.

Virtual Memory Layout FFFF S t a c k Break (Heap) . text. bss. data Operating System Reserved 0

How The Sections Work. data . text Instruction 1 Instruction 2 Op. Code Operand

How The Sections Work. data . text Instruction 1 Instruction 2 Op. Code Operand 1 Operand 2 ADD 0101 11110000 Code a=1 b=1 a+b Variable b @ (11110000) Variable a @(0101) 00000001

Indirection. text. data Instruction 1 Instruction 2 Op. Code Operand 1 Operand 2 ADD

Indirection. text. data Instruction 1 Instruction 2 Op. Code Operand 1 Operand 2 ADD 11110011 1111 Variable d @ (1111) Code a=1 b=1 c* = &a d* = &b c* + d* 11110000 Variable c @ (11110011) 0101 Variable b @ (11110000) 00000001 Variable a @(0101) 00000001 I n d i r e c t

Example A “C” “Array” Code my. Array = array() i=1 my. Array[i] = 0

Example A “C” “Array” Code my. Array = array() i=1 my. Array[i] = 0 my. Array[i] == my. Array[1] == my. Array + 1 @ (11110001) Index value (indirection) 0000 my. Array @ (11110000) i @(0101) 00000001

Back To Memory Code a=0 p* =&a a =Actual pointed variable = Whole_Memory_Array[p]@ (11110001)

Back To Memory Code a=0 p* =&a a =Actual pointed variable = Whole_Memory_Array[p]@ (11110001) Pointer value (indirection) Pointer p @(0101) Whole_Memory_Array 0000 11110001

Intro To Binary Switch = Bit 8 Switches = 8 Bits = Byte We

Intro To Binary Switch = Bit 8 Switches = 8 Bits = Byte We can have only 0 or 1 Is there any way to get real numbers? 1 = On 0 = Off

Lets Take an Example From Regular (Decimal) Numbers • • • 0 1 2

Lets Take an Example From Regular (Decimal) Numbers • • • 0 1 2 3 4 5 6 7 8 9 ? ? ? We have only 9 numerals So how do we proceed?

Lets Take an Example From Regular (Decimal) Numbers • • • 0 1 2

Lets Take an Example From Regular (Decimal) Numbers • • • 0 1 2 3 4 5 6 7 8 9 ? ? ? • • • 10 11 12 … 20 21 …. 30 …. 99 ? ? ? • • • 100 101 …. 200 …. . The answer is combination

Back To Binary • 0 • 1 • ? ? ? • 10 •

Back To Binary • 0 • 1 • ? ? ? • 10 • 11 • ? ? ? • • • 100 101 110 111 ? ? ? • • 1000 1001 1010 1101 1110 1111 ? ? ? The answer is combination

Binary Compared To Decimal • • • 0 1 2 3 4 5 6

Binary Compared To Decimal • • • 0 1 2 3 4 5 6 7 8 9 …… Binary • • • 00000001 00000010 00000011 00000100 00000101 00000110 00000111 00001000 00001001 ……. . Octal • • • 0 1 2 3 4 5 6 7 10 11 ……

Negative Numbers • Rule 1: Negative has a 1 in front Ex: 100000001 •

Negative Numbers • Rule 1: Negative has a 1 in front Ex: 100000001 • Rule 2: The 2’s complement 1. The 1’s Complement – Xor all bits Ex: Xor: 00001 11110 - (Decimal “ 1”) 2. The 2’s Complement – Add 1 Ex: Xor: Add 1: 00001 111111110 - (Decimal “ 1”) - (Decimal “-1”)

Converting Between Smaller And Larger Types Positive Values Code byte a = 1 short

Converting Between Smaller And Larger Types Positive Values Code byte a = 1 short b = a unsigned byte a = 255 short b = a Correct 00000001 000000001 1111 00001111 Negative Values Wrong byte a = -1 short b = a Correct 1111 000011111111

Identifiers Turned Into Memory Addresses 1. The identifiers that are being turned into memory

Identifiers Turned Into Memory Addresses 1. The identifiers that are being turned into memory addresses: – Global Variables – Functions – Labels 2. The identifiers that are NOT being turned into memory addresses, (are only used to measure the size to reserve): – Custom types – Struct – Class names 3. The identifiers that are used as offsets: – Array index – Class (and struct) field (NOT function) member – Local variables

Variables are a “higher language – human readable” name for a memory address •

Variables are a “higher language – human readable” name for a memory address • Size of the reserved memory is based on the type • There might be the following types 1. 2. 3. 4. 5. 6. 7. Built-in type – which is just a group of bytes, based on the compiler and/or platform Custom type – which is just a series of built in types Array (in C and C++) – which is just a series of the same built-in type (but no one keeps track of the length, so it is the programmers job) Pointer – system defined to hold memory addresses Reference – a pointer without the possibility to access the memory address Handle – a value supplied by the operating system (like the index of an array) Typedef and Define – available in C and C++ to rename existing types

Labels are a “higher language – human readable” name for a memory address •

Labels are a “higher language – human readable” name for a memory address • There is no size associated with it • Location – In assembly it might be everywhere • In fact in assembly it is generally the only way to declare a variable, function, loop, or “else” • In Dos batch it is the only way to declare a function – In C and C++ it might be only in a function – but is only recommended to break out of a nested loop – In VB it is used for error handling “On error goto” – In Java it might only be before a loop

Label Sample In Assembly. data. int var 1: 1 var 2: 10. text. global

Label Sample In Assembly. data. int var 1: 1 var 2: 10. text. global start: mov var 1 %eax call myfunc jmp myfunc: mov var 2 %ebx add %eax %ebx ret

Label Sample In Java (Or C) outer. Label: while(1==1) { while(2==2) { //Java syntax

Label Sample In Java (Or C) outer. Label: while(1==1) { while(2==2) { //Java syntax break outer. Label; //C syntax (not the same as before, as it will cause the loop again) goto outer. Label; } }

Boolean Type • False == 0 (all switches are of) • True == 1

Boolean Type • False == 0 (all switches are of) • True == 1 (switch is on, and also matches Boolean algebra) • All other numbers are also considered true (as there are switches on) • There are languages that require conversion between numbers and Boolean (and other are doing it behind the scenes)) However TWO languages are an exception

Why Some Languages Require conversion • Consider the following C code, all of them

Why Some Languages Require conversion • Consider the following C code, all of them are perfectly valid: if(1==1) if(a) if(a==b) if(a=b) //true as well //Checks if “a” is non-zero //Compares “a” to “b” //Sets “a” to the value of “b”, and then //checks “a” if it is non-zero, Is this by //intention or typo? • However in Java the last statement would not compile, as it is not a Boolean operation • For C there is some avoidance by having constants to the left, ie. if(20==a) Instead of if(a==20) Because if(20=a) Is a syntax error While if(a=20) Is perfectly valid

Implicit Conversion • However some languages employ implicit conversion • Java. Script considers –

Implicit Conversion • However some languages employ implicit conversion • Java. Script considers – If (1) : true (0) : false (“”) : false (“ 0”) : true • Php Considers • If (“ 0”) : false • If (array()) : false

Boolean In Visual Basic • True in VB = -1 • To see why

Boolean In Visual Basic • True in VB = -1 • To see why let us see it in binary – 1 = 00000001 – 1’s complement = 111110 – 2’s complement = 11111 • So all switches are on But why different than all others? To answer that we need to understand the difference between Logical and Bitwise operators, and why do Logical operators short circuit?

Logical vs bitwise • Logical – Step 1 – Check the left side for

Logical vs bitwise • Logical – Step 1 – Check the left side for true – Step 2 – If still no conclusion check the right side – Step 3 – Compare both sides and give the answer • Bitwise – Step 1 – Translate both sides into binary – Step 2 – Compare both sides bit per bit – Step 3 – Provide the soltuion

Example Bitwise vs Logical • Example 1 If 1==1 AND 2==2 Logical And Bitwise

Example Bitwise vs Logical • Example 1 If 1==1 AND 2==2 Logical And Bitwise And 1==1 is true=00000001 2 ==2 is true 2==2 is true=00000001 Step 1: Step 2: Step 3: True And True = True 00000001

More Examples • Example 2 If 1==2 AND 2==2 Logical And Bitwise And Step

More Examples • Example 2 If 1==2 AND 2==2 Logical And Bitwise And Step 1: 1==2 is false=0000 Step 2: return false 2==2 is true =00000001 Step 3: N/A 0000 AND 00000001 0000 • Example 3 If 1 AND 2 Step 1: 1 is True 00000001 Step 2: 2 is True 00000010 Step 3: True 0000

Bitwise vs Logical Operators Operator C Basic VB. Net (Logical) Logical AND && N/A

Bitwise vs Logical Operators Operator C Basic VB. Net (Logical) Logical AND && N/A And. Also Logical OR || N/A Or. Else Logical NOT ! N/A (Bitwise) Bitwise AND & AND Bitwise OR | OR OR Bitwise XOR ^ XOR Bitwise NOT ~ NOT

Back To VB • Since we have only a bitwise NOT we have to

Back To VB • Since we have only a bitwise NOT we have to make sure it works on Boolean NOT On 1 1 = 00000001 = True NOT = 11111110 = True NOT On -1 -1 = 1111 = True NOT = 0000 = False Beware Of The Win 32 API

Boolean In Bash Shell Scripting # if(true) then echo “works”; fi # works #

Boolean In Bash Shell Scripting # if(true) then echo “works”; fi # works # # if(false) then echo “works”; fi # # if(test 1 –eq 1) then echo “works”; fi # works # # if(test 1 –eq 2) then echo “works”; fi # # echo test 1 –eq 1 # # test 1 –eq 1 # echo $? #0 # test 1 –eq 2 # echo $? #1 # # true # echo #? #0 # false # echo #? #1

Simple Function Call Stack Base Pointer Stack Pointer Function func 1() { func 2();

Simple Function Call Stack Base Pointer Stack Pointer Function func 1() { func 2(); } Function func 2() { return; } Code

Simple Function Call – Assembly Code Stack Base Pointer 9997 Stack Pointer 9995 Assembly

Simple Function Call – Assembly Code Stack Base Pointer 9997 Stack Pointer 9995 Assembly Code Func 1: #label Jmp func 2 Func 2: #label Push Base. Pointer = Stack. Pointer #some code Pop Base. Pointer Code Function func 1() { func 2(); } Function func 2() { //Some Code return; }

Simple Function Call – Step 1 Stack 9997 Base Pointer Stack Pointer 9997 9994

Simple Function Call – Step 1 Stack 9997 Base Pointer Stack Pointer 9997 9994 Assembly Code Func 1: #label Jmp func 2 Func 2: #label Push Base. Pointer = Stack. Pointer #some code Pop Base. Pointer Code Function func 1() { func 2(); } Function func 2() { //Some Code return; }

Simple Function Call – Step 2 Stack Base Pointer Stack Pointer 9997 9994 Assembly

Simple Function Call – Step 2 Stack Base Pointer Stack Pointer 9997 9994 Assembly Code Func 1: #label Jmp func 2 Func 2: #label Push Base. Pointer = Stack. Pointer #some code Pop Base. Pointer Code Function func 1() { func 2(); } Function func 2() { //Some Code return; }

Simple Function Call – Step 3 Stack 9997 Base Pointer Stack Pointer 9995 Assembly

Simple Function Call – Step 3 Stack 9997 Base Pointer Stack Pointer 9995 Assembly Code Func 1: #label Jmp func 2 Func 2: #label Push Base. Pointer = Stack. Pointer #some code Pop Base. Pointer Code Function func 1() { func 2(); } Function func 2() { //Some Code return; }

Function Call Stack Base Pointer Stack Pointer Code Function func 1() { func 2(5,

Function Call Stack Base Pointer Stack Pointer Code Function func 1() { func 2(5, 10); } Function func 2(int x, int y) { int a; int[10] b; //Some code return; }

Function Call – Assembly Code Stack Base Pointer 9997 Stack Pointer 9995 Assembly Code

Function Call – Assembly Code Stack Base Pointer 9997 Stack Pointer 9995 Assembly Code Func 1: #label Push 10 Push 5 Jmp func 2 Function func 1() { func 2(5, 10); } Func 2: #label Push Base. Pointer = Stack. Pointer Sub 10 #some code Add 10 Add 1 Pop Base. Pointer Function func 2(int x, int y) { int a; int[10] b; //Some code return; }

Function Call – Step 1 Stack 9997 Base Pointer Stack Pointer Assembly Code 10

Function Call – Step 1 Stack 9997 Base Pointer Stack Pointer Assembly Code 10 5 9993 Code Func 1: #label Push 10 Push 5 Jmp func 2 Function func 1() { func 2(5, 10); } Func 2: #label Push Base. Pointer = Stack. Pointer Sub 10 #some code Add 10 Add 1 Pop Base. Pointer Function func 2(int x, int y) { int a; int[10] b; //Some code return; }

Function Call – Step 2 Stack 9997 Base Pointer Stack Pointer Assembly Code 10

Function Call – Step 2 Stack 9997 Base Pointer Stack Pointer Assembly Code 10 5 9997 9992 Code Func 1: #label Push 10 Push 5 Jmp func 2 Function func 1() { func 2(5, 10); } Func 2: #label Push Base. Pointer = Stack. Pointer Sub 10 #some code Add 10 Add 1 Pop Base. Pointer Function func 2(int x, int y) { int a; int[10] b; //Some code return; }

Function Call – Step 3 Stack Base Pointer Stack Pointer Assembly Code 10 5

Function Call – Step 3 Stack Base Pointer Stack Pointer Assembly Code 10 5 9997 9992 Code Func 1: #label Push 10 Push 5 Jmp func 2 Function func 1() { func 2(5, 10); } Func 2: #label Push Base. Pointer = Stack. Pointer Sub 10 #some code Add 10 Add 1 Pop Base. Pointer Function func 2(int x, int y) { int a; int[10] b; //Some code return; }

Function Call – Step 4 Stack Base Pointer Stack Pointer Assembly Code 10 5

Function Call – Step 4 Stack Base Pointer Stack Pointer Assembly Code 10 5 9997 9992 9981 Code Func 1: #label Push 10 Push 5 Jmp func 2 Function func 1() { func 2(5, 10); } Func 2: #label Push Base. Pointer = Stack. Pointer Sub 10 #some code Add 10 Add 1 Pop Base. Pointer Function func 2(int x, int y) { int a; int[10] b; //Some code return; }

Function Call – Step 5 Stack Base Pointer Stack Pointer Assembly Code 10 5

Function Call – Step 5 Stack Base Pointer Stack Pointer Assembly Code 10 5 9997 9992 Code Func 1: #label Push 10 Push 5 Jmp func 2 Function func 1() { func 2(5, 10); } Func 2: #label Push Base. Pointer = Stack. Pointer Sub 10 #some code Add 10 Add 1 Pop Base. Pointer Function func 2(int x, int y) { int a; int[10] b; //Some code return; }

Function Call – Step 6 Stack 9997 Base Pointer Stack Pointer Assembly Code 10

Function Call – Step 6 Stack 9997 Base Pointer Stack Pointer Assembly Code 10 5 9993 Func 1: #label Push 10 Push 5 Jmp func 2 Add 2 Func 2: #label Push Base. Pointer = Stack. Pointer Sub 10 #some code Add 10 Add 1 Pop Base. Pointer Code Function func 1() { func 2(5, 10); } Function func 2(int x, int y) { int a; int[10] b; //Some code return; }

Function Call – Step 6 Stack 9997 Base Pointer Stack Pointer 9995 Assembly Code

Function Call – Step 6 Stack 9997 Base Pointer Stack Pointer 9995 Assembly Code Func 1: #label Push 10 Push 5 Jmp func 2 Add 2 Stack. Pointer Func 2: #label Push Base. Pointer = Stack. Pointer Sub 10 Stack. Pointer #some code Add 10 Stack. Pointer Add 1 Stack. Pointer Pop Base. Pointer Code Function func 1() { func 2(5, 10); } Function func 2(int x, int y) { int a; int[10] b; //Some code return; }

Function call - summary Call 1. 2. 3. 4. Arguments are copied (passed by

Function call - summary Call 1. 2. 3. 4. Arguments are copied (passed by value) on the stack from right to left (In the C calling convention) The return address is pushed The old base pointer is pushed We make place for local variables (but is not intialized and contans “Garbage”) Access 1. 2. All access to local variables or parameters are relative to the base pointer i. e the compiler does not transalate “local. Var” to “FF 55”, but to “Base. Pointer - 1” Return 1. 2. 3. 4. Remove the space from the local variables (everything there is destroyed) The old base pointer is restored The return address is poped and jumped to. Parameters are destroyed

The Heap FFFF S t a c k Break (Heap) C Code Int* my.

The Heap FFFF S t a c k Break (Heap) C Code Int* my. Ptr = NULL; my. Ptr = alloc(sizeof(int)); If(my. Ptr == NULL) exit(1); //Some code free(my. Ptr); Points here after malloc and new. text. bss my. Ptr . data ? ? ? Operating System Reserved 0 C++ Code Int* my. Ptr = NULL; my. Ptr = new(sizeof(int)); If(my. Ptr == NULL) exit(1); //Some code delete(my. Ptr);

Heap vs Stack Summary • All types of objects can be allocated both on

Heap vs Stack Summary • All types of objects can be allocated both on the stack and on the heap • To use the heap we must have a pointer and then call “malloc” or “new” • Objects created on the stack are automatically destroyed when the function exists (unless with the keyword static in C which actually creates in the data section) • Objects created on the heap, must be explicitly freed with “free”, or “malloc” – IF the pointer is destroyed then we are stuck (or if we just forgat)

Copying. data Variable d @ (1111) Code a=1 b=a c* = &a d* =

Copying. data Variable d @ (1111) Code a=1 b=a c* = &a d* = c 0101 Variable c @ (11110011) 0101 Variable b @ (11110000) 00000001 Variable a @(0101) 00000001 I n d i r e c t

Copying And Then Changing. data Variable d @ (1111) Code a=1 b=a b =

Copying And Then Changing. data Variable d @ (1111) Code a=1 b=a b = 2 //Not affecting a c* = &a d* = c 0101 Variable c @ (11110011) 0101 Variable b @ (11110000) 00000010 Variable a @(0101) 00000001 I n d i r e c t

Copying Pointers And Then Changing Value. data Variable d @ (1111) Code a=1 b=a

Copying Pointers And Then Changing Value. data Variable d @ (1111) Code a=1 b=a b = 2 //Not affecting a c* = &a d* = c (*d) = 3 0101 Variable c @ (11110011) 0101 Variable b @ (11110000) 00000010 Variable a @(0101) 00000011 I n d i r e c t

Copying Pointers And Then Changing Pointer Value. data Variable d @ (1111) Code a=1

Copying Pointers And Then Changing Pointer Value. data Variable d @ (1111) Code a=1 b=a b = 2 //Not affecting a c* = &a d* = c (*d) = 3 d = &b (*d) = 4 0101 Variable c @ (11110011) 0101 Variable b @ (11110000) 00000100 Variable a @(0101) 00000011 I n d i r e c t

Copying Summary • Copying value variables, just copy the value, any change after to

Copying Summary • Copying value variables, just copy the value, any change after to one does NOT affect to the other • Copying pointer variables, both point to the same value variable – Any change to the value variable via one of the pointers IS reflected to the other – Any changes to the pointer value of one is NOT reflected by the other pointer, and also not affect the original value variable

Pointer vs Reference • Problems with pointers, stack and heap so far: – A

Pointer vs Reference • Problems with pointers, stack and heap so far: – A pointer can be directly (by intention or accidently) changed to access any memory – Failure to free the allocated heap results in a memory leak – Forgetting to free the allocated heap till the function returns, then there is no longer a way to free it – Creating a large object on the stack is not worth, and of course not to pass by value

References • Reference is the same as a pointer but without the ability to

References • Reference is the same as a pointer but without the ability to change the memory address, only possible to change pointing from one object of the same type to another Code object my. Var = new object(); object my. Var 2 = my. Var; //my. Var 2 points to the same object as my. Var = new object(); //Now my. Var points to a new object, while my. Var 2 points to the old • A primitive variable is in general a value type and cannot be NULL • A object is generally a reference and is NULL as long there is no call to “new()” • Now the compiler can keep track of what is referenced by what

Garbage Collection • Garbage Collection means to free heap memory no longer in use

Garbage Collection • Garbage Collection means to free heap memory no longer in use • In COM and VB 6 (based on COM) it is done by keeping a count of the variables that still reference the object – For each new reference it is incremented, and for each out of scope or null or just change in reference it is decremented – When the reference count reaches 0 it is deleted – Problem if two objects just reference each other, they will never be deleted • In Java and. Net the JVM and CLR keep track of objects that are not referenced by real references

Null Pointer And DB Null • A pointer or reference not pointing anywhere is

Null Pointer And DB Null • A pointer or reference not pointing anywhere is pointing to null • In C NULL is a constant with the value 0 • 0 == Null (in C) and null == null • NULL + “Some. String” = “Some. String” – (but not in Linq To SQL!!! WATCH OUT) • • • In Database it is different NULL is nothing NULL = NULL is also NULL, use IS NULL + “some. String” is NULL WHERE NOT IN(NULL) is corrupting everything SQL Server has an option “SET ANSI NULLS OFF”, and My. SQl has an operator <=> to compare Nulls

Na. N • Division of an integer by 0 is an error • Division

Na. N • Division of an integer by 0 is an error • Division of a floating point by 0 results in Nan IEEE Java Na. N == Na. N = false Na. N != Na. N = false Na. N == Na. N = false Na. N != Na. N = true • Check for Nan by checking If((my. Var == my. Var) == false) If(my. Var. Is. Na. N()) • This does not work for a database NULL == NULL != NULL

Characters

Characters

String

String

String Quoting

String Quoting

Pass By Value vs Pass By Reference

Pass By Value vs Pass By Reference

Immutable Strings

Immutable Strings