120 likes | 187 Views
ICS 312 SET 10. Multiplication & Division & input using function 0Ah. IMUL Instruction (signed multiplication).
E N D
ICS 312 SET 10 Multiplication & Division & input using function 0Ah
IMUL Instruction (signed multiplication) 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, any type of operand ;reg1 = reg1 * 2nd operand 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. i.e any type except immeditate
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 END
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: Unlike the case for IMUL, 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, as shown on the next slide, 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(Not needed for IDIV) CWDE sign extends AX into EAX Convert Doubleword to Quadword CDQ sign extends EAX into EDX:EAX CBW, CWD, CDQ, CWDE Instructions • These instructions perform the following sign-extensions:
EXAMPLES Given that M, N, U, and V are all word variables, to divide M by N, and put the quotient in U, and the remainder in V: mov ax, M cwd ; sign extends M into dx:ax idiv N ; divides dx:ax by N mov U, ax ; the quotient mov V, dx ; the remainder
Given that X, Y, Z, U, V are all word variables, to evaluate (X*Y*Z) / (U*V) ignoring remainders produced in divisions: mov ax, X ; for imul, no need to init. dx imul Y ; X*Y is now in dx:ax idiv U ; (X*Y)/U is in ax imul Z ; (X*Y*Z)/U is in dx:ax idiv V ; (X*Y*Z)/(U*V) is in ax The result is in ax
Reading an Entire Line with 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 area” 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 area will contain the count of the actual number of characters typed, not including the carriage return. This number is filledin by DOS after the string, including the carriage return has been entered. The remaining bytes of the buffer area will contain the ASCII keyboard data entered, 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 fn. 0Ah. The labels shown are optional. .data MAX db 15 ; allows a max. of 14 characters plus enter key (0Dh) ACTUAL db ? ; value to be filled in by 0Ah fn – the no. of chars. read BUFFER db 15 dup (?) ; place where the characters are read in .code mov ah, 0Ah ; input string function lea dx, MAX ; address of input buffer int 21h ; read string This assigns convenient 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 will be stored starting at the address labelled BUFFER, and the no. of bytes read in, including the enter key, will be stored in ACTUAL.