100 likes | 255 Views
Multiplication and Division Instructions & the 0Ah function. IMUL Instruction (signed multiplication) (1).
E N D
Multiplication and Division Instructions & the 0Ah function
IMUL Instruction (signed multiplication)(1) • Newer forms of IMUL instruction allow use of immediate operands. (There isno 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 * immed2 operandsIMUL 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.
IMUL Instruction (2) • Operand:
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: Nfor N times do factorial = factorial * N N = N - 1 (loop instruction)end for
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 = factorialFACTORIAL ENDP
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:
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.
Interpretation • Mnemonic OPCODE • Effect • Convert Byte to Word • CBW • sign extends AL into AX • Convert Word to Doubleword • CWD • sign extends AX into DX:AX • Convert Word to Extended Double • CWDE • sign extends AX into EAX • Convert Doubleword to Quadword • CDQ • sign extends EAX into EDX:EAX CBW, CWD, CDQ, CWDE Instructions(1) • These instructions perform the following sign-extensions:
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 = 0AHDS:DX gives the address for the buffer for the keyboard input. The first byteof 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 filledin 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.
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 thekeyboard is stored starting at the address given by BUFFER, and the no. of bytes read in including the carriage return is stored in ACTUAL.