CS 2505 Exam 2

¡Supera tus tareas y exámenes ahora con Quizwiz!

What are the major registers and what are they used for?

%rax - the accumulator - used to accumulate results, i.e. used in many of our calculations. - also used to return results from functions. %rbp - the base pointer - this points, or stores the address, of the start of our stack frame. Its contents is an address that is higher, or greater than, that that is stored in the %rsp. %rsp - the stack pointer - this points to, or stores the address, of the end of our stack frame. Its contents are an address that is smaller than that that is stored in the %rbp

What is the %rsp? %rbp?

%rbp - the base pointer - this points, or stores the address, of the start of our stack frame. Its contents is an address that is higher, or greater than, that that is stored in the %rsp. %rsp - the stack pointer - this points to, or stores the address, of the end of our stack frame. Its contents are an address that is smaller than that that is stored in the %rbp

How does quoting effect a variable?

- Single quotes and double quote do different things - Single quotes will prevent all variable expansion echo '$PATH'-> $PATH - Double quote allow for the expansion of variables echo "$PATH" -> whatever is in my path

What an unconditional jump depends on uninitialized value means in valgrind

- String char star and forget to get the null terminator, gets to the end and doesn't find the null terminator - MESA forget to initialize struct. You need to initialize things

How do you use realloc and what is it used for?

- realloc will attempt to resize the current memory to the requested size. - If it can do so in the same memory location now memory is moved - If it cannot, then realloc will: 1. allocate new memory of the requested size somewhere else, 2. copy the existing memory into the new memory, 3. release the old memory, and 4. return a pointer to the new memory

What is the effect of shifting an integer to the left? to the right?

- shifting to the left results in a value that is 2^N times larger than the original value, when N is the number of bits shifted example: x=x<<3; should result in 16*8 (x=16) - shifting to the right is sometimes the same as dividing by 2^N where N is the number of bits we are shifting

What major units does our computer process consist of?

-A memory containing instructions and data -A processing unit to perform arithmetic and logical operations -A control unit to interpret the instructions

What is the process for determining the value of a negative integer?

1. Find the right most 1 in a number. 2. Leave it as a 1. 3. Invert all of the bits to the left of that leftmost 1. 0010 1010 ---> 42 ^ ---> right most 1 1101 0110 ---> leave it alone and invert the others.

What happens when a function is entered? What happens to the base pointer? What happens to the stack pointer? How many bytes are in the stack frame?

1. If parameters are being passed, they have to be moved into the registers used for passing parameter, or placed on the stack if there are more than 6 parameters being passed. 2. The return address has to be stored on the stack which is done with the call command 3. Execute the function 4. Open return, return is continued with the saved instruction address 5. Any return value can be retrieved - NOT COMPLETE

What are the bitwise operators in C?

1. Shift bits left or right (<< or >>) 2. Complement (invert) bits (~) 3. Combine bits using a. AND (&) b. OR (|) c. XOR (^)

What is the first line of a bash script?

First line is known has the hash-bang, hash for # and bang for ! The rest of the line is the path to the interpreter to use for this script. Example: #! /bin/bash

How does the size of the data type impact the range of numbers that can be represented?

If the number uses N bits then the range of numbers is: 0 - 2^N-1 For example: size of data type: 32 bits -> 2^32 = 4,294,967,296 unsigned numbers range: 0 - 4,294,967,295 signed numbers range: -2,147,483,648 - 2,147,483,647 (2's complement)

Who designed our computer architecture?

John von Neuman

Know valgrind basics How to detect memory leak and what those messages mean What an invalid read or write is.

N/A

know basic usage of gdb How to set break points, how to display/print/examine variables, etc. How to run your executable in gdb with arguments.

N/A

What is a pointer in assembly?

Pointers in assembly are a form of indirect address - Essentially the address of what you want is stored and it must be followed before it can be used.

What are the 16 bit registers? 32 bit registers? 64 bit registers?

The 64 bit registers start with the letter r The 32 bit registers start with the letter e The 16 and 8 bit registers are 2 letters long

What is a signed number vs. an unsigned number? Said differently, how does a number being signed or unsigned impact the range of values that can be represented?

Unsigned Numbers: - All positive, there is no sign indicator unsigned int uint32_t - Uses all of the available bits to represent the number - If the number uses N bits then the range of numbers is: 0 - 2^N-1 Signed Numbers - Numbers that use the most significant bit to represent if the number is positive, 0, or negative 1. - Use 2s complement to store the number. -When the left most bit is a 0, then the number is simply the sum of the powers of 2. - When the left most bit is a 1, then the number is negative and we need to convert the number to determine its value

How can different values be represented, e.g. decimal, hex, etc.?

Use $ to represent value Can be in decimal, or hex. - 0xA, or 10

Once you have written your bash script what command must be executed on the file so you can run it?

Use the chmod command, chmod +x info.sh Run it with ./info.sh

What tool do we use to help detect memory leaks?

Valgrind is a tool that can help find memory issues.

What operations are allowed/disallowed on structs?

What can you do with a struct: - Pass them to functions - Return them from functions - Assign one to the other What can't you do with a struct: - Compare them for equality or other relational operators - No I/O - No deep copy - No arithmetic operators

What registers are used for passing parameters? If all of those are used, where do extra parameters go?

When there are 6 or less parameters they are passed in the following registers: Parameter Register First %rdi Second %rsi Third %rdx Fourth %rcx Fifth %r8 Sixth %r9 - After this, the remaining parameters are pushed on the stack in reverse order.

How can you set (make a bit 1) or unset (make a bit 0) certain bits in a number?

You can create a mask that will allow you to do this. 1. Create a variable and set it to 1. 2. Shift that variable to the left to the desired position 3. Complement the mask. 4. AND the mask with the value

What is a byte?

a byte is 8 bits

What are the basic math commands?

addl source, destination # destination = destination + source subl source, destination # destination = destination - source imull source, destination # destination = destination * source negl destination # destination = - destination incl destination # destination = destination + 1 decl destination # destination = destination - 1

What are bitwise operators in assembly?

andl source, destination # destination = destination & source orl source, destination # destination = destination | source xorl source, destination # destination = destination ˆ source notl destination # destination = ~destination - The l represents the size of the operands like before (long)

What is legal to put in a bash script?

anything that can be done on the command line can be done in a shell script

What do the single letter suffixes on our commands mean?

b means byte (8 bytes) s means short (16 bit integer value or 32 bit floating point) w means word (16 bit value) l means long (32 bit integer or 64 bit floating point) q means quad (64 bit value)

How do you create an array statically?

char buffer[100] = {0}; - Statically allocated data is created on the run-time stack

What are the hexadecimal numbers and their related 4 bit binary representations?

check table

What are the various jump commands? What are they checking? e.g. jg is jump greater than...

check table

What direction does our stack "grow"?

down

How do you free a dynamically allocated array?

free(numbers);

What is the endedness of a system?

how to store a multi-byte value.

How do you create an array dynamically?

int *numbers = malloc(size * sizeof(int) ); free(numbers); - Dynamically allocated data is stored on the heap

What is leaq? How is it used?

leaq: Load effective address of source into destination - used to create a pointer variable by moving in the address of a variable, and storing it in rax

For our commands, what is the meaning of the first and second operands?

movl source, destination - This is equivalent to saying destination = source

How can you tell if there is a selection statement? How can you tell if there is an else statement?

not sure

What if you have a pointer (p) to a struct, how do you access the fields in the struct?

p->x = x; p->y = y;

How do you create a struct?

struct point { int x; int y; }; struct point a; //create a as a struct point a.x = 8 //access the x field in a; a.y = 10; //access the y filed in a;

How do you set or retrieve a value from a struct?

struct point a; //create a as a struct point a.x = 8 //access the x field in a; a.y = 10; //access the y filed in a

What is a memory leak?

- A memory leak occurs when memory is allocated but never deallocated. - When memory is allocated, be sure to deallocate it. - For example, if a function creates an array and returns it to another function, that second function is responsible for cleaning the memory.

What are the common bases used in computer science?

- Base 10 (decimal), base 16 (hex), base 8 (octal), and base 2 (binary) - Each digit is multiplied by its based raised to a power. - The power is determined by it's position in the number. 73901 in base 10 is: 7x10^4 + 3x10^3 + 9x10^2 + 0x10^1 + 1x10^0 Base 2, or binary, works the same way 0001 0010 0000 1010 1101 in base 2 is: 1x2^16 + 1x2^13 + 1x2^7 + 1x2^5 + 1x2^3 + 1x2^2 + 1x2^0

What does the leave command do?

- It has to reset the stack pointer and the base pointer so that the previous function can continue. - The base pointer is right above the old base pointer. It was pushed when we started, moving the stack pointer 8 bytes - So we can update the stack pointer to be the current base pointer - Then we can pop the stack into the current base pointer, resetting it to its previous value, also moving the stack pointer up 8 bytes - Now the stack pointer is pointing the return address that the call function pushed

What does the calling function do to call a function? What does the call function do that is "hidden" in the call command?

- It uses the call command - NOT COMPLETE

What happens when you shift a signed integer to the right? an unsigned integer to the right?

- Left shifts will shift in 0s to the least significant bits of the value. - Right shifts will depend if the number is signed or not and if the most significant bit is 1 or 0 - On our systems right shifts will shift in the sign bit.

What types are allowed to be stored in a struct?

- The data does not need to be all of the same data type - Any data type that C supports can be stored in a struct including: a. pointers b. arrays c. other structs

What does the popq command do?

- The popq destination command will store the 8 bytes above the %rsp in the destination and incrementthe %rsp by 8 bytes since q is a quad or 8 byte destination - This is the same as the stack pop in any data structure - The data that is on top of the stack is returned and the top is implicitly changed due to the command - The data is the 8 bytes above the %rsp since the stack grows down

What does the pushq command do?

- The pushq source command will put the given operand on the stack and decrement the %rsp by 8bytes since q is a quad or 8 byte source. - This is the same as a stack push in any data structures - The data is stored and the top is implicitly changed due to the command - The top of the stack is the %rsp

What does the ret command do?

- The ret command will pop the stack and store the result in the %rip - Now the stack pointer is at the same address it was before the call. - The base pointer is back to where it was before the call. - The instruction register contains the address of the next instruction

What are common built-in variables in bash?

- There are several variables that allow you to access parameters to functions $* or $@ : These are slightly different but both give you access to all of the parameters $1, $2, $3, etc are the positional parameters that are passed to you $# :the number of arguments passed $? :the return value from the last command executed, useful if you want to see if the command exited cleanly or not

How can you tell if there is a loop? How can you tell what kind of loop it is?

- You can tell if there is a loop if you keep jumping after a check - if the jmp is after a set of commands, it's a do while loop - if you jump straight to a command and compare and jmp, it's a while loop

What does free do, or not do, to a pointer after it is called?

- free returns the memory that was assigned to a pointer back to the system. - free may only be used on pointers that have been allocated using malloc and friends. - free may only be used 1 time on a pointer. - free does not automatically reset the pointer to NULL - After using free the pointer is still a variable and can be reused, assuming you allocate it new memory - You should always NULL out a pointer after you free it.

What sorts of control structures does bash support?What are the basic syntax for those structures?

- if/elseif/else is done this way: if [[ ]]; then elif [[ ]]; then fi - for loops can be done with a for in construct for i in {1..10}; do ... done does something 10 times - While loops exist and work like we expect them to

What is a struct?

- like classes in java - create structured data - structs organize related information

What was the first processor in the line of x86 processors?

8086

What is a bit?

A bit is the smallest unit of computer memory - It can store one of 2 values, 0, or 1 (binary)

What command(s) is(are) used to represent C control structures in assembly?

A combination of a compare instruction and a jump instruction Compare: - takes 2 operands - subtracts the first operand from the second operand - does not update either operand - sets flags according to result of subtraction compl source, destination destination - source Jump: - checks the results of the flags from the compare - check table for specific jump commands

What is a control structure in general? e.g. not even just in assembly, but what is the definition of a control structure?

A control structure is an instruction that alters the typical sequential flow of control - For example, if statements, while loops, etc.

What is half a byte?

A nybble: 4 bits, half a byte

What major style of assembly syntax do we use?

AT&T Syntax

What is the [ ] notation really doing?

Array names are essentially constant pointers- meaning only the address of the first element is stored- and the location of the array elements may not be moved...it's constant array[0] = 10; //is the same as *(array+0) = 10;

How are negative integers stored in a computer?

As signed ints

How can shifting be used to replace some of the basic math commands?

Assembly has instructions for shifting right or left: sall source, destination # destiniation = destination << source sarl source, destination # destiniation = destination >> source (shifts in sign bit) shll source, destination # destiniation = destination << source (shifts in 0s like sall) shrl source, destination # destiniation = destination >> source (shifts in 0s) Effects of Shifting - Shifting an integer value to the left has the affect of multiplying the operand by 2^n where n is the value used for shifting sall 1, %eax # %eax = %eax << 1, or %eax = %eax * 2 sall 3, %edx # %edx = %edx << 3, or %edx = %edx * 8 (2^3) Looking at the bits %edx is 0000 0000 0000 0000 0000 0000 0000 0101 => 5 base 10 sall 3, %edx% edx is 0000 0000 0000 0000 0000 0000 0010 1000 => 40 base 10 Shifting is faster than doing multiplication in general, so shifting is preferred Right Shifts sarl will not work for division by powers of 2 if 1. The value is interpreted as signed, and the left most bit is 1 2. The value is interpreted as unsigned, and the left most bit is 1

What is big ended? What is little ended?

Big ended: we store the most significant bits at lower addresses Little ended: we store the least significant bits at lower addresses

How can consts be used with pointers? - const targets - const pointers - all const

Can be used in 4 different ways: 1. No const int *ptr; //ptr is a pointer to an int. Both can be changed 2. Constant pointer int * const ptr; //ptr is a constant pointer to an int.//ptr cannot be changed. Target can be. 3. Constant target const int *ptr; //ptr is a pointer to an int constant//ptr can be pointed to a new target, but target cannot be changed 4. Constant pointer and target const int * const ptr;//ptr is a constant pointer to a constant int.//all is contant and cannot be chagned.

What is pointer casting?

Casting a pointer to a pointer of a different size can yield in different amounts of data being dereferenced. - For example, int32_t *ptr;//assign memory to ptr int32_t data = *ptr;//will dereference and retrieve 4 bytes int16_t short_data = *(int16_t*)ptr;//will deference and retrieve 2 bytes int8_t byte_data = *(int8_t*)ptr;//will deference and retrieve 1 byte

How do you convert from one base to another?

Converting Decimal to Binary - Repeatedly divide the decimal number by 2. - Keep a list of the remainders. - That list of remainders, in reverse order, is the base 2 number. Converting Binary to Decimal - Determine position of 1 in number - Add sum all powers of 2 for each 1 in binary number. 1 0010 0000 1010 1101 2^16 + 2^13 + 2^7 + 2^5 + 2^3 + 2^2 + 2^0 = 73901 Converting Decimal to Other bases - Same process as converting decimal to binary just divide by new base - Read remainders in reverse order Converting Hex to Binary - We can directly convert a hex number to binary by replacing the hex number with its equivalent 4 digit binary representation 1 -> 0001 2 -> 0010 120AD -> 0001 0010 0000 1010 1101 Converting Binary to Hex - We can group 4 bits in binary to directly "read" the hex 0001 0010 0000 1010 1101 1 2 0 A D Converting Binary to Octal - We can group 3 bits in binary to directly "read" the octal 010 010 000 010 101 101 -> 2 2 0 2 5 5

What is the general formula for computing an address?

D(Rb, Ri, S) = Rb + (Ri*S) + D D - displacement Rb - Base register Ri - Index register S - scale, 1,2,4, or 8


Conjuntos de estudio relacionados

Nutrition 2750 Chapter 4: Lipids

View Set

True or False/ Naming Careers in Health Care

View Set

Intro to Forensic psych (midterm)

View Set

Chapter 13 Quiz: Positive Externalities and Public Goods

View Set

Control - Six Sigma Statistical Process Control Basics

View Set