140 likes | 287 Views
This comprehensive overview of computer organization delves into registers, instructions, and data movement within a processor. It covers fundamental instructions such as data movement (MOV), arithmetic operations (ADD, SUB, MUL, DIV), logical operations (AND, OR, NOT, XOR), and comparison instructions (CMP). The guide explores various data types and ranges, including bytes, words, and dwords, along with multiplication and division operations for both signed and unsigned integers. Additionally, it introduces function calls, recursion, and foundational data structures like linked lists and binary search trees.
E N D
Instructions • Data movement instructions • Mov • Arithmetic operations • Add, Sub, Mul, Div • Logical operations • And, Or, Not, Xor, Shr, Shl • Comparison instructions • Cmp • Control Transfer Instructions • Unconditional: Jmp • Conditional: Jg, Jl, Jge, Jle, Jne
Data Ranges and Data Types • Data Ranges • E.g. 4 bits: • Unsigned: from 0 to 15 • 1’s complement: from -7 to 7 • 2’s complement: from -8 to 7 • Data Types • Byte: 8 bits • Word: 16 bits • Dword: 32 bits
Multiplication Operation • Unsigned multiplication: MUL • Code: MUL Operand • If Operand is a byte, e.g. MUL BL, thenAX = AL*Operand • If Operand is a word, e.g., MUL BX, thenDX:AX = AX*Operand • If Operand is a dword, e.g., MUL EBX, thenEDX:EAX = EAX*Operand • Signed multiplication: IMUL • (look up the code table)
Division Operation • Unsigned division: DIV • Code: DIV Operand • If Operand is a byte, e.g. DIV BL, thenAL = AX/Operand AH = Rest • If Operand is a word, e.g., DIV BX, thenAX = DX:AX/Operand DX = Rest • If Operand is a dword, e.g., DIV EBX, thenEAX = EDX:EAX/Operand EDX = Rest • Signed division: IDIV • (look up the code table)
Stack Push/Pop Examples • … run out of registersPush EAX //save EAX on the stackPush EBX //save EBX on the stack … use EAX and EBX for some calculations …Pop EBX //move top of the stack to EBXPop EAX //move top of the stack to EAX to restore its original value • Note the orders of PUSH and POP are reversed
Function Call __declspec(naked) int add_func(int param1) { __asm{ PUSH ebx; PUSH ecx; // 1. Access param1 MOV ebx, dword ptr[esp+12]; MOV ecx, ebx; ADD ecx, 5; // 2. The return value should be placed in EAX MOV eax, ecx; POP ecx; POP ebx; // 3. Return to the caller RET; } } void main() { int result = 0; __asm{ PUSH eax; PUSH ebx; PUSH ecx; // 1. Pass parameter(s) to the procedure MOV ebx, 10; PUSH ebx; // 2. Execute call CALL add_func; // 3. Remove parameter(s) from the stack POP ebx; // 4. EAX has the return value MOV result, eax; POP ecx; POP ebx; POP eax; } }
Function (cont.) void main() { int result = 0; __asm{ PUSH eax; PUSH ebx; PUSH ecx; // 1. Pass parameter(s) to the procedure MOV ebx, 10; PUSH ebx; // 2. Execute call CALL add_func; // 3. Remove parameter(s) from the stack POP ebx; // 4. EAX has the return value MOV result, eax; POP ecx; POP ebx; POP eax; } }
Function (cont.) __declspec(naked) int add_func(int param1) { __asm{ PUSH ebx; PUSH ecx; // 1. Access param1 MOV ebx, dword ptr[esp+12]; MOV ecx, ebx; ADD ecx, 5; // 2. The return value should be placed in EAX MOV eax, ecx; POP ecx; POP ebx; // 3. Return to the caller RET; } }
Number Systems • Decimal (10) • Binary (2) • Hexadecimal (16) • Octal (8) • Conversion between them
Logical Operations • Get a bit • Using AND/OR • Count number of ones • Using DIV • Using AND and SHR • Mirror a byte
Recursion • Recursion in computer programming is exemplified when a function is defined in terms of itself. • Base case • Recursive case • E.g. Factorial(n) as: • Base case: • n=0 or n=1: Factorial(n)= 1 • Recursive case: • n>1 : n * Factorial(n-1)
Data Structures • Linked List • Element has • Value • Pointer to next element • Binary Search Tree • Element has • Value • Pointer to left child • Pointer to right child • Value of left subtree < root < right subtree • Insert and Traverse