Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multiplication and Division Instructions & the 0Ah function.

Similar presentations


Presentation on theme: "Multiplication and Division Instructions & the 0Ah function."— Presentation transcript:

1 Multiplication and Division Instructions & the 0Ah function

2 IMUL Instruction (signed multiplication) (1) Newer forms of IMUL instruction allow use of immediate operands. (There is no corresponding form for the MUL instruction). For each of these instructions, the operands are all the same length. 3 operands: IMUL reg1, reg2/memory, immediate ; reg1 = reg2/mem * immed 2 operands IMUL reg1, reg2/memory/immediate ; reg1 = reg1 * reg2/immed 1 operand IMUL multiplies an 8, 16, or 32-bit signed operand by AL or AX or EAX respectively. It sign-extends the result into the word or DX:AX or EDX:EAX respectively. IMUL multiplier ; multiplier is 8, 16, or 32-bit register or memory operand.

3 IMUL Instruction (2) Operand: Multiplier (explicit)Multiplier (implicit)Result is in: 8-bit reg/mem operand AL AX 16-bit reg/mem operand AX DX:AX 32-bit reg/mem operand EAX EDX:EAX

4 Applications of Multiplication (1) Write a subroutine procedure to compute N! for a positive integer N. Return N! in AX. Definition: for N > 1, N! = N (N-1)*(N-2)…* 1 for N = 1, N! = 1 Algorithm: factorial = 1 (initialization) input: N for N times do factorial = factorial * N N = N - 1 (loop instruction) end for

5 Applications of Multiplication (2) Code: FACTORIAL PROC ; computes N factorial ; input: CX = N ; output: AX = N! MOV AX, 1 ; factorial TOP: iMUL CX ; fact = fact * N LOOP TOP ; decrements N RET ; return to caller: AX = factorial FACTORIAL ENDP

6 IDIV Instruction (signed division) (1) IDIV divides AX, DX:AX, or EDX:EAX (dividend) by an 8, 16, or 32-bit signed register or memory operand (divisor) Syntax: IDIV divisor ; divisor is 8, 16, or 32-bit register or memory operand. Operands: Divisor (explicit)Dividend(implicit)QuotientReminder 8-bit reg/mem operand AX ALAH 16-bit reg/mem operand DX:AX AXDX 32-bit reg/mem operand EDX:EAX EAXEDX

7 IDIV Instruction (signed division) (2) Note: The high byte/word/doubleword of the dividend must be initialized before the division is performed to ensure the correct results are obtained. For signed division, this usually means that the value in the low byte/word/doubleword of the operand must be sign-extended into the high byte/word/doubleword before the division can be performed.

8 CBW, CWD, CDQ, CWDE Instructions (1) InterpretationMnemonic OPCODEEffect Convert Byte to WordCBWsign extends AL into AX Convert Word to DoublewordCWD sign extends AX into DX:AX Convert Word to Extended Double CWDEsign extends AX into EAX Convert Doubleword to Quadword CDQ sign extends EAX into EDX:EAX These instructions perform the following sign-extensions:

9 Reading an Entire Line with an Echo (Function 0Ah) Function 0AH reads an entire line of information --- up to 255 characters from the keyboard. It continues to acquire data until either the enter key (0DH) is typed or the character count expires. Required Input: AH = 0AH DS:DX gives the address for the buffer for the keyboard input. The first byte of the buffer area must contain the maximum number of keyboard characters to be read by this function, including the carriage return at the end of the string. If the number typed exceeds this maximum number, the function stops accepting input until the carriage return is entered. Function 0Ah Output: The second byte of the buffer contains the count of the actual number of characters typed, not including the carriage return. The number is filled in by DOS after the string, including the carriage return has been entered. The remaining bytes in the buffer contain the ASCII keyboard data, including the carriage return character at the end of the string.

10 Example Here is a convenient way to set up the input buffer area to use Function 0Ah: The data: MAX db 15 ACTUAL db ? BUFFER db 15 dup (?) The code: mov ah, 0Ah ; input string function lea dx, MAX ; address of input buffer int 21h ; read string This assigns variable names to the first byte, the second byte, and the beginning of the actual input buffer area. The string read in from the keyboard is stored starting at the address given by BUFFER, and the no. of bytes read in including the carriage return is stored in ACTUAL.


Download ppt "Multiplication and Division Instructions & the 0Ah function."

Similar presentations


Ads by Google