Integer Multiplication and Division - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Integer Multiplication and Division

Description:

we can reuse this computation for the next digit if we modify value: value = value / base ... Store digits in an array as they are discovered ... – PowerPoint PPT presentation

Number of Views:72
Avg rating:3.0/5.0
Slides: 19
Provided by: timma87
Category:

less

Transcript and Presenter's Notes

Title: Integer Multiplication and Division


1
Integer Multiplication and Division
  • Assembly Language Programming
  • University of Akron
  • Dr. Tim Margush

2
Multiply Instruction
  • Signed Multiply
  • IMUL source
  • Unsigned Multiply
  • MUL source
  • source can be a register or memory location (not
    a constant)
  • Byte form
  • AXALsource
  • Word form
  • DXAXAXsource
  • CFOF
  • 1 if answer size gt op size
  • 0 otherwise
  • SF, ZF, AF, and PF
  • Undefined

3
IMUL Example
  • mov BL,0FEh
  • mov AL,0E5h
  • imul BL
  • BL contains -2, AL contains -27
  • After IMUL
  • AX contains 0036h (54d)
  • CFOF0 (result still fits in byte)
  • SF, AF, PF, and ZF are undefined

4
MUL Example
  • mov BL,0FEh
  • mov AL,0E5h
  • mul BL
  • BL contains 254, AL contains 229
  • After MUL
  • AX contains 0E336h (58166d)
  • CFOF1 (result requires a word)
  • SF, AF, PF, and ZF are undefined

5
Another IMUL Example
  • mov AWORD,-136
  • mov AX,6784
  • imul AWORD
  • AWORD contains 0FF78h, AX is 1A80h
  • After IMUL
  • DXAX contains 0FFF1EC00h (-922624d)
  • CFOF1 (result requires a doubleword)

6
Another MUL Example
  • mov AWORD,-136
  • mov AX,6784
  • mul AWORD
  • AWORD is 0FF78h (65400d), AX is 1A80h
  • After MUL
  • DXAX contains 1A71EC00h (443673600d)
  • CFOF1 (result requires a doubleword)

7
One More IMUL Example
  • mov AX,-1
  • mov DX,2
  • imul DX
  • AX is 0FFFFh, BX is 0002h
  • After IMUL
  • DXAX contains 0FFFFFFFEh (-2d)
  • CFOF0 (result still fits in a word)

8
Application Formatted Decimal Input
  • Input a number (sequence of decimal digits)
  • Implement as a procedure
  • Skip leading whitespace, stop processing at next
    whitespace (tab, space, or newline)
  • Return result in AX
  • Return with CF1 if error occurred
  • Errors overflow, illegal digit, no digit
  • Assume unsigned data for this example

9
Conversion Algorithm
  • skip whitespace
  • v 0
  • while (digit d is not whitespace)
  • if (illegal) set illegal flag
  • otherwise process digit
  • v v10 d
  • if (overflow) set overflow flag
  • nextDigit
  • mov ax,10
  • mul valueSoFar
  • jo setOF
  • add ax,digitValue
  • jnc nextDigit
  • setOF incOFFlag
  • jmp nextDigit

10
Divide Instruction
  • Signed Divide
  • IDIV divisor
  • Unsigned Divide
  • DIV divisor
  • divisor can be a register or memory location (not
    a constant)
  • Byte form
  • ALAX / divisor
  • AHAX divisor
  • Word form
  • AXDXAX / divisor
  • DXDXAX divisor
  • All Flags undefined
  • For IDIV
  • sign of dividend sign of remainder

11
Divide Overflow
  • Occurs when result will not fit in Word/Byte
  • Terminates program with Divide Overflow message!
  • Need to anticipate and avoid It will occur if
  • Byte form AHgtdivisor
  • Word form DXgtdivisor

12
Preparing Dividend
  • To divide a word in AX by a word, AX must be
    converted to a doubleword in DXAX
  • If signed CWD (called sign-extension)
  • If unsigned MOV DX,0
  • For byte (in AL) to word (AX) conversion
  • If signed CBW (these do not affect Flags)
  • If unsigned MOV AH,0

13
Formatted Numeric Output
  • Want to produce a sequence of characters
    representing a number in some representation
    scheme
  • decimal, hex, octal, binary are common
    representation schemes
  • decimal format usually implies a sign might be
    shown if the number is negative (assuming we are
    displaying a signed value)

14
Generating the Digits
  • least_significant_digit value base
  • we can reuse this computation for the next digit
    if we modify value value value / base
  • The process repeats until a 0 is obtained or the
    desired number of digits determined
  • Problem we generate the digits in the wrong
    order for display

15
Reversing the Digits
  • Solution A
  • Push digits on stack as they are discovered
  • Pop each digit off the stack and display it
  • Solution B
  • Store digits in an array as they are discovered
  • Display digits from the array in the opposite
    order

16
A Decimal Output Procedure
  • Given an unsigned number in AX, fill character in
    DH and field width in DL
  • Display the decimal representation on the screen,
    right justified in the specified field with the
    specified fill character
  • If DL is smaller than the required number of
    digits, display all digits of the result, no fill)

17
DECOUT Procedure
  • decout proc near
  • Output AX in decimal format
  • DH fill char, DL field width
  • count and push ASCII code of the
  • significant digits onto the stack
  • display fill characters if needed
  • display digits from stack
  • ret
  • decout endp

18
Homework
  • Pg 176
  • parts a, b, and c only of 1, 2, 3, 4
  • 5, 6, and 7b
Write a Comment
User Comments (0)
About PowerShow.com