Procedure,
Multiplication & Division
5/2/2016
EEE 315: MICROPROCESSOR & INTERFACING
Md.Ayaz Masud
Lecturer,Dept of EEE, BUET
Procedures
Decompose the original problem into a
series of sub-problems that are easier to
solve
Main procedure contains the entry to the
program
Main procedure calls one of the other
procedures
These procedures can call each other and a
procedure can also call itself
When one procedure calls another, control
transfers to the called procedure
5/2/2016
EEE 315: MICROPROCESSOR & INTERFACING
Md.Ayaz Masud
Lecturer,Dept of EEE, BUET
Procedures
 Syntax:
name PROC type
;body of the procedure
RET
name ENDP
 Type (near or far) is optional
 Near: the statement that calls the procedure is in
the same segment as the procedure itself. NEAR
is assumed if type is omitted.
 Far: the statement that calls the procedure is in a
different segment.
5/2/2016
EEE 315: MICROPROCESSOR & INTERFACING
Md.Ayaz Masud
Lecturer,Dept of EEE, BUET
Procedures
5/2/2016
EEE 315: MICROPROCESSOR & INTERFACING
Md.Ayaz Masud
Lecturer,Dept of EEE, BUET
Main PROC
CALL PROC1
next instruction
PROC1 PROC
first instruction
RET
Procedures
The RET instruction causes control to
transfer back to the calling procedure.
Every procedure should have a RET
someplace (except the main procedure)
Usually it is the last statement in the
procedure
5/2/2016
EEE 315: MICROPROCESSOR & INTERFACING
Md. Ayaz Masud
Lecturer,Dept of EEE, BUET
CALL and RET
To invoke a procedure, the CALL instruction
is used.
There are two kinds of procedure calls,
direct and indirect.
Direct: CALL name
Indirect: CALL address_expression
address_expression specifies a register or
memory location containing the address of
a procedure.
5/2/2016
EEE 315: MICROPROCESSOR & INTERFACING
Md. Ayaz Masud
Lecturer,Dept of EEE, BUET
Executing CALL instruction
The return address of the calling
program is saved on the stack. This is the
offset of the next instruction after the
CALL statement. CS:IP has segment:
offset of this instruction at the time when
the call is executed
 IP gets the offset address of the first
instruction of the procedure. This
transfers control to the procedure.
5/2/2016
EEE 315: MICROPROCESSOR & INTERFACING
Md. Ayaz Masud
Lecturer,Dept of EEE, BUET
Before CALL
CODE SEGMENT STACK SEGMENT
5/2/2016
EEE 315: MICROPROCESSOR & INTERFACING
Md. Ayaz Masud
Lecturer,Dept of EEE, BUET
OFFSET
address
Content
Main PROC
0010H CALL PROC1
0012H next instruction
…….
PROC PROC1
0200H first instruction
……..
0300H RET
IP
OFFSET
address
Content
00FCH
00FDH
00FE H
00FF H
0100 H
SP
After CALL
CODE SEGMENT STACK SEGMENT
5/2/2016
EEE 315: MICROPROCESSOR & INTERFACING
Md. Ayaz Masud
Lecturer,Dept of EEE, BUET
OFFSET
address
Content
Main PROC
0010H CALL PROC1
0012H next instruction
…….
PROC PROC1
0200H first instruction
……..
0300H RET
IP
SP
OFFSET
address
Content
00FCH
00FDH
00FE H 12H
00FF H 00H
0100 H
Executing RET instruction
Syntax: RET pop_value
The integer argument pop_value is
optional.
For a NEAR procedure, execution of RET
causes the stack to be popped into IP.
If a pop value N is specified, it is added to
SP and thus has the effect of removing N
additional bytes from the stack.
CS:IP now contains the segment:offset of the
return address and control returns to the
calling program.
5/2/2016
EEE 315: MICROPROCESSOR & INTERFACING
Md. Ayaz Masud
Lecturer,Dept of EEE, BUET
Before RET
CODE SEGMENT STACK SEGMENT
5/2/2016
EEE 315: MICROPROCESSOR & INTERFACING
Md. Ayaz Masud
Lecturer,Dept of EEE, BUET
OFFSET
address
Content
Main PROC
0010H CALL PROC1
0012H next instruction
…….
PROC PROC1
0200H first instruction
……..
0300H RET
IP
SP
OFFSET
address
Content
00FCH
00FDH
00FE H 12H
00FF H 00H
0100 H
After RET
CODE SEGMENT STACK SEGMENT
5/2/2016
EEE 315: MICROPROCESSOR & INTERFACING
Md. Ayaz Masud
Lecturer,Dept of EEE, BUET
OFFSET
address
Content
Main PROC
0010H CALL PROC1
0012H next instruction
…….
PROC PROC1
0200H first instruction
……..
0300H RET
IP
SP
OFFSET
address
Content
00FCH
00FDH
00FE H XX
00FF H XX
0100 H
Multiplication Instruction
Syntax:
MUL source (for unsigned multiplication)
IMUL source (for signed multiplication)
If two bytes are multiplied, the product is a
word (16 bits).
If two words are multiplied, the product is a
doubleword (32 bits).
For multiplication of positive numbers both
MUL and IMUL give the same result.
5/2/2016
EEE 315: MICROPROCESSOR & INTERFACING
Md. Ayaz Masud
Lecturer,Dept of EEE, BUET
Multiplication Instruction:
BYTE form
Multiplier in source byte
Multiplicand in AL
16 bit product in AX
The source may be a byte register, a
memory byte, but not a constant
MUL BL
MUL MYBYTE
5/2/2016
EEE 315: MICROPROCESSOR & INTERFACING
Md. Ayaz Masud
Lecturer,Dept of EEE, BUET
Multiplication Instruction:
WORD form
Multiplier in source word
Multiplicand in AX
Most significant 16 bits of product in DX
Least significant 16 bits of product in AX
The source may be a 16 bit register, a
memory word but not a constant
MUL BX
MUL MYWORD
5/2/2016
EEE 315: MICROPROCESSOR & INTERFACING
Md. Ayaz Masud
Lecturer,Dept of EEE, BUET
Multiplication Instruction:
Effect on Flags
SF, ZF, AF, PF : Undefined
CF/ OF:
5/2/2016
EEE 315: MICROPROCESSOR & INTERFACING
Md. Ayaz Masud
Lecturer,Dept of EEE, BUET
Instruction CF/OF
MUL
0, if the upper half of the result is zero
1, otherwise
IMUL
0, if the upper half of the result is the sign
extension of the lower half
1, otherwise
Multiplication Instruction:
Examples
AX=FFFF H, BX=FFFF H
AL=80 H, BL=FF H
5/2/2016
EEE 315: MICROPROCESSOR & INTERFACING
Md. Ayaz Masud
Lecturer,Dept of EEE, BUET
Instruction Decimal
Product
Hex
Product
DX AX CF/OF
MUL BX 4294836225 FFFE0001 FFFE 0001 1
IMUL BX 1 00000001 0000 0001 0
Instruction Decimal
Product
Hex
Product
AH AL CF/OF
MUL BL 32640 7F80 7F 80 1
IMUL BL 128 0080 00 80 1
Division Instruction
 Syntax:
DIV divisor (for unsigned multiplication)
IDIV divisor (for signed multiplication)
 The quotient and the remainder have the same
size as the divisor.
 All the flags are undefined
 Divide Overflow: If the quotient is too long to fit
in the destination, program terminates and the
system displays the message“Divide Overflow”
5/2/2016
EEE 315: MICROPROCESSOR & INTERFACING
Md. Ayaz Masud
Lecturer,Dept of EEE, BUET
Division Instruction:
BYTE form
Divisor is an 8 bit register or memory
byte
16 bit dividend is in AX
8 bit quotient is in AL
8 bit remainder is in AH
5/2/2016
EEE 315: MICROPROCESSOR & INTERFACING
Md. Ayaz Masud
Lecturer,Dept of EEE, BUET
Division Instruction:
WORD form
Divisor is a 16 bit register or memory
word
32 bit dividend is in DX:AX
16 bit quotient is in AX
16 bit remainder is in DX
5/2/2016
EEE 315: MICROPROCESSOR & INTERFACING
Md. Ayaz Masud
Lecturer,Dept of EEE, BUET
Sign Extension of the Dividend
 Word division: If the dividend fits in AX only
– For DIV, DX should be cleared
– For IDIV, DX should be made sign extension of
AX. The instruction CWD (Convert Word to
Doubleword)will do the extension
 Word division: If the dividend fits in AL only
– For DIV, AH should be cleared
– For IDIV, AH should be made sign extension
of AL.The instruction CBW (Convert Byte to
Word) will do the extension
 CBW and CWD has no operands and has no effect
on flags
5/2/2016
EEE 315: MICROPROCESSOR & INTERFACING
Md. Ayaz Masud
Lecturer,Dept of EEE, BUET

Procedure Multiplication Division.pdf

  • 1.
    Procedure, Multiplication & Division 5/2/2016 EEE315: MICROPROCESSOR & INTERFACING Md.Ayaz Masud Lecturer,Dept of EEE, BUET
  • 2.
    Procedures Decompose the originalproblem into a series of sub-problems that are easier to solve Main procedure contains the entry to the program Main procedure calls one of the other procedures These procedures can call each other and a procedure can also call itself When one procedure calls another, control transfers to the called procedure 5/2/2016 EEE 315: MICROPROCESSOR & INTERFACING Md.Ayaz Masud Lecturer,Dept of EEE, BUET
  • 3.
    Procedures  Syntax: name PROCtype ;body of the procedure RET name ENDP  Type (near or far) is optional  Near: the statement that calls the procedure is in the same segment as the procedure itself. NEAR is assumed if type is omitted.  Far: the statement that calls the procedure is in a different segment. 5/2/2016 EEE 315: MICROPROCESSOR & INTERFACING Md.Ayaz Masud Lecturer,Dept of EEE, BUET
  • 4.
    Procedures 5/2/2016 EEE 315: MICROPROCESSOR& INTERFACING Md.Ayaz Masud Lecturer,Dept of EEE, BUET Main PROC CALL PROC1 next instruction PROC1 PROC first instruction RET
  • 5.
    Procedures The RET instructioncauses control to transfer back to the calling procedure. Every procedure should have a RET someplace (except the main procedure) Usually it is the last statement in the procedure 5/2/2016 EEE 315: MICROPROCESSOR & INTERFACING Md. Ayaz Masud Lecturer,Dept of EEE, BUET
  • 6.
    CALL and RET Toinvoke a procedure, the CALL instruction is used. There are two kinds of procedure calls, direct and indirect. Direct: CALL name Indirect: CALL address_expression address_expression specifies a register or memory location containing the address of a procedure. 5/2/2016 EEE 315: MICROPROCESSOR & INTERFACING Md. Ayaz Masud Lecturer,Dept of EEE, BUET
  • 7.
    Executing CALL instruction Thereturn address of the calling program is saved on the stack. This is the offset of the next instruction after the CALL statement. CS:IP has segment: offset of this instruction at the time when the call is executed  IP gets the offset address of the first instruction of the procedure. This transfers control to the procedure. 5/2/2016 EEE 315: MICROPROCESSOR & INTERFACING Md. Ayaz Masud Lecturer,Dept of EEE, BUET
  • 8.
    Before CALL CODE SEGMENTSTACK SEGMENT 5/2/2016 EEE 315: MICROPROCESSOR & INTERFACING Md. Ayaz Masud Lecturer,Dept of EEE, BUET OFFSET address Content Main PROC 0010H CALL PROC1 0012H next instruction ……. PROC PROC1 0200H first instruction …….. 0300H RET IP OFFSET address Content 00FCH 00FDH 00FE H 00FF H 0100 H SP
  • 9.
    After CALL CODE SEGMENTSTACK SEGMENT 5/2/2016 EEE 315: MICROPROCESSOR & INTERFACING Md. Ayaz Masud Lecturer,Dept of EEE, BUET OFFSET address Content Main PROC 0010H CALL PROC1 0012H next instruction ……. PROC PROC1 0200H first instruction …….. 0300H RET IP SP OFFSET address Content 00FCH 00FDH 00FE H 12H 00FF H 00H 0100 H
  • 10.
    Executing RET instruction Syntax:RET pop_value The integer argument pop_value is optional. For a NEAR procedure, execution of RET causes the stack to be popped into IP. If a pop value N is specified, it is added to SP and thus has the effect of removing N additional bytes from the stack. CS:IP now contains the segment:offset of the return address and control returns to the calling program. 5/2/2016 EEE 315: MICROPROCESSOR & INTERFACING Md. Ayaz Masud Lecturer,Dept of EEE, BUET
  • 11.
    Before RET CODE SEGMENTSTACK SEGMENT 5/2/2016 EEE 315: MICROPROCESSOR & INTERFACING Md. Ayaz Masud Lecturer,Dept of EEE, BUET OFFSET address Content Main PROC 0010H CALL PROC1 0012H next instruction ……. PROC PROC1 0200H first instruction …….. 0300H RET IP SP OFFSET address Content 00FCH 00FDH 00FE H 12H 00FF H 00H 0100 H
  • 12.
    After RET CODE SEGMENTSTACK SEGMENT 5/2/2016 EEE 315: MICROPROCESSOR & INTERFACING Md. Ayaz Masud Lecturer,Dept of EEE, BUET OFFSET address Content Main PROC 0010H CALL PROC1 0012H next instruction ……. PROC PROC1 0200H first instruction …….. 0300H RET IP SP OFFSET address Content 00FCH 00FDH 00FE H XX 00FF H XX 0100 H
  • 13.
    Multiplication Instruction Syntax: MUL source(for unsigned multiplication) IMUL source (for signed multiplication) If two bytes are multiplied, the product is a word (16 bits). If two words are multiplied, the product is a doubleword (32 bits). For multiplication of positive numbers both MUL and IMUL give the same result. 5/2/2016 EEE 315: MICROPROCESSOR & INTERFACING Md. Ayaz Masud Lecturer,Dept of EEE, BUET
  • 14.
    Multiplication Instruction: BYTE form Multiplierin source byte Multiplicand in AL 16 bit product in AX The source may be a byte register, a memory byte, but not a constant MUL BL MUL MYBYTE 5/2/2016 EEE 315: MICROPROCESSOR & INTERFACING Md. Ayaz Masud Lecturer,Dept of EEE, BUET
  • 15.
    Multiplication Instruction: WORD form Multiplierin source word Multiplicand in AX Most significant 16 bits of product in DX Least significant 16 bits of product in AX The source may be a 16 bit register, a memory word but not a constant MUL BX MUL MYWORD 5/2/2016 EEE 315: MICROPROCESSOR & INTERFACING Md. Ayaz Masud Lecturer,Dept of EEE, BUET
  • 16.
    Multiplication Instruction: Effect onFlags SF, ZF, AF, PF : Undefined CF/ OF: 5/2/2016 EEE 315: MICROPROCESSOR & INTERFACING Md. Ayaz Masud Lecturer,Dept of EEE, BUET Instruction CF/OF MUL 0, if the upper half of the result is zero 1, otherwise IMUL 0, if the upper half of the result is the sign extension of the lower half 1, otherwise
  • 17.
    Multiplication Instruction: Examples AX=FFFF H,BX=FFFF H AL=80 H, BL=FF H 5/2/2016 EEE 315: MICROPROCESSOR & INTERFACING Md. Ayaz Masud Lecturer,Dept of EEE, BUET Instruction Decimal Product Hex Product DX AX CF/OF MUL BX 4294836225 FFFE0001 FFFE 0001 1 IMUL BX 1 00000001 0000 0001 0 Instruction Decimal Product Hex Product AH AL CF/OF MUL BL 32640 7F80 7F 80 1 IMUL BL 128 0080 00 80 1
  • 18.
    Division Instruction  Syntax: DIVdivisor (for unsigned multiplication) IDIV divisor (for signed multiplication)  The quotient and the remainder have the same size as the divisor.  All the flags are undefined  Divide Overflow: If the quotient is too long to fit in the destination, program terminates and the system displays the message“Divide Overflow” 5/2/2016 EEE 315: MICROPROCESSOR & INTERFACING Md. Ayaz Masud Lecturer,Dept of EEE, BUET
  • 19.
    Division Instruction: BYTE form Divisoris an 8 bit register or memory byte 16 bit dividend is in AX 8 bit quotient is in AL 8 bit remainder is in AH 5/2/2016 EEE 315: MICROPROCESSOR & INTERFACING Md. Ayaz Masud Lecturer,Dept of EEE, BUET
  • 20.
    Division Instruction: WORD form Divisoris a 16 bit register or memory word 32 bit dividend is in DX:AX 16 bit quotient is in AX 16 bit remainder is in DX 5/2/2016 EEE 315: MICROPROCESSOR & INTERFACING Md. Ayaz Masud Lecturer,Dept of EEE, BUET
  • 21.
    Sign Extension ofthe Dividend  Word division: If the dividend fits in AX only – For DIV, DX should be cleared – For IDIV, DX should be made sign extension of AX. The instruction CWD (Convert Word to Doubleword)will do the extension  Word division: If the dividend fits in AL only – For DIV, AH should be cleared – For IDIV, AH should be made sign extension of AL.The instruction CBW (Convert Byte to Word) will do the extension  CBW and CWD has no operands and has no effect on flags 5/2/2016 EEE 315: MICROPROCESSOR & INTERFACING Md. Ayaz Masud Lecturer,Dept of EEE, BUET