You are on page 1of 29

8086 Assembly Programming

Lecture4-Part2-8086 Assembly Programming


8086 Assembly Programming

 Logic Operations

 Branching Instructions

 Arithmetic Instructions

 Other Instructions

 Procedures
8086 Assembly Programming
Logic Operations
AND, OR, XOR, NOT, SHIFT

AND Destination, Source


BL
MOV BL, 35H ; 00110101
00000101
AND BL, 0FH ; 00001111
SF=0 , ZF= 0 , PF=1 ,
CF= 0 , OF=0
; Check for zero operand
AND BL, BL
JZ L1
-----
L1: MOV BL, 1
8086 Assembly Programming
Logic Operations
AND, OR, XOR, NOT, SHIFT

; mask certain bits of an operand


AND BL, 00101111b BL
AND BL, 11111011b 00101011

; Convert small char into capital a A


97 65
MOV AH, 1 01100001 01000001
INT 21H
AND AL, 11011111b
MOV DL, AL
MOV AH,2
INT 21H
8086 Assembly Programming
Logic Operations
AND, OR, XOR, NOT, SHIFT

OR Destination, Source
BL
MOV BL, 35H ; 00110101
00111111
OR BL, 0FH ; 00001111
SF=0 , ZF= 0 , PF=1 ,
; check for zero operand CF= 0 , OF=0
OR AL, AL ; or
OR AL, 0
; set certain bits of an operand AL
MOV AL, 3AH ; 00111010 00111111
OR AL, 00000101b
8086 Assembly Programming
Logic Operations
AND, OR, XOR, NOT, SHIFT

XOR Destination, Source


BL
MOV BL, 35H ; 00110101
00111010
XOR BL, 0FH ; 00001111
SF=0 , ZF= 0 , PF=1 ,
CF= 0 , OF=0
; check if 2 registers have the same content
MOV BL, 35H ; 00110101 AL
MOV AL, 35H ; 00110101 00000000
XOR AL, BL SF=0 , ZF= 1 , PF=1 ,
CF= 0 , OF=0
8086 Assembly Programming
Logic Operations
AND, OR, XOR, NOT, SHIFT

; toggle certain bits of an operand


MOV BL, 35H ; 00110101 BL
XOR BL, 02H ; 00000010 00110111

; clear the content of a register


MOV AL, 35H ; 00110101 AL
XOR AL, AL 00000000
8086 Assembly Programming
Logic Operations
AND, OR, XOR, NOT, SHIFT

; toggle certain bits of an operand


MOV BL, 35H ; 00110101 BL
XOR BL, 02H ; 00000010 00110111

; clear the content of a register


MOV AL, 35H ; 00110101 AL
XOR AL, AL 00000000
8086 Assembly Programming
Logic Operations
AND, OR, XOR, NOT, SHIFT

NOT dest

; 1’s complement of an operand AL


MOV AL, 00011010b
11100101
NOT AL
SF=1 , ZF= 0 , PF=0 ,
CF= 0 , OF=0
8086 Assembly Programming
Logic Operations
AND, OR, XOR, NOT, SHIFT

SHR dest, 1 ; shift once

SHL dest, CL ; cl contains number of shifts

0 MSB LSB CF
MOV AL, 10101100b
SHR AL, 1
CF MSB LSB 0
MOV CL, 3
SHR AL, CL
8086 Assembly Programming
Branching Instructions
JMP, LOOP, JNZ/JNE, JZ/JE

JMP label ;jump unconditional

Example:
XOR AX, AX
L1: INC AX
JMP L1
MOV BX, 20 ; unreachable instruction
8086 Assembly Programming
Branching Instructions
JMP, JNZ/JNE, JZ/JE, LOOP

JMP label ;jump unconditional


CMP Destination, Source
Example:
XOR AX, AX
L1: INC AX
CMP AX, 1000
JZ L2
JMP L1
L2: MOV BX, 20 ; reachable instruction
8086 Assembly Programming
Branching Instructions
JMP, JNZ/JNE, JZ/JE, LOOP
LOOP label ; repeat
XOR AX, AX
MOV CX, 1000
L1: INC AX
LOOP L1
MOV BX, 20 ; reachable instruction
8086 Assembly Programming
Branching Instructions
JMP, JNZ/JNE, JZ/JE, LOOP
Write a program that adds numbers from 1 to 100
8086 Assembly Programming
Branching Instructions
JMP, JNZ/JNE, JZ/JE, LOOP
Write a program that adds numbers from 1 to 100
.MODEL SMALL
.DATA
SUM DW 0
.CODE
.STARTUP
SUB BX,BX
MOV BX,1
MOV CL, 100
L1:
ADD SUM, BX
INC BX
LOOP L1
MOV AH, 4CH
INT 21H
END
8086 Assembly Programming
Branching Instructions
JMP, JNZ/JNE, JZ/JE, LOOP
Write programs that print the following patterns
********* *
*
*
*
8086 Assembly Programming
Branching Instructions
JMP, JNZ/JNE, JZ/JE, LOOP
.model small .model small
.data .data
count=10 l2:
count=10 .code
.code .startup mov dl,' '
mov cl,count mov ah,2
.startup int 21h
sub bl,bl
sub bh,bh dec bl
mov cl, count jnz l2
L1: l1:
mov dl,13 mov dl, '*'
mov dl, '*' mov ah,2
mov ah,2
mov ah, 2 int 21h int 21h
int 21h
mov dl,10 loop l1
mov ah,2
mov dl, ' ' int 21h end
mov ah, 2
int 21h mov bl,bh
loop L1 inc bl
mov bh, bl
end
8086 Assembly Programming
Arithmetic Instructions
ADD, SUB, INC, DEC, MUL, DIV
ADD Destination, Source
ADD AX, BX ; AX=AX+BX
SUB Destination, Source
SF=1 , ZF= 0 , PF=0 ,
SUB AX, BX ; AX=AX-BX CF= 0 , OF=0
INC source
INC AX ; AX=AX+1
DEC source
DEC AX ; AX=AX-1
8086 Assembly Programming
Arithmetic Instructions
ADD, SUB, INC, DEC, MUL, DIV
Write a program that compute 5+3-4+1
8086 Assembly Programming
Arithmetic Instructions
ADD, SUB, INC, DEC, MUL, DIV
Write a program that compute 5+3-4+1
.MODEL SMALL
.DATA
SUM DW 0
.CODE
.STARTUP
MOV AX,5
ADD AX,3
SUB AX,4
INC AX
MOV SUM,AX
MOV AH, 4CH
INT 21H
END
8086 Assembly Programming
Arithmetic Instructions
ADD, SUB, INC, DEC, MUL, DIV

MOV AL, Source


MUL Multiplier AL X 8 bits src
=
MOV AL,05
MOV CL,03 AH AL
MUL CL ; reg. or mem.
MOV MEM1, AX AX X 16 bits src
=
DX AX
8086 Assembly Programming
Arithmetic Instructions
ADD, SUB, INC, DEC, MUL, DIV

MOV AL, Source AX


DIV Divisor

MOV AX,600 8 bit src.


MOV CL,03 =
DIV CL ; reg. or mem. AH AL
MOV MEM1, AL
MOV MEM2, AH
8086 Assembly Programming
Other Instructions
XCHG, PTR, EQU, DUP
XCHG AX, BX ; put AX in BX and BX in AX
XCHG MEM1, AX ; put AX in MEM1 and MEM1 in AX
XCHG MEM1, MEM2 ; illegal- can’t exchange memory locations

.DATA
Num DD 0
.CODE
MOV AX, WORD PTR Num[0]
MOV DX, WORD PTR Num[2]
8086 Assembly Programming
Other Instructions
XCHG, PTR, EQU, DUP
EQU (=) can be used to define constants that can’t be changed
during execution. No memory location is allocated!
.data
count equ 30 ; count = 30
.code
-----
mov cx, count
-----
mov ax, count
end
8086 Assembly Programming
Other Instructions
XCHG, PTR, EQU, DUP
DUP used to allocate memory locations with specific value

.data
Num db 10 dup(0)
.code
Mov [Num], 3
Mov [Num+1], 5
mov al,[num+1]
end
8086 Assembly Programming
Procedures
PROC, ENDP, CALL
.MODEL SMALL
Name PROC .DATA
---- MSG DB 'Hello world!',13,10, '$'
---- .CODE
.STARTUP
RET CALL Print_MSG
Name ENDP CALL Print_MSG
MOV AH, 4CH
INT 21H
Print_MSG PROC
MOV DX,OFFSET MSG
MOV AH,9
INT 21H
RET
Print_MSG ENDP
END
8086 Assembly Programming
Procedures
PROC, ENDP, CALL
.MODEL SMALL
Passing parameters to procedures: .DATA
In registers SUM DB ?
In memory .CODE
In stack .STARTUP
MOV AL,5
MOV BL,3
CALL ADD_NUMBERS
MOV AH, 4CH
INT 21H
ADD_NUMBERS PROC
ADD AL, BL
MOV SUM, AL
RET
ADD_NUMBERS ENDP
END
8086 Assembly Programming
Procedures
8086 Assembly Programming
Lab Assignments
 Write a program that adds numbers from 1 to 100.
 Write a program that print the char ‘*’ 100 times.
 Write a procedure that add 2 numbers passed to it by
registers and use it in your main code.
 Write 3 programs that print the following 3 patterns.

* * ********************* *********************
* * * * ******************
* * * * ***************
* * * ************
********************* *********
*******
****
**
*

You might also like