SlideShare a Scribd company logo
1 of 21
Download to read offline
Lecture 05
Programming The 8086 In
AAiT
Programming The 8086 In
Assembly Language
…Continued
Daniel D.Daniel D.Daniel D.Daniel D. DECEECEgECEgECEgECEg –––– 4501 Microcomputers & Interfacing4501 Microcomputers & Interfacing4501 Microcomputers & Interfacing4501 Microcomputers & Interfacing
Basic Instructions
In This Lecture AAiT
Details of the basic instructions
1ECEgECEgECEgECEg ---- 4501450145014501
Basic Instructions
1. Data transfer instructions
MOV, LEA, IN/OUT, PUSH/POP, PUSHF/POPF
2. Arithmetic instructions
ADD, INC, SUB, DEC, NEG, COMP, MUL/IMUL, DIV/IDIV
3. Bit manipulation instructions
logical (AND, OR, TEST, XOR), SHR/SHL, RCL/RCR
AAiT
logical (AND, OR, TEST, XOR), SHR/SHL, RCL/RCR
4. String instructions
REP/REPE/REPNE, LOADS, STOS
5. Program execution transfer instructions
JMP/JE/JG/JL/JGE/JLE/JC, CALL/RET, LOOP, INT/IRET
6. Processor control instructions
flags(set/clear), HLT, WAIT, NOP
2ECEgECEgECEgECEg ---- 4501450145014501
1. Data Transfer Instructions
MOV destination, source
Destination: can be a register or a memory location.
Source: can be a register, a memory location, or an
immediate number.
The source and destination must both be of type byte, or they
AAiT
The source and destination must both be of type byte, or they
must both be of type word.
e.g.
MOV CX, 037AH Put the immediate number 037AH in CX
MOV BL, [437AH] Copy byte in DS at offset 437AH to BL
MOV AX, BX Copy contents of register BX to AX
MOV DL, [BX] Copy byte from memory at [BX] to DL.
3ECEgECEgECEgECEg ---- 4501450145014501
PUSH source, POP destination
Destination: can be a register or a memory location.
Source: can be a register or a memory location
The stack segment register and the stack pointer must be initialized
before these instructions can be used.
Data Transfer Instructions…cntd AAiT
before these instructions can be used.
e.g.
PUSH BX Decrement SP by 2, copy BX to stack
PUSH DS Decrement SP by 2, copy DS to stack
POP DX Copy a word from top of stack to DX, Increment SP by 2
PUSH AL Illegal, must push a word
POP CS Just Illegal
PUSHF/POPF can be used with flags
4ECEgECEgECEgECEg ---- 4501450145014501
IN accumulator, port OUT port, accumulator
Port: 8 or 16-bit I/O port address. Can be fixed or variable
For the fixed-port type, the 8-bit address of a port is specified
directly in the instruction.
e.g.
Data Transfer Instructions…cntd AAiT
e.g.
IN AX,34H Input a word from port 34H to AX
OUT 3BH,AL Copy the contents of AL to port 3BH
For the variable port, the port address is loaded into the DX
register before the IN/OUT instruction.
e.g.
MOV DX, 0FF78H Initialize DX to point to port
IN AL, DX Input a byte from 8-bit port 0FF78H to AL
5ECEgECEgECEgECEg ---- 4501450145014501
LEA register, source (load effective address)
This instruction determines the offset of the variable or memory
location named as the source and puts this offset in the indicated
16-bit register.
Data Transfer Instructions…cntd AAiT
e.g.
LEA BX, PRICES Load BX with offset of PRICES in DS
LEA BP, SS: STACK_TOP Load BP with offset of STACK_TOP in SS
Assume PRICES is an array of bytes in data segment. The instruction
LEA BX, PRICES will load the displacement of the first price in the list
directly into BX.
6ECEgECEgECEgECEg ---- 4501450145014501
ADD destination, source /also ADC (add with carry)
e.g.
ADD AL, 74H Add immediate number 74H to contents of AL.
Result In AL.
ADD DX,[SI] Add word from memory at offset [SI] in DS to
2. Arithmetic Instructions AAiT
ADD DX,[SI] Add word from memory at offset [SI] in DS to
contents of DX. Result in DX.
NB: Arithmetic instructions affect the flags.
INC destination can be used to add 1 to a number
e.g
INC BX, INC CX, …
INC doesn’t affect the carry flag.
7ECEgECEgECEgECEg ---- 4501450145014501
SUB destination, source
e.g.
SUB CX, BX CX - BX. Result in CX
SUB AX,3427H Subtract immediate number 3427H from AX
The decrement instruction DEC destination can be used to
subtract 1 from a number
2. Arithmetic Instructions…cntd
AAiT
subtract 1 from a number
DEC CX subtract 1 from the content of the CX register
The NEG destination instruction can be used to form 2’s
complement of the number in destination
NEG AL Replace number in AL with its 2’s complement
The CMP source, dest is used to compare two numbers
CMP BH, CL compare a byte in BH with a byte in CL
8ECEgECEgECEgECEg ---- 4501450145014501
MUL source IMUL source (signed)
•Source: can be memory or registers.
•one of the operands is in AL if source contains a byte, else in AX if
source holds a word.
AL x source = AH: AL (AX)……………byte x byte = word
2. Arithmetic Instructions…cntd AAiT
AL x source = AH: AL (AX)……………byte x byte = word
AX x source = DX: AX ………………..word x word = double word
e.g.
MUL BH AL times BH, result in AX
MUL CX AX times CX, result high word in DX, low word in AX
•IMUL can be used the same way for signed numbers taking care of
the sign bit.
9ECEgECEgECEgECEg ---- 4501450145014501
DIV source IDIV source (signed)
• Source: contains the divisor. It can be in memory or registers.
• The dividend is in AX if word/byte or in DX:AX if double/word
• Word/byte: AX/source…quotient in AL, remainder in AH
• Double/word: DX:AX/source…quotient in AX, remainder in DX
e.g.
2. Arithmetic Instructions…cntd AAiT
e.g.
DIV BL Divide word in AX by byte in BL. Quotient in AL,
remainder in AH.
DIV CX Divide double word in DX and AX by word in CX.
Quotient in AX, remainder in DX.
•IMUL can be used the same way for signed numbers taking care of
the sign bit.
•CPU generates interrupts on divide by zero and quotient overflow
10ECEgECEgECEgECEg ---- 4501450145014501
3. Bit-oriented Instructions
Logical (AND/TEST, OR, XOR, NOT)
AND/OR/XOR dest, source NOT dest
Do the indicated logic operation on the given source and
destination operands and keep the result in destination
e.g.
AAiT
e.g.
AND BH,CL AND byte in BH with byte in CL Result in BH
TEST AL, BH AND BH with AL, no result stored. Update PF, ZF
OR BL,80H BL ORed with immediate 80H. Set MSB of BL to a 1
XOR CL,BH Byte in BH Exclusive-ORed with byte in CL. Result in
CL. BH not chanced.
NOT BX Complement contents of BX register
Logical operators affect the zero flag and the parity flag
11ECEgECEgECEgECEg ---- 4501450145014501
Bit-oriented Instructions…cntd
SHL dest, count SHR dest, count
Shift the binary number in dest. count times
CF MSB LSB 0 SHL ( *2)
0 MSB LSB CF SHR (/2)
e.g.
AAiT
e.g.
SHL BX, 1 Shift word in BX 1 bit position left, 0 in LSB
MOV CL, 02H Load desired number of shifts in CL
SHL BP, CL Shift word in BP left (CL) bit positions, 0's in 2 LSBs
SHR AL, 4 Shift AL four bit positions right and put 0's in
upper 4 bits.
• shift operators affect CF, ZF, OF, SF, and PF
12ECEgECEgECEgECEg ---- 4501450145014501
4. String Instructions
MOVS {dest string, source string} LODS source / STOS dest
MOVS: copies string in data segment to a string in extra segment
LODS: copies a string in memory pointed by SI in to accumulator
STOS: Copies a string in accumulator to memory at [DI]
Use the REP/REPE/REPNE prefixes for multiple operations
e.g.
CLD Clear direction flag, i.e. SI will be auto incremented.
AAiT
CLD Clear direction flag, i.e. SI will be auto incremented.
MOV SI, OFFSET SOURCE_STRING Point SI at start of string
LODS SOURCE_STRING Copy byte or word from string to AL or AX
MOV CX, 04H Load length of string into CX as counter
REP
MOVSB Decrement CX and copy string bytes until CX = 0
MOV DI, OFFGET TARGET_STRING Point DI at destination
STOS TARGET_STRING Store string starting @ [DI]
13ECEgECEgECEgECEg ---- 4501450145014501
String Instructions…cntd
CMPS {dest string, source string}
The comparison is done by subtracting the byte or word pointed to
by DI from the byte or word pointed to by SI.
The AF, CF, OF, PF, SF, and ZF flags are affected by the comparison,
but neither of the operands is affected.
AAiT
but neither of the operands is affected.
e.g.
MOV SI, OFFSET FIRST-STRING Point SI at source string
MOV DI, OFFSET SECOND-STRING Point DI at destination string
CLD DF cleared, so SI and DI will auto-increment after compare
MOV CX,100 Put number of string elements in CX
REPE Repeat the comparison of string bytes until end
CMPSB of string or until compared bytes are not equal
14ECEgECEgECEgECEg ---- 4501450145014501
5. Program Execution Instructions
CALL subroutine
There are two basic types of calls, near and far.
Near: is a call to a procedure, which is in the same segment
as the CALL instruction.
Far: a call to a procedure, which is in a different segment from the
one that contains the CALL instruction.
AAiT
CPU saves the return address in stack before branching to the
subroutine
e.g.
CALL BX near call to a subroutine whose address is in BX
CALL SAMPLE far call to a subroutine labeled SAMPLE
There should be the RET instruction at the end of a subroutine
15ECEgECEgECEgECEg ---- 4501450145014501
... Program Execution Instructions
JUMP instructions
JMP/JE/JZ/JC/JO/JA/JB/JG/JGE/JL/JLE…address
Address: is the destination to jump to
Short jump: within the same segment; long jump: to a different seg.
e.g.
…
AAiT
…
NEXT: CMP BX,DX ;Compare (BX-DX)
JGE DONE ;Jump to DONE if BX ≥ DX
SUB BX,AX ;Else subtract AX from BX
INC CX ;Increment counter
JMP NEXT ;Check again
DONE: MOV AX,CX ;Copy count to AX
IN AL, 8FH ;Read data from port 8FH
SUB AL,30H ;Subtract 30h from AX
JZ START_MACHINE ;Jump to label if result of subtraction was 0
16ECEgECEgECEgECEg ---- 4501450145014501
... Program Execution Instructions
LOOP instructions
LOOP/LOOPE/LOOPNE label
LOOP: loop while CX ≠ 0 aŌer auto decrement
LOOPE/LOOPZ: loop while CX ≠ 0 or ZF = 1
LOOPNE/LOOPNZ: loop while CX ≠ 0 or ZF = 0
e.g.
AAiT
e.g.
MOV BX, OFFSET ARRAY ;Point BX to start of array
DEC BX
MOV CX, 100 ;Put number of array elements in CX
NEXT: INC BX ;Point to next element in array
CMP [BX], FFH ;Compare array element with FFH
LOOPE NEXT ;Loop until an array element is equal to
;FFH or all elements of the array are checked.
17ECEgECEgECEgECEg ---- 4501450145014501
... Program Execution Instructions
Interrupt instruction
INT type
Type: a number between 0 and 255 which identifies the interrupt.
• CPU pushes current program address and status in to stack before
executing the ISR.
• In DOS, INT 21 is used for basic I/O functions, like display, print…
AAiT
• In DOS, INT 21 is used for basic I/O functions, like display, print…
Example of DOS INT 21 ISRs are
o INT 21h / AH=1 read character from standard input, result is
stored in AL. If there is no character in the keyboard
buffer, the function waits until any key is pressed.
o INT 21h / AH=2 write character to standard output. DL = character to write
o INT 21h / AH=9 printout a string from DS:DX. String must be terminated by '$’
18ECEgECEgECEgECEg ---- 4501450145014501
... Program Execution Instructions
…Interrupt instruction
e.g. (prints a number in binary on the console window)
…..
mov al, 5 ; 05h, or 00000101b
mov bl, 10 ; 0Ah, or 00001010b
add bl, al ; 5 + 10 = 15 (decimal), 0Fh, or 00001111b
; print result in binary
mov cx, 8 ;for the 8 bits
print: mov ah, 2 ; print function.
AAiT
print: mov ah, 2 ; print function.
mov dl, '0‘
test bl, 10000000b ; test first bit.
jz zero
mov dl, '1‘
zero: int 21h ;print to console
shl bl, 1
loop print
; print binary suffix:
mov dl, ‘b’
int 21h
;Print the string “hello world”
msg db "hello world $“
mov dx, offset msg
mov ah, 9
int 21h
;wait for any key press
mov ah, 1
int 21h
ret ;return to the operating system
19ECEgECEgECEgECEg ---- 4501450145014501
6. Processor control Instructions
flag set/clear instruction
STC/CLC: set/clear the carry flag
STD/CLD: set/clear the direction flag, etc.
HLT Stops the processor
To get the processor out of the halt state, apply an interrupt signal
on the INTR pin, an interrupt signal on the NMI pin, or a reset
signal on the RESET input.
AAiT
signal on the RESET input.
WAIT puts the processor in idle state (no processing)
CPU stays in this idle state until the TEST input pin is made low or
an interrupt signal is received on the INTR or the NMI input pins.
NOP fetch- decode- no execution
refer: ESC, LOCK instruction for multi-processor system
20ECEgECEgECEgECEg ---- 4501450145014501

More Related Content

What's hot

Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
9840596838
 
8086-instruction-set-ppt
 8086-instruction-set-ppt 8086-instruction-set-ppt
8086-instruction-set-ppt
jemimajerome
 
Stack in microprocessor 8085(presantation)
Stack in microprocessor 8085(presantation)Stack in microprocessor 8085(presantation)
Stack in microprocessor 8085(presantation)
Safin Biswas
 

What's hot (20)

8086 Microprocessor
8086 Microprocessor8086 Microprocessor
8086 Microprocessor
 
Emu8086
Emu8086Emu8086
Emu8086
 
ARM Fundamentals
ARM FundamentalsARM Fundamentals
ARM Fundamentals
 
Microprocessor 8086 instruction description
Microprocessor 8086 instruction descriptionMicroprocessor 8086 instruction description
Microprocessor 8086 instruction description
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
 
N_Asm Assembly arithmetic instructions (sol)
N_Asm Assembly arithmetic instructions (sol)N_Asm Assembly arithmetic instructions (sol)
N_Asm Assembly arithmetic instructions (sol)
 
Assembly language 8086
Assembly language 8086Assembly language 8086
Assembly language 8086
 
Stacks & subroutines 1
Stacks & subroutines 1Stacks & subroutines 1
Stacks & subroutines 1
 
Assembly language part I
Assembly language part IAssembly language part I
Assembly language part I
 
ARM instruction set
ARM instruction  setARM instruction  set
ARM instruction set
 
8086-instruction-set-ppt
 8086-instruction-set-ppt 8086-instruction-set-ppt
8086-instruction-set-ppt
 
Instruction set
Instruction setInstruction set
Instruction set
 
ARM lab programs
ARM  lab programs  ARM  lab programs
ARM lab programs
 
Instruction set of 8086 Microprocessor
Instruction set of 8086 Microprocessor Instruction set of 8086 Microprocessor
Instruction set of 8086 Microprocessor
 
Assembly Language Lecture 5
Assembly Language Lecture 5Assembly Language Lecture 5
Assembly Language Lecture 5
 
Assembly Language Lecture 4
Assembly Language Lecture 4Assembly Language Lecture 4
Assembly Language Lecture 4
 
Lecture4
Lecture4Lecture4
Lecture4
 
Instruction formats-in-8086
Instruction formats-in-8086Instruction formats-in-8086
Instruction formats-in-8086
 
Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086
 
Stack in microprocessor 8085(presantation)
Stack in microprocessor 8085(presantation)Stack in microprocessor 8085(presantation)
Stack in microprocessor 8085(presantation)
 

Viewers also liked (7)

[ASM] Lab2
[ASM] Lab2[ASM] Lab2
[ASM] Lab2
 
Unit 5 assembly language programming
Unit 5   assembly language programmingUnit 5   assembly language programming
Unit 5 assembly language programming
 
Mp lab manual
Mp lab manualMp lab manual
Mp lab manual
 
8086 microprocessor lab manual
8086 microprocessor lab manual8086 microprocessor lab manual
8086 microprocessor lab manual
 
8086 assembly language
8086 assembly language8086 assembly language
8086 assembly language
 
Microprocessor chapter 9 - assembly language programming
Microprocessor  chapter 9 - assembly language programmingMicroprocessor  chapter 9 - assembly language programming
Microprocessor chapter 9 - assembly language programming
 
8086 Architecture
8086 Architecture8086 Architecture
8086 Architecture
 

Similar to Lecture5(1)

Chap 3_2.ppt
Chap 3_2.pptChap 3_2.ppt
Chap 3_2.ppt
inian2
 
Topic 6 - Programming in Assembly Language_230517_115118.pdf
Topic 6 - Programming in Assembly Language_230517_115118.pdfTopic 6 - Programming in Assembly Language_230517_115118.pdf
Topic 6 - Programming in Assembly Language_230517_115118.pdf
ezaldeen2013
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
aviban
 
Https _doc-0o-c4-apps-viewer.googleusercontent
Https  _doc-0o-c4-apps-viewer.googleusercontent Https  _doc-0o-c4-apps-viewer.googleusercontent
Https _doc-0o-c4-apps-viewer.googleusercontent
vijaydeepakg
 

Similar to Lecture5(1) (20)

Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
 
Chap 3_2.ppt
Chap 3_2.pptChap 3_2.ppt
Chap 3_2.ppt
 
instruction-set-of-8086-mr-binu-joy3.ppt
instruction-set-of-8086-mr-binu-joy3.pptinstruction-set-of-8086-mr-binu-joy3.ppt
instruction-set-of-8086-mr-binu-joy3.ppt
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
 
Instructionsetof8086 180224060745(3)
Instructionsetof8086 180224060745(3)Instructionsetof8086 180224060745(3)
Instructionsetof8086 180224060745(3)
 
8086 instructions
8086 instructions8086 instructions
8086 instructions
 
Instruction sets of 8086
Instruction sets of 8086Instruction sets of 8086
Instruction sets of 8086
 
8086 instruction set
8086 instruction set8086 instruction set
8086 instruction set
 
Intrl 8086 instruction set
Intrl 8086 instruction setIntrl 8086 instruction set
Intrl 8086 instruction set
 
Instruction Set of 8086 Microprocessor
Instruction Set of 8086 MicroprocessorInstruction Set of 8086 Microprocessor
Instruction Set of 8086 Microprocessor
 
Mastering Assembly Language: Programming with 8086
Mastering Assembly Language: Programming with 8086Mastering Assembly Language: Programming with 8086
Mastering Assembly Language: Programming with 8086
 
Introduction of 8086 micro processor .
Introduction of 8086 micro processor .Introduction of 8086 micro processor .
Introduction of 8086 micro processor .
 
Topic 6 - Programming in Assembly Language_230517_115118.pdf
Topic 6 - Programming in Assembly Language_230517_115118.pdfTopic 6 - Programming in Assembly Language_230517_115118.pdf
Topic 6 - Programming in Assembly Language_230517_115118.pdf
 
8085 instruction set
8085 instruction set8085 instruction set
8085 instruction set
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
 
Arithmetic instrctions
Arithmetic instrctionsArithmetic instrctions
Arithmetic instrctions
 
Lec04
Lec04Lec04
Lec04
 
Lec04
Lec04Lec04
Lec04
 
Https _doc-0o-c4-apps-viewer.googleusercontent
Https  _doc-0o-c4-apps-viewer.googleusercontent Https  _doc-0o-c4-apps-viewer.googleusercontent
Https _doc-0o-c4-apps-viewer.googleusercontent
 
Microprocessor Based Design and operations
Microprocessor Based Design and operationsMicroprocessor Based Design and operations
Microprocessor Based Design and operations
 

More from misgina Mengesha (14)

Lecture9
Lecture9Lecture9
Lecture9
 
Lecture5
Lecture5Lecture5
Lecture5
 
Lecture3
Lecture3Lecture3
Lecture3
 
Lecture2
Lecture2Lecture2
Lecture2
 
Lecture1
Lecture1Lecture1
Lecture1
 
Sprabq8
Sprabq8Sprabq8
Sprabq8
 
Sensors2006
Sensors2006Sensors2006
Sensors2006
 
Sensors31
Sensors31Sensors31
Sensors31
 
Sensing for robotics and control s set13
Sensing for robotics and control s set13Sensing for robotics and control s set13
Sensing for robotics and control s set13
 
Introduction to sensors
Introduction to sensorsIntroduction to sensors
Introduction to sensors
 
Cn3 sensors and transducers-12
Cn3 sensors and transducers-12Cn3 sensors and transducers-12
Cn3 sensors and transducers-12
 
Cn3 sensors and transducers-1
Cn3 sensors and transducers-1Cn3 sensors and transducers-1
Cn3 sensors and transducers-1
 
480 sensors
480 sensors480 sensors
480 sensors
 
Wwwwww
WwwwwwWwwwww
Wwwwww
 

Lecture5(1)

  • 1. Lecture 05 Programming The 8086 In AAiT Programming The 8086 In Assembly Language …Continued Daniel D.Daniel D.Daniel D.Daniel D. DECEECEgECEgECEgECEg –––– 4501 Microcomputers & Interfacing4501 Microcomputers & Interfacing4501 Microcomputers & Interfacing4501 Microcomputers & Interfacing
  • 2. Basic Instructions In This Lecture AAiT Details of the basic instructions 1ECEgECEgECEgECEg ---- 4501450145014501
  • 3. Basic Instructions 1. Data transfer instructions MOV, LEA, IN/OUT, PUSH/POP, PUSHF/POPF 2. Arithmetic instructions ADD, INC, SUB, DEC, NEG, COMP, MUL/IMUL, DIV/IDIV 3. Bit manipulation instructions logical (AND, OR, TEST, XOR), SHR/SHL, RCL/RCR AAiT logical (AND, OR, TEST, XOR), SHR/SHL, RCL/RCR 4. String instructions REP/REPE/REPNE, LOADS, STOS 5. Program execution transfer instructions JMP/JE/JG/JL/JGE/JLE/JC, CALL/RET, LOOP, INT/IRET 6. Processor control instructions flags(set/clear), HLT, WAIT, NOP 2ECEgECEgECEgECEg ---- 4501450145014501
  • 4. 1. Data Transfer Instructions MOV destination, source Destination: can be a register or a memory location. Source: can be a register, a memory location, or an immediate number. The source and destination must both be of type byte, or they AAiT The source and destination must both be of type byte, or they must both be of type word. e.g. MOV CX, 037AH Put the immediate number 037AH in CX MOV BL, [437AH] Copy byte in DS at offset 437AH to BL MOV AX, BX Copy contents of register BX to AX MOV DL, [BX] Copy byte from memory at [BX] to DL. 3ECEgECEgECEgECEg ---- 4501450145014501
  • 5. PUSH source, POP destination Destination: can be a register or a memory location. Source: can be a register or a memory location The stack segment register and the stack pointer must be initialized before these instructions can be used. Data Transfer Instructions…cntd AAiT before these instructions can be used. e.g. PUSH BX Decrement SP by 2, copy BX to stack PUSH DS Decrement SP by 2, copy DS to stack POP DX Copy a word from top of stack to DX, Increment SP by 2 PUSH AL Illegal, must push a word POP CS Just Illegal PUSHF/POPF can be used with flags 4ECEgECEgECEgECEg ---- 4501450145014501
  • 6. IN accumulator, port OUT port, accumulator Port: 8 or 16-bit I/O port address. Can be fixed or variable For the fixed-port type, the 8-bit address of a port is specified directly in the instruction. e.g. Data Transfer Instructions…cntd AAiT e.g. IN AX,34H Input a word from port 34H to AX OUT 3BH,AL Copy the contents of AL to port 3BH For the variable port, the port address is loaded into the DX register before the IN/OUT instruction. e.g. MOV DX, 0FF78H Initialize DX to point to port IN AL, DX Input a byte from 8-bit port 0FF78H to AL 5ECEgECEgECEgECEg ---- 4501450145014501
  • 7. LEA register, source (load effective address) This instruction determines the offset of the variable or memory location named as the source and puts this offset in the indicated 16-bit register. Data Transfer Instructions…cntd AAiT e.g. LEA BX, PRICES Load BX with offset of PRICES in DS LEA BP, SS: STACK_TOP Load BP with offset of STACK_TOP in SS Assume PRICES is an array of bytes in data segment. The instruction LEA BX, PRICES will load the displacement of the first price in the list directly into BX. 6ECEgECEgECEgECEg ---- 4501450145014501
  • 8. ADD destination, source /also ADC (add with carry) e.g. ADD AL, 74H Add immediate number 74H to contents of AL. Result In AL. ADD DX,[SI] Add word from memory at offset [SI] in DS to 2. Arithmetic Instructions AAiT ADD DX,[SI] Add word from memory at offset [SI] in DS to contents of DX. Result in DX. NB: Arithmetic instructions affect the flags. INC destination can be used to add 1 to a number e.g INC BX, INC CX, … INC doesn’t affect the carry flag. 7ECEgECEgECEgECEg ---- 4501450145014501
  • 9. SUB destination, source e.g. SUB CX, BX CX - BX. Result in CX SUB AX,3427H Subtract immediate number 3427H from AX The decrement instruction DEC destination can be used to subtract 1 from a number 2. Arithmetic Instructions…cntd AAiT subtract 1 from a number DEC CX subtract 1 from the content of the CX register The NEG destination instruction can be used to form 2’s complement of the number in destination NEG AL Replace number in AL with its 2’s complement The CMP source, dest is used to compare two numbers CMP BH, CL compare a byte in BH with a byte in CL 8ECEgECEgECEgECEg ---- 4501450145014501
  • 10. MUL source IMUL source (signed) •Source: can be memory or registers. •one of the operands is in AL if source contains a byte, else in AX if source holds a word. AL x source = AH: AL (AX)……………byte x byte = word 2. Arithmetic Instructions…cntd AAiT AL x source = AH: AL (AX)……………byte x byte = word AX x source = DX: AX ………………..word x word = double word e.g. MUL BH AL times BH, result in AX MUL CX AX times CX, result high word in DX, low word in AX •IMUL can be used the same way for signed numbers taking care of the sign bit. 9ECEgECEgECEgECEg ---- 4501450145014501
  • 11. DIV source IDIV source (signed) • Source: contains the divisor. It can be in memory or registers. • The dividend is in AX if word/byte or in DX:AX if double/word • Word/byte: AX/source…quotient in AL, remainder in AH • Double/word: DX:AX/source…quotient in AX, remainder in DX e.g. 2. Arithmetic Instructions…cntd AAiT e.g. DIV BL Divide word in AX by byte in BL. Quotient in AL, remainder in AH. DIV CX Divide double word in DX and AX by word in CX. Quotient in AX, remainder in DX. •IMUL can be used the same way for signed numbers taking care of the sign bit. •CPU generates interrupts on divide by zero and quotient overflow 10ECEgECEgECEgECEg ---- 4501450145014501
  • 12. 3. Bit-oriented Instructions Logical (AND/TEST, OR, XOR, NOT) AND/OR/XOR dest, source NOT dest Do the indicated logic operation on the given source and destination operands and keep the result in destination e.g. AAiT e.g. AND BH,CL AND byte in BH with byte in CL Result in BH TEST AL, BH AND BH with AL, no result stored. Update PF, ZF OR BL,80H BL ORed with immediate 80H. Set MSB of BL to a 1 XOR CL,BH Byte in BH Exclusive-ORed with byte in CL. Result in CL. BH not chanced. NOT BX Complement contents of BX register Logical operators affect the zero flag and the parity flag 11ECEgECEgECEgECEg ---- 4501450145014501
  • 13. Bit-oriented Instructions…cntd SHL dest, count SHR dest, count Shift the binary number in dest. count times CF MSB LSB 0 SHL ( *2) 0 MSB LSB CF SHR (/2) e.g. AAiT e.g. SHL BX, 1 Shift word in BX 1 bit position left, 0 in LSB MOV CL, 02H Load desired number of shifts in CL SHL BP, CL Shift word in BP left (CL) bit positions, 0's in 2 LSBs SHR AL, 4 Shift AL four bit positions right and put 0's in upper 4 bits. • shift operators affect CF, ZF, OF, SF, and PF 12ECEgECEgECEgECEg ---- 4501450145014501
  • 14. 4. String Instructions MOVS {dest string, source string} LODS source / STOS dest MOVS: copies string in data segment to a string in extra segment LODS: copies a string in memory pointed by SI in to accumulator STOS: Copies a string in accumulator to memory at [DI] Use the REP/REPE/REPNE prefixes for multiple operations e.g. CLD Clear direction flag, i.e. SI will be auto incremented. AAiT CLD Clear direction flag, i.e. SI will be auto incremented. MOV SI, OFFSET SOURCE_STRING Point SI at start of string LODS SOURCE_STRING Copy byte or word from string to AL or AX MOV CX, 04H Load length of string into CX as counter REP MOVSB Decrement CX and copy string bytes until CX = 0 MOV DI, OFFGET TARGET_STRING Point DI at destination STOS TARGET_STRING Store string starting @ [DI] 13ECEgECEgECEgECEg ---- 4501450145014501
  • 15. String Instructions…cntd CMPS {dest string, source string} The comparison is done by subtracting the byte or word pointed to by DI from the byte or word pointed to by SI. The AF, CF, OF, PF, SF, and ZF flags are affected by the comparison, but neither of the operands is affected. AAiT but neither of the operands is affected. e.g. MOV SI, OFFSET FIRST-STRING Point SI at source string MOV DI, OFFSET SECOND-STRING Point DI at destination string CLD DF cleared, so SI and DI will auto-increment after compare MOV CX,100 Put number of string elements in CX REPE Repeat the comparison of string bytes until end CMPSB of string or until compared bytes are not equal 14ECEgECEgECEgECEg ---- 4501450145014501
  • 16. 5. Program Execution Instructions CALL subroutine There are two basic types of calls, near and far. Near: is a call to a procedure, which is in the same segment as the CALL instruction. Far: a call to a procedure, which is in a different segment from the one that contains the CALL instruction. AAiT CPU saves the return address in stack before branching to the subroutine e.g. CALL BX near call to a subroutine whose address is in BX CALL SAMPLE far call to a subroutine labeled SAMPLE There should be the RET instruction at the end of a subroutine 15ECEgECEgECEgECEg ---- 4501450145014501
  • 17. ... Program Execution Instructions JUMP instructions JMP/JE/JZ/JC/JO/JA/JB/JG/JGE/JL/JLE…address Address: is the destination to jump to Short jump: within the same segment; long jump: to a different seg. e.g. … AAiT … NEXT: CMP BX,DX ;Compare (BX-DX) JGE DONE ;Jump to DONE if BX ≥ DX SUB BX,AX ;Else subtract AX from BX INC CX ;Increment counter JMP NEXT ;Check again DONE: MOV AX,CX ;Copy count to AX IN AL, 8FH ;Read data from port 8FH SUB AL,30H ;Subtract 30h from AX JZ START_MACHINE ;Jump to label if result of subtraction was 0 16ECEgECEgECEgECEg ---- 4501450145014501
  • 18. ... Program Execution Instructions LOOP instructions LOOP/LOOPE/LOOPNE label LOOP: loop while CX ≠ 0 aŌer auto decrement LOOPE/LOOPZ: loop while CX ≠ 0 or ZF = 1 LOOPNE/LOOPNZ: loop while CX ≠ 0 or ZF = 0 e.g. AAiT e.g. MOV BX, OFFSET ARRAY ;Point BX to start of array DEC BX MOV CX, 100 ;Put number of array elements in CX NEXT: INC BX ;Point to next element in array CMP [BX], FFH ;Compare array element with FFH LOOPE NEXT ;Loop until an array element is equal to ;FFH or all elements of the array are checked. 17ECEgECEgECEgECEg ---- 4501450145014501
  • 19. ... Program Execution Instructions Interrupt instruction INT type Type: a number between 0 and 255 which identifies the interrupt. • CPU pushes current program address and status in to stack before executing the ISR. • In DOS, INT 21 is used for basic I/O functions, like display, print… AAiT • In DOS, INT 21 is used for basic I/O functions, like display, print… Example of DOS INT 21 ISRs are o INT 21h / AH=1 read character from standard input, result is stored in AL. If there is no character in the keyboard buffer, the function waits until any key is pressed. o INT 21h / AH=2 write character to standard output. DL = character to write o INT 21h / AH=9 printout a string from DS:DX. String must be terminated by '$’ 18ECEgECEgECEgECEg ---- 4501450145014501
  • 20. ... Program Execution Instructions …Interrupt instruction e.g. (prints a number in binary on the console window) ….. mov al, 5 ; 05h, or 00000101b mov bl, 10 ; 0Ah, or 00001010b add bl, al ; 5 + 10 = 15 (decimal), 0Fh, or 00001111b ; print result in binary mov cx, 8 ;for the 8 bits print: mov ah, 2 ; print function. AAiT print: mov ah, 2 ; print function. mov dl, '0‘ test bl, 10000000b ; test first bit. jz zero mov dl, '1‘ zero: int 21h ;print to console shl bl, 1 loop print ; print binary suffix: mov dl, ‘b’ int 21h ;Print the string “hello world” msg db "hello world $“ mov dx, offset msg mov ah, 9 int 21h ;wait for any key press mov ah, 1 int 21h ret ;return to the operating system 19ECEgECEgECEgECEg ---- 4501450145014501
  • 21. 6. Processor control Instructions flag set/clear instruction STC/CLC: set/clear the carry flag STD/CLD: set/clear the direction flag, etc. HLT Stops the processor To get the processor out of the halt state, apply an interrupt signal on the INTR pin, an interrupt signal on the NMI pin, or a reset signal on the RESET input. AAiT signal on the RESET input. WAIT puts the processor in idle state (no processing) CPU stays in this idle state until the TEST input pin is made low or an interrupt signal is received on the INTR or the NMI input pins. NOP fetch- decode- no execution refer: ESC, LOCK instruction for multi-processor system 20ECEgECEgECEgECEg ---- 4501450145014501