Transcript Slide 1

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
◦ 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, then
AX = AL*Operand
 If Operand is a word, e.g., MUL BX, then
DX:AX = AX*Operand
 If Operand is a dword, e.g., MUL EBX, then
EDX: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, then
AL = AX/Operand
AH = Rest
 If Operand is a word, e.g., DIV BX, then
AX = DX:AX/Operand
DX = Rest
 If Operand is a dword, e.g., DIV EBX, then
EAX = EDX:EAX/Operand
EDX = Rest

Signed division: IDIV
◦ (look up the code table)
Stack Push/Pop Examples

… run out of registers
Push EAX //save EAX on the stack
Push EBX //save EBX on the stack
… use EAX and EBX for some calculations …
Pop EBX //move top of the stack to EBX
Pop EAX //move top of the stack to EAX to
restore its original value

Note the orders of PUSH and POP are reversed
Function Call
void main()
{
int result = 0;
__asm{
PUSH eax;
PUSH ebx;
PUSH ecx;
__declspec(naked)
int add_func(int param1)
{
__asm{
PUSH ebx;
PUSH ecx;
// 1. Access param1
MOV ebx, dword ptr[esp+12];
// 1. Pass parameter(s) to the procedure
MOV ebx, 10;
PUSH ebx;
MOV ecx, ebx;
ADD ecx, 5;
// 2. Execute call
CALL add_func;
// 2. The return value should be placed in EAX
MOV eax, ecx;
// 3. Remove parameter(s) from the stack
POP ebx;
POP ecx;
POP ebx;
// 4. EAX has the return value
MOV result, eax;
POP ecx;
POP ebx;
POP eax;
// 3. Return to the caller
RET;
}
}
}
}
8
Function (cont.)
void main()
{
int result = 0;
__asm{
ECX
ESP
EBX
ESP+4
PUSH eax;
PUSH ebx;
PUSH ecx;
EAX
ESP+8
10 (param1)
ESP
// 1. Pass parameter(s) to the procedure
MOV ebx, 10;
PUSH ebx;
ECX
ESP+4
EBX
ESP+8
// 2. Execute call
CALL add_func;
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
// 3. Remove parameter(s) from the stack
POP ebx;
// 4. EAX has the return value
MOV result, eax;
POP ecx;
POP ebx;
POP eax;
}
}
9
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;
}
}
Return Addr
ESP
param1
ESP+4
ECX
ESP+8
EBX
ESP+12
EAX
ESP+16
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
10
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.
1. Base case
2. 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