Dis. 3 - University of California, Irvine

Download Report

Transcript Dis. 3 - University of California, Irvine

ICS 51 – Discussion 3
Multiplication, Division, and Stack
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)
MUL Examples
Mov
 Mov
 Mov
 Mul


EAX, 0 // Clear EAX
AL, 5 //First number
BL, 10 //Second number
BL //Multiply 2 numbers
Where and what is the result?
◦ AX = AL*BL = 50
MUL Examples (cont.)
Mov
 Mov
 Mov
 Mov
 Mul


EDX, 0 //clear EDX
EAX, 0 //clear EAX
AX, 4460 //first number
BX, 1000 //second number
BX //multiply 2 numbers
Where and what is the result?
◦ DX:AX = AX*BX = 4,460,000
◦ 4,460,000 = 440DE0 in Hex:
 DX has 0x0044, AX has 0x0DE0
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)
DIV Example
Mov
 Mov
 Shr
 Mov
 Div


EDX, 70001 //dividend
AX, DX //move lower 2 bytes to AX
EDX, 16 //DX has the higher 2 bytes
CX, 2 //divisor
CX //divide 70001 by 2
Result:
◦ Quotient = AX = DX:AX / CX = 35000
◦ Remainder = DX = 1
Why Using Stack

What if we run out of registers?

What if the registers you are going to use
are currently used by other functions?

Answers = PUSH and POP
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
Stack Push/Pop Examples (cont.)

void functionXXX()
{
__asm
{
Push EAX
Push EBX
…
Pop EBX
Pop EAX
}
}