1 / 9

ICS 51 – Discussion 3

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

alva
Download Presentation

ICS 51 – Discussion 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ICS 51 – Discussion 3 Multiplication, Division, and Stack

  2. 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)

  3. MUL Examples • Mov EAX, 0 // Clear EAX • Mov AL, 5 //First number • Mov BL, 10 //Second number • Mul BL //Multiply 2 numbers • Where and what is the result? • AX = AL*BL = 50

  4. MUL Examples (cont.) • Mov EDX, 0 //clear EDX • Mov EAX, 0 //clear EAX • MovAX, 4460 //first number • Mov BX, 1000 //second number • MulBX //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

  5. 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)

  6. DIV Example • Mov EDX, 70001 //dividend • Mov AX, DX //move lower 2 bytes to AX • Shr EDX, 16 //DX has the higher 2 bytes • Mov CX, 2 //divisor • Div CX //divide 70001 by 2 • Result: • Quotient = AX = DX:AX / CX = 35000 • Remainder = DX = 1

  7. 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

  8. 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

  9. Stack Push/Pop Examples (cont.) • void functionXXX(){ __asm { Push EAX Push EBX … Pop EBX Pop EAX }}

More Related