Assembly language-lab9

A

Assembly Language Lab # 9 Shift,Rotate,Multiplication and Division Instruction

Faculty of Engineering
Computer Engineering Department
Islamic University of Gaza
Eng. Alaa.I.Haniya
Assembly Language Lab # 9
Shift,Rotate,Multiplication and Division
Instruction
1
AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction
Assembly Language Fundamentals
Objective:
To know more about Assembly language, such Shift, Rotate, Multiplication and Division
Instructions.
Shift and Rotate Instructions
 Shifting means to move bits right and left inside an operand.
 The following table provides Shift and Rotate Instructions.
 All affecting the Overflow and Carry flags.
 Logical Shifts and Arithmetic Shifts
- A logical shift fills the newly created bit position with zero.
An arithmetic shift fills the newly created bit position with a copy of the number’s sign bit.
2
AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction|2/20/2013
Example of Right Logical Shifts and Right Arithmetic Shifts
SHL Instruction
-The SHL (shift left) instruction performs a logical left shift on the destination operand, filling
the lowest bit with 0.
The first operand in SHL is the destination and the second is the shift count:
SHL destination,count
 Operand types for SHL:
SHL reg,imm8
SHL mem,imm8
SHL reg,CL
SHL mem,CL
 Formats shown here also apply to the SHR, SAL, SAR, ROR, ROL, RCR, and RCL instructions.
Application: Fast Multiplication
 Shifting left 1 bit multiplies a number by 2
 Shifting the integer 5 left by 1 bit yields the product of 5 * 21=10
mov dl,5
shl dl,1
3
AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction
Shifting left n bits multiplies the operand by2 𝑛
For example, 5 * 22 = 20
mov dl,5
shl dl,2 ;DL = 20, CF = 0
SHR Instruction
 The SHR (shift right) instruction performs a logical right shift on the destination operand.
The highest bit position is filled with a zero.
Application: Division
 Shifting right n bits divides the operand by 2 𝑛
mov dl,80 ; DL = 01010000b
shr dl,1 ; DL = 00101000b = 40, CF = 0
shr dl,2 ; DL = 00001010b = 10, CF = 0
 SAL and SAR Instructions
 SAL (shift arithmetic left) is identical to SHL.
 SAR (shift arithmetic right) performs a right arithmetic shift on the destination operand.
4
AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction|2/20/2013
 Applications:
1. Signed Division
An arithmetic shift preserves the number's sign.
mov dl,-80 ; DL = 10110000b
sar dl,1 ; DL = 11011000b = -40, CF = 0
sar dl,2 ; DL = 11110110b = -10, CF = 0
2. Sign-Extend
Suppose AX contains a signed integer and you want to extend its sign into EAX. First shift EAX
16 bits to the left, then shift it arithmetically 16 bits to the right:
mov ax,-128 ; EAX = ????FF80h
shl eax,16 ; EAX = FF800000h
sar eax,16 ; EAX = FFFFFF80h
 ROL Instruction
 The ROL (rotate left) instruction shifts each bit to the left. The highest bit is copied into
the Carry flag and the lowest bit position.
 No bits are lost.
Example:
mov al,11110000b
rol al,1 ;AL = 11100001b, CF = 1
Application: Exchanging Groups of Bits
You can use ROL to exchange the upper (bits 4–7) and lower (bits0–3) halves of a byte.
mov dl,3Fh ; DL = 00111111b
rol dl,4 ; DL = 11110011b = F3h, CF = 1
 ROR Instruction
 The ROR (rotate right) instruction shifts each bit to the right and copies the lowest bit
into the Carry flag and the highest bit position.
 No bits are lost.
5
AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction
 Example:
mov al,11110000b
ror al,1 ; AL = 01111000b, CF = 0
 Application: Exchanging Groups of Bits
 You can use ROL to exchange the upper (bits 4–7) and lower (bits0–3) halves of a byte.
mov dl,3Fh ; DL = 00111111b
ror dl,4 ; DL = 11110011b = F3h, CF = 1
RCL Instruction
 The RCL (rotate carry left) instruction shifts each bit to the left, copies the Carry flag to the
LSB, and copies the MSB into the Carry flag.
Example:
Clc ; clear carry, CF = 0
mov bl,88h ;CF = 0 ,BL = 10001000b
rcl bl,1 ;CF = 1 ,BL = 00010000b
rcl bl,1 ;CF = 0 ,BL = 00100001b
RCR Instruction
The RCR (rotate carry right) instruction shifts each bit to the right, copies the Carry flag into
the MSB, and copies the LSB into the Carry flag.
Example:
stc ; set carry, CF = 1
mov ah,10h ;CF = 1, AH = 00010000b
rcr ah,1 ; CF = 0, AH = 10001000b 5
6
AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction|2/20/2013
SHLD Instruction
 The SHLD (shift left double) instruction shifts a destination operand a given number of
bits to the left.
 The bit positions opened up by the shift are filled by the most significant bits of the
source operand.
 Only the destination is modified, not the source.
Syntax:
SHLD dest, source, count
Operand types:
SHLD reg16,reg16,CL/imm8
SHLD mem16,reg16,CL/imm8
SHLD reg32,reg32,CL/imm8
SHLD mem32,reg32,CL/imm8

Example:
.data
wval WORD 9BA6h 
.code
mov ax,0AC36h 
shld wval,ax,4 
SHRD Instruction
 The SHRD (shift right double) instruction shifts a destination operand a given number of
bits to the right.
 The bit positions opened up by the shift are filled by the least significant bits of the
source operand.
7
AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction
Example:
mov ax,234Bh
mov dx,7654h
shrd ax,dx,4
Multiplication and Division Instructions
MUL Instruction
 The MUL (unsigned multiply) instruction comes in three versions:
 The first version multiplies an 8-bit operand by the AL register.
 The second version multiplies a 16-bit operand by the AX register.
 The third version multiplies a 32-bit operand by the EAX register.
 The multiplier and multiplicand must always be the same size, and the product is twice
their size.
 The three formats accept register and memory operands, but not immediate operands:
MUL reg/mem8
MUL reg/mem16
MUL reg/mem32
 MUL sets the Carry and Overflow flags if the upper half of the product is not equal to
zero.
8
AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction|2/20/2013
 Example1: Multiply 16-bit var1 (2000h) * var2 (100h)
.data
var1 WORD 2000h
var2 WORD 100h
.code
mov ax,var1
mul var2 ; DX:AX = 00200000h, CF = OF = 1

 Example2: Multiply EAX (12345h) * EBX (1000h)
mov eax,12345h
mov ebx,1000h
mul ebx ; EDX:EAX = 0000000012345000h, CF=OF=0
 DIV Instruction
 The DIV (unsigned divide) instruction performs 8-bit, 16-bit, and 32-bit unsigned
integer division.
 The single register or memory operand is the divisor.
 The formats are
o DIV reg/mem8
o DIV reg/mem16
o DIV reg/mem32
 Example1: Divide AX = 8003h by CX = 100h, using 16-bit operands
mov dx,0 ;clear dividend, high
mov ax,8003h ;dividend, low
mov cx,100h ;divisor
div cx ; AX = 0080h, DX = 0003h (Remainder)

 Example2: Same division, using 32-bit operands
mov edx,0 ;clear dividend, high
mov eax,8003h ;dividend, low
mov ecx,100h ;divisor
div ecx ; EAX = 00000080h, EDX = 00000003h
Lab work:
9
AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction
Excercise1:
The greatest common divisor of two integers is the largest integer that will evenly divide
(without a remainder) both integers . The GCD algorithm involves integer division in a loop,
described by the following c++ code:
int GCD(int x , int y)
{
x = abs (x);
y = abs (y);
do {
int n = x % y;
x = y;
y = n;
} while (y > 0) ;
return x ;
}
Write an assembly program that determines the gcd (greatest common divisor) of two positive
integer numbers for example (8,12).
This solution is just for positive integers and don’t follow the previous algorithm. In your
homework you should implement the algorithm exactly.
10
AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction|2/20/2013
11
AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction
Excercise2:
Write an assembly code to convert the number 12438765h to 87654321h.
Excercise3:
Write an assembly code to evaluate the following expression:
(val1*(val3/val4)) + (val2*(val3%val4))
Use these values:
val1 dw 4
val2 dw 5
val3 dw 4
val4 dw 3
result dd ?
12
AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction|2/20/2013
13
AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction
Homework:
1. Write an assembly code to convert a binary string into hexadecimal value. If the binary
String is greater than 32 digits length a value zero must be returned. Use this value
declaration:
B_Val db '10001111' , '$'
2. Write an assembly code to find the power of any integer to any integer using mul
instruction. Use these values for testing:
base db 3h
power db 5h
result dd ? ;3^5 =243 = F3H
3. Implement the following GCD algorithm:
int GCD(int x , int y)
{
x = abs (x);
y = abs (y);
do {
int n = x % y;
x = y;
y = n;
} while (y > 0) ;
return x ;
}
Find: GCD(-8,12)

Recomendados

Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,... por
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 7 (Logic, Shift,...Bilal Amjad
3K visualizações48 slides
Arithmetic & logical operations in 8051 por
Arithmetic & logical operations in 8051Arithmetic & logical operations in 8051
Arithmetic & logical operations in 8051Jay Patel
39.8K visualizações27 slides
DAA AND DAS por
DAA AND DASDAA AND DAS
DAA AND DASBasavaraj Shetty
6.1K visualizações11 slides
arithmetic ins in 8051 por
arithmetic ins in 8051arithmetic ins in 8051
arithmetic ins in 8051VJ Aiswaryadevi
6.4K visualizações9 slides
Chap 8 The stack and introduction to procedures & Chapter 9 multiplication an... por
Chap 8 The stack and introduction to procedures & Chapter 9 multiplication an...Chap 8 The stack and introduction to procedures & Chapter 9 multiplication an...
Chap 8 The stack and introduction to procedures & Chapter 9 multiplication an...warda aziz
7.7K visualizações13 slides
Ascii adjust & decimal adjust por
Ascii adjust & decimal adjustAscii adjust & decimal adjust
Ascii adjust & decimal adjustTech_MX
22.6K visualizações30 slides

Mais conteúdo relacionado

Mais procurados

8086 instruction set por
8086  instruction set8086  instruction set
8086 instruction setmengistu ketema
2.5K visualizações52 slides
Binary and hex input/output (in 8086 assembuly langyage) por
Binary and hex input/output (in 8086 assembuly langyage)Binary and hex input/output (in 8086 assembuly langyage)
Binary and hex input/output (in 8086 assembuly langyage)Bilal Amjad
7.6K visualizações20 slides
8085 data transfer instruction set por
8085 data transfer instruction set8085 data transfer instruction set
8085 data transfer instruction setprashant1271
821 visualizações5 slides
IMPLEMENTING ARITHMETIC INSTRUCTIONS IN EMU 8086 por
IMPLEMENTING ARITHMETIC INSTRUCTIONS IN EMU 8086IMPLEMENTING ARITHMETIC INSTRUCTIONS IN EMU 8086
IMPLEMENTING ARITHMETIC INSTRUCTIONS IN EMU 8086COMSATS Abbottabad
5.6K visualizações13 slides
assembly language programming organization of IBM PC chapter 9 part-2(decimal... por
assembly language programming organization of IBM PC chapter 9 part-2(decimal...assembly language programming organization of IBM PC chapter 9 part-2(decimal...
assembly language programming organization of IBM PC chapter 9 part-2(decimal...Bilal Amjad
860 visualizações34 slides
8085 stack & machine control instruction por
8085 stack & machine control instruction8085 stack & machine control instruction
8085 stack & machine control instructionprashant1271
409 visualizações6 slides

Mais procurados(20)

8086 instruction set por mengistu ketema
8086  instruction set8086  instruction set
8086 instruction set
mengistu ketema2.5K visualizações
Binary and hex input/output (in 8086 assembuly langyage) por Bilal Amjad
Binary and hex input/output (in 8086 assembuly langyage)Binary and hex input/output (in 8086 assembuly langyage)
Binary and hex input/output (in 8086 assembuly langyage)
Bilal Amjad7.6K visualizações
8085 data transfer instruction set por prashant1271
8085 data transfer instruction set8085 data transfer instruction set
8085 data transfer instruction set
prashant1271821 visualizações
IMPLEMENTING ARITHMETIC INSTRUCTIONS IN EMU 8086 por COMSATS Abbottabad
IMPLEMENTING ARITHMETIC INSTRUCTIONS IN EMU 8086IMPLEMENTING ARITHMETIC INSTRUCTIONS IN EMU 8086
IMPLEMENTING ARITHMETIC INSTRUCTIONS IN EMU 8086
COMSATS Abbottabad5.6K visualizações
assembly language programming organization of IBM PC chapter 9 part-2(decimal... por Bilal Amjad
assembly language programming organization of IBM PC chapter 9 part-2(decimal...assembly language programming organization of IBM PC chapter 9 part-2(decimal...
assembly language programming organization of IBM PC chapter 9 part-2(decimal...
Bilal Amjad860 visualizações
8085 stack & machine control instruction por prashant1271
8085 stack & machine control instruction8085 stack & machine control instruction
8085 stack & machine control instruction
prashant1271409 visualizações
microprocessor ppt (branching and logical instructions) por Nemish Bhojani
microprocessor ppt (branching and logical instructions)microprocessor ppt (branching and logical instructions)
microprocessor ppt (branching and logical instructions)
Nemish Bhojani1.1K visualizações
Shift rotate por fika sweety
Shift rotateShift rotate
Shift rotate
fika sweety7.1K visualizações
Multiplication & division instructions microprocessor 8086 por University of Gujrat, Pakistan
Multiplication & division instructions microprocessor 8086Multiplication & division instructions microprocessor 8086
Multiplication & division instructions microprocessor 8086
University of Gujrat, Pakistan23.4K visualizações
Assignment on alp por Jahurul Islam
Assignment on alpAssignment on alp
Assignment on alp
Jahurul Islam2.8K visualizações
Chapter 5The proessor status and the FLAGS registers por warda aziz
Chapter 5The proessor status and the FLAGS registersChapter 5The proessor status and the FLAGS registers
Chapter 5The proessor status and the FLAGS registers
warda aziz15.6K visualizações
Instruction Set 8085 por Stupidsid.com
Instruction Set 8085Instruction Set 8085
Instruction Set 8085
Stupidsid.com3K visualizações
8085 branching instruction por prashant1271
8085 branching instruction8085 branching instruction
8085 branching instruction
prashant12711.9K visualizações
8085 arithmetic instructions por prashant1271
8085 arithmetic instructions8085 arithmetic instructions
8085 arithmetic instructions
prashant1271527 visualizações
Computer design por Pradeep Kumar TS
Computer designComputer design
Computer design
Pradeep Kumar TS610 visualizações
8086 instructions por Ravi Anand
8086 instructions8086 instructions
8086 instructions
Ravi Anand63.3K visualizações
A109211002 switchingtheoryandlogicdesign1 por jntuworld
A109211002 switchingtheoryandlogicdesign1A109211002 switchingtheoryandlogicdesign1
A109211002 switchingtheoryandlogicdesign1
jntuworld196 visualizações
8086 instruction set por jemimajerome
8086 instruction set8086 instruction set
8086 instruction set
jemimajerome11K visualizações
8085:branching and logical instruction por Nemish Bhojani
8085:branching and logical instruction8085:branching and logical instruction
8085:branching and logical instruction
Nemish Bhojani2.3K visualizações

Similar a Assembly language-lab9

[ASM]Lab7 por
[ASM]Lab7[ASM]Lab7
[ASM]Lab7Nora Youssef
638 visualizações56 slides
[ASM]Lab4 por
[ASM]Lab4[ASM]Lab4
[ASM]Lab4Nora Youssef
1.2K visualizações99 slides
Al2ed chapter7 por
Al2ed chapter7Al2ed chapter7
Al2ed chapter7Abdullelah Al-Fahad
733 visualizações43 slides
Admission in india 2015 por
Admission in india 2015Admission in india 2015
Admission in india 2015Edhole.com
140 visualizações23 slides
Hemanth143 por
Hemanth143 Hemanth143
Hemanth143 vijaydeepakg
104 visualizações37 slides
8086-instruction-set-ppt por
 8086-instruction-set-ppt 8086-instruction-set-ppt
8086-instruction-set-pptjemimajerome
28K visualizações91 slides

Similar a Assembly language-lab9(20)

[ASM]Lab7 por Nora Youssef
[ASM]Lab7[ASM]Lab7
[ASM]Lab7
Nora Youssef638 visualizações
[ASM]Lab4 por Nora Youssef
[ASM]Lab4[ASM]Lab4
[ASM]Lab4
Nora Youssef1.2K visualizações
Admission in india 2015 por Edhole.com
Admission in india 2015Admission in india 2015
Admission in india 2015
Edhole.com140 visualizações
Hemanth143 por vijaydeepakg
Hemanth143 Hemanth143
Hemanth143
vijaydeepakg104 visualizações
8086-instruction-set-ppt por jemimajerome
 8086-instruction-set-ppt 8086-instruction-set-ppt
8086-instruction-set-ppt
jemimajerome28K visualizações
Admission in india 2015 por Edhole.com
Admission in india 2015Admission in india 2015
Admission in india 2015
Edhole.com248 visualizações
Data manipulation instructions por Mahesh Kumar Attri
Data manipulation instructionsData manipulation instructions
Data manipulation instructions
Mahesh Kumar Attri645 visualizações
Data manipulation instructions por Mahesh Kumar Attri
Data manipulation instructionsData manipulation instructions
Data manipulation instructions
Mahesh Kumar Attri21.1K visualizações
15CS44 MP & MC Module 2 por RLJIT
15CS44 MP & MC Module  215CS44 MP & MC Module  2
15CS44 MP & MC Module 2
RLJIT787 visualizações
Assembly language programs por HarshitParkar6677
Assembly language programsAssembly language programs
Assembly language programs
HarshitParkar667754 visualizações
Uc 2(vii) por Ankita Jaiswal
Uc 2(vii)Uc 2(vii)
Uc 2(vii)
Ankita Jaiswal854 visualizações
microp-8085 74 instructions for mct-A :P por Jathin Kanumuri
microp-8085 74 instructions for mct-A :Pmicrop-8085 74 instructions for mct-A :P
microp-8085 74 instructions for mct-A :P
Jathin Kanumuri5.7K visualizações
8085 Paper Presentation slides,ppt,microprocessor 8085 ,guide, instruction set por Saumitra Rukmangad
8085 Paper Presentation slides,ppt,microprocessor 8085 ,guide, instruction set8085 Paper Presentation slides,ppt,microprocessor 8085 ,guide, instruction set
8085 Paper Presentation slides,ppt,microprocessor 8085 ,guide, instruction set
Saumitra Rukmangad136.8K visualizações
microp-8085 74 instructions for mct-A :P-2 por Jathin Kanumuri
microp-8085 74 instructions for mct-A :P-2microp-8085 74 instructions for mct-A :P-2
microp-8085 74 instructions for mct-A :P-2
Jathin Kanumuri3.6K visualizações
15CS44 MP &MC Module 3 por RLJIT
15CS44 MP &MC Module 315CS44 MP &MC Module 3
15CS44 MP &MC Module 3
RLJIT772 visualizações
12 chapter06 math_instructions_fa14 por John Todora
12 chapter06 math_instructions_fa1412 chapter06 math_instructions_fa14
12 chapter06 math_instructions_fa14
John Todora1.7K visualizações
Seminar on Digital Multiplier(Booth Multiplier) Using VHDL por Naseer LoneRider
Seminar on Digital Multiplier(Booth Multiplier) Using VHDLSeminar on Digital Multiplier(Booth Multiplier) Using VHDL
Seminar on Digital Multiplier(Booth Multiplier) Using VHDL
Naseer LoneRider12.6K visualizações

Último

What is Unit Testing por
What is Unit TestingWhat is Unit Testing
What is Unit TestingSadaaki Emura
24 visualizações25 slides
SPICE PARK DEC2023 (6,625 SPICE Models) por
SPICE PARK DEC2023 (6,625 SPICE Models) SPICE PARK DEC2023 (6,625 SPICE Models)
SPICE PARK DEC2023 (6,625 SPICE Models) Tsuyoshi Horigome
28 visualizações218 slides
SUMIT SQL PROJECT SUPERSTORE 1.pptx por
SUMIT SQL PROJECT SUPERSTORE 1.pptxSUMIT SQL PROJECT SUPERSTORE 1.pptx
SUMIT SQL PROJECT SUPERSTORE 1.pptxSumit Jadhav
15 visualizações26 slides
GDSC Mikroskil Members Onboarding 2023.pdf por
GDSC Mikroskil Members Onboarding 2023.pdfGDSC Mikroskil Members Onboarding 2023.pdf
GDSC Mikroskil Members Onboarding 2023.pdfgdscmikroskil
53 visualizações62 slides
Proposal Presentation.pptx por
Proposal Presentation.pptxProposal Presentation.pptx
Proposal Presentation.pptxkeytonallamon
42 visualizações36 slides
Update 42 models(Diode/General ) in SPICE PARK(DEC2023) por
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)Update 42 models(Diode/General ) in SPICE PARK(DEC2023)
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)Tsuyoshi Horigome
33 visualizações16 slides

Último(20)

What is Unit Testing por Sadaaki Emura
What is Unit TestingWhat is Unit Testing
What is Unit Testing
Sadaaki Emura24 visualizações
SPICE PARK DEC2023 (6,625 SPICE Models) por Tsuyoshi Horigome
SPICE PARK DEC2023 (6,625 SPICE Models) SPICE PARK DEC2023 (6,625 SPICE Models)
SPICE PARK DEC2023 (6,625 SPICE Models)
Tsuyoshi Horigome28 visualizações
SUMIT SQL PROJECT SUPERSTORE 1.pptx por Sumit Jadhav
SUMIT SQL PROJECT SUPERSTORE 1.pptxSUMIT SQL PROJECT SUPERSTORE 1.pptx
SUMIT SQL PROJECT SUPERSTORE 1.pptx
Sumit Jadhav 15 visualizações
GDSC Mikroskil Members Onboarding 2023.pdf por gdscmikroskil
GDSC Mikroskil Members Onboarding 2023.pdfGDSC Mikroskil Members Onboarding 2023.pdf
GDSC Mikroskil Members Onboarding 2023.pdf
gdscmikroskil53 visualizações
Proposal Presentation.pptx por keytonallamon
Proposal Presentation.pptxProposal Presentation.pptx
Proposal Presentation.pptx
keytonallamon42 visualizações
Update 42 models(Diode/General ) in SPICE PARK(DEC2023) por Tsuyoshi Horigome
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)Update 42 models(Diode/General ) in SPICE PARK(DEC2023)
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)
Tsuyoshi Horigome33 visualizações
_MAKRIADI-FOTEINI_diploma thesis.pptx por fotinimakriadi
_MAKRIADI-FOTEINI_diploma thesis.pptx_MAKRIADI-FOTEINI_diploma thesis.pptx
_MAKRIADI-FOTEINI_diploma thesis.pptx
fotinimakriadi8 visualizações
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc... por csegroupvn
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...
csegroupvn5 visualizações
MSA Website Slideshow (16).pdf por msaucla
MSA Website Slideshow (16).pdfMSA Website Slideshow (16).pdf
MSA Website Slideshow (16).pdf
msaucla76 visualizações
Design of machine elements-UNIT 3.pptx por gopinathcreddy
Design of machine elements-UNIT 3.pptxDesign of machine elements-UNIT 3.pptx
Design of machine elements-UNIT 3.pptx
gopinathcreddy32 visualizações
Design_Discover_Develop_Campaign.pptx por ShivanshSeth6
Design_Discover_Develop_Campaign.pptxDesign_Discover_Develop_Campaign.pptx
Design_Discover_Develop_Campaign.pptx
ShivanshSeth632 visualizações
Searching in Data Structure por raghavbirla63
Searching in Data StructureSearching in Data Structure
Searching in Data Structure
raghavbirla637 visualizações
K8S Roadmap.pdf por MaryamTavakkoli2
K8S Roadmap.pdfK8S Roadmap.pdf
K8S Roadmap.pdf
MaryamTavakkoli28 visualizações
Investigation of Physicochemical Changes of Soft Clay around Deep Geopolymer ... por AltinKaradagli
Investigation of Physicochemical Changes of Soft Clay around Deep Geopolymer ...Investigation of Physicochemical Changes of Soft Clay around Deep Geopolymer ...
Investigation of Physicochemical Changes of Soft Clay around Deep Geopolymer ...
AltinKaradagli12 visualizações
Control Systems Feedback.pdf por LGGaming5
Control Systems Feedback.pdfControl Systems Feedback.pdf
Control Systems Feedback.pdf
LGGaming56 visualizações
CHEMICAL KINETICS.pdf por AguedaGutirrez
CHEMICAL KINETICS.pdfCHEMICAL KINETICS.pdf
CHEMICAL KINETICS.pdf
AguedaGutirrez13 visualizações
Generative AI Models & Their Applications por SN
Generative AI Models & Their ApplicationsGenerative AI Models & Their Applications
Generative AI Models & Their Applications
SN8 visualizações
Introduction to CAD-CAM.pptx por suyogpatil49
Introduction to CAD-CAM.pptxIntroduction to CAD-CAM.pptx
Introduction to CAD-CAM.pptx
suyogpatil495 visualizações
Codes and Conventions.pptx por IsabellaGraceAnkers
Codes and Conventions.pptxCodes and Conventions.pptx
Codes and Conventions.pptx
IsabellaGraceAnkers9 visualizações

Assembly language-lab9

  • 1. Faculty of Engineering Computer Engineering Department Islamic University of Gaza Eng. Alaa.I.Haniya Assembly Language Lab # 9 Shift,Rotate,Multiplication and Division Instruction
  • 2. 1 AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction Assembly Language Fundamentals Objective: To know more about Assembly language, such Shift, Rotate, Multiplication and Division Instructions. Shift and Rotate Instructions  Shifting means to move bits right and left inside an operand.  The following table provides Shift and Rotate Instructions.  All affecting the Overflow and Carry flags.  Logical Shifts and Arithmetic Shifts - A logical shift fills the newly created bit position with zero. An arithmetic shift fills the newly created bit position with a copy of the number’s sign bit.
  • 3. 2 AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction|2/20/2013 Example of Right Logical Shifts and Right Arithmetic Shifts SHL Instruction -The SHL (shift left) instruction performs a logical left shift on the destination operand, filling the lowest bit with 0. The first operand in SHL is the destination and the second is the shift count: SHL destination,count  Operand types for SHL: SHL reg,imm8 SHL mem,imm8 SHL reg,CL SHL mem,CL  Formats shown here also apply to the SHR, SAL, SAR, ROR, ROL, RCR, and RCL instructions. Application: Fast Multiplication  Shifting left 1 bit multiplies a number by 2  Shifting the integer 5 left by 1 bit yields the product of 5 * 21=10 mov dl,5 shl dl,1
  • 4. 3 AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction Shifting left n bits multiplies the operand by2 𝑛 For example, 5 * 22 = 20 mov dl,5 shl dl,2 ;DL = 20, CF = 0 SHR Instruction  The SHR (shift right) instruction performs a logical right shift on the destination operand. The highest bit position is filled with a zero. Application: Division  Shifting right n bits divides the operand by 2 𝑛 mov dl,80 ; DL = 01010000b shr dl,1 ; DL = 00101000b = 40, CF = 0 shr dl,2 ; DL = 00001010b = 10, CF = 0  SAL and SAR Instructions  SAL (shift arithmetic left) is identical to SHL.  SAR (shift arithmetic right) performs a right arithmetic shift on the destination operand.
  • 5. 4 AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction|2/20/2013  Applications: 1. Signed Division An arithmetic shift preserves the number's sign. mov dl,-80 ; DL = 10110000b sar dl,1 ; DL = 11011000b = -40, CF = 0 sar dl,2 ; DL = 11110110b = -10, CF = 0 2. Sign-Extend Suppose AX contains a signed integer and you want to extend its sign into EAX. First shift EAX 16 bits to the left, then shift it arithmetically 16 bits to the right: mov ax,-128 ; EAX = ????FF80h shl eax,16 ; EAX = FF800000h sar eax,16 ; EAX = FFFFFF80h  ROL Instruction  The ROL (rotate left) instruction shifts each bit to the left. The highest bit is copied into the Carry flag and the lowest bit position.  No bits are lost. Example: mov al,11110000b rol al,1 ;AL = 11100001b, CF = 1 Application: Exchanging Groups of Bits You can use ROL to exchange the upper (bits 4–7) and lower (bits0–3) halves of a byte. mov dl,3Fh ; DL = 00111111b rol dl,4 ; DL = 11110011b = F3h, CF = 1  ROR Instruction  The ROR (rotate right) instruction shifts each bit to the right and copies the lowest bit into the Carry flag and the highest bit position.  No bits are lost.
  • 6. 5 AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction  Example: mov al,11110000b ror al,1 ; AL = 01111000b, CF = 0  Application: Exchanging Groups of Bits  You can use ROL to exchange the upper (bits 4–7) and lower (bits0–3) halves of a byte. mov dl,3Fh ; DL = 00111111b ror dl,4 ; DL = 11110011b = F3h, CF = 1 RCL Instruction  The RCL (rotate carry left) instruction shifts each bit to the left, copies the Carry flag to the LSB, and copies the MSB into the Carry flag. Example: Clc ; clear carry, CF = 0 mov bl,88h ;CF = 0 ,BL = 10001000b rcl bl,1 ;CF = 1 ,BL = 00010000b rcl bl,1 ;CF = 0 ,BL = 00100001b RCR Instruction The RCR (rotate carry right) instruction shifts each bit to the right, copies the Carry flag into the MSB, and copies the LSB into the Carry flag. Example: stc ; set carry, CF = 1 mov ah,10h ;CF = 1, AH = 00010000b rcr ah,1 ; CF = 0, AH = 10001000b 5
  • 7. 6 AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction|2/20/2013 SHLD Instruction  The SHLD (shift left double) instruction shifts a destination operand a given number of bits to the left.  The bit positions opened up by the shift are filled by the most significant bits of the source operand.  Only the destination is modified, not the source. Syntax: SHLD dest, source, count Operand types: SHLD reg16,reg16,CL/imm8 SHLD mem16,reg16,CL/imm8 SHLD reg32,reg32,CL/imm8 SHLD mem32,reg32,CL/imm8  Example: .data wval WORD 9BA6h  .code mov ax,0AC36h  shld wval,ax,4  SHRD Instruction  The SHRD (shift right double) instruction shifts a destination operand a given number of bits to the right.  The bit positions opened up by the shift are filled by the least significant bits of the source operand.
  • 8. 7 AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction Example: mov ax,234Bh mov dx,7654h shrd ax,dx,4 Multiplication and Division Instructions MUL Instruction  The MUL (unsigned multiply) instruction comes in three versions:  The first version multiplies an 8-bit operand by the AL register.  The second version multiplies a 16-bit operand by the AX register.  The third version multiplies a 32-bit operand by the EAX register.  The multiplier and multiplicand must always be the same size, and the product is twice their size.  The three formats accept register and memory operands, but not immediate operands: MUL reg/mem8 MUL reg/mem16 MUL reg/mem32  MUL sets the Carry and Overflow flags if the upper half of the product is not equal to zero.
  • 9. 8 AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction|2/20/2013  Example1: Multiply 16-bit var1 (2000h) * var2 (100h) .data var1 WORD 2000h var2 WORD 100h .code mov ax,var1 mul var2 ; DX:AX = 00200000h, CF = OF = 1   Example2: Multiply EAX (12345h) * EBX (1000h) mov eax,12345h mov ebx,1000h mul ebx ; EDX:EAX = 0000000012345000h, CF=OF=0  DIV Instruction  The DIV (unsigned divide) instruction performs 8-bit, 16-bit, and 32-bit unsigned integer division.  The single register or memory operand is the divisor.  The formats are o DIV reg/mem8 o DIV reg/mem16 o DIV reg/mem32  Example1: Divide AX = 8003h by CX = 100h, using 16-bit operands mov dx,0 ;clear dividend, high mov ax,8003h ;dividend, low mov cx,100h ;divisor div cx ; AX = 0080h, DX = 0003h (Remainder)   Example2: Same division, using 32-bit operands mov edx,0 ;clear dividend, high mov eax,8003h ;dividend, low mov ecx,100h ;divisor div ecx ; EAX = 00000080h, EDX = 00000003h Lab work:
  • 10. 9 AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction Excercise1: The greatest common divisor of two integers is the largest integer that will evenly divide (without a remainder) both integers . The GCD algorithm involves integer division in a loop, described by the following c++ code: int GCD(int x , int y) { x = abs (x); y = abs (y); do { int n = x % y; x = y; y = n; } while (y > 0) ; return x ; } Write an assembly program that determines the gcd (greatest common divisor) of two positive integer numbers for example (8,12). This solution is just for positive integers and don’t follow the previous algorithm. In your homework you should implement the algorithm exactly.
  • 12. 11 AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction Excercise2: Write an assembly code to convert the number 12438765h to 87654321h. Excercise3: Write an assembly code to evaluate the following expression: (val1*(val3/val4)) + (val2*(val3%val4)) Use these values: val1 dw 4 val2 dw 5 val3 dw 4 val4 dw 3 result dd ?
  • 14. 13 AssemblyLanguageLab#9Shift,Rotate,MultiplicationandDivisionInstruction Homework: 1. Write an assembly code to convert a binary string into hexadecimal value. If the binary String is greater than 32 digits length a value zero must be returned. Use this value declaration: B_Val db '10001111' , '$' 2. Write an assembly code to find the power of any integer to any integer using mul instruction. Use these values for testing: base db 3h power db 5h result dd ? ;3^5 =243 = F3H 3. Implement the following GCD algorithm: int GCD(int x , int y) { x = abs (x); y = abs (y); do { int n = x % y; x = y; y = n; } while (y > 0) ; return x ; } Find: GCD(-8,12)