Upload
nikita
View
39
Download
2
Embed Size (px)
DESCRIPTION
ICS 51 - Introductory Computer Organization. Review. Registers. 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 - PowerPoint PPT Presentation
Citation preview
ICS 51 - Introductory ICS 51 - Introductory Computer OrganizationComputer OrganizationReview
RegistersRegisters
InstructionsInstructionsData movement instructions
◦MovArithmetic operations
◦Add, Sub, Mul, DivLogical operations
◦And, Or, Not, Xor, Shr, ShlComparison instructions
◦CmpControl Transfer Instructions
◦Unconditional: Jmp◦Conditional: Jg, Jl, Jge, Jle, Jne
Data Ranges and Data Data Ranges and Data TypesTypesData 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 OperationMultiplication OperationUnsigned multiplication: MUL
◦ Code: MUL Operand If Operand is a byte, e.g. MUL BL, then
AX = 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 OperationDivision OperationUnsigned division: DIV
◦Code: DIV Operand If Operand is a byte, e.g. DIV BL, then
AL = 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 ExamplesStack Push/Pop Examples… run out of registers
Push 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
8
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 callCALL add_func;
// 3. Remove parameter(s) from the stack
POP ebx;
// 4. EAX has the return valueMOV result, eax;
POP ecx;POP ebx;POP eax;
}}
__declspec(naked)int add_func(int param1){ __asm{
PUSH ebx;PUSH ecx;
// 1. Access param1MOV 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 callerRET;
}}
Function CallFunction Call
9
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 callCALL add_func;
// 3. Remove parameter(s) from the stack
POP ebx;
// 4. EAX has the return valueMOV result, eax;
POP ecx;POP ebx;POP eax;
}}
ECX ESP
EBX ESP+4
EAX ESP+8
10 (param1) ESP
ECX ESP+4
EBX ESP+8
EAX ESP+12
Return Addr ESP
10 (param1) ESP+4
ECX ESP+8
EBX ESP+12
EAX ESP+16
ECX ESP
EBX ESP+4
EAX ESP+8
Function (cont.)Function (cont.)
10
Return Addr ESP
param1 ESP+4
ECX ESP+8
EBX ESP+12
EAX ESP+16__declspec(naked)int add_func(int param1){ __asm{
PUSH ebx;PUSH ecx;
// 1. Access param1MOV 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 callerRET;
}}
ECX ESP
EBX ESP+4
Return Addr ESP+8
param1 ESP+12
ECX ESP+16
EBX ESP+20
EAX ESP+24
Return Addr ESP
param1 ESP+4
ECX ESP+8
EBX ESP+12
EAX ESP+16
param1 ESP+4
ECX ESP+8
EBX ESP+12
EAX ESP+16
Function (cont.)Function (cont.)
Number SystemsNumber SystemsDecimal (10)Binary (2)Hexadecimal (16)Octal (8)
◦Conversion between them
Logical OperationsLogical OperationsGet a bit
◦Using AND/OR
Count number of ones◦Using DIV◦Using AND and SHR
Mirror a byte
RecursionRecursionRecursion in computer
programming is exemplified when a function is defined in terms of itself. 1. Base case2. 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 StructuresData StructuresLinked 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