0% found this document useful (0 votes)
18 views26 pages

Assembly Language

The document provides an overview of assembly language programming, including the structure of machine programs, syntax for various instructions, and examples of basic input/output operations. It covers key concepts such as branching, loops, arrays, and conditional jumps, along with practical examples and pseudocode comparisons. Additionally, it includes specific assembly code snippets for tasks like linear search and handling odd/even checks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views26 pages

Assembly Language

The document provides an overview of assembly language programming, including the structure of machine programs, syntax for various instructions, and examples of basic input/output operations. It covers key concepts such as branching, loops, arrays, and conditional jumps, along with practical examples and pseudocode comparisons. Additionally, it includes specific assembly code snippets for tasks like linear search and handling odd/even checks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Assembly Language Programming

Md. Nazmul Abdal


Lecturer, Department of CSE
University of Liberal Arts Bangladesh (ULAB)
Structure

Machine programs consists of:

Stack: A block of memory to store stack


Syntax: .STACK size

Data: Contains all variable definitions


Syntax: .DATA

Code: Contains a program’s instructions.


Syntax: .CODE
Format of a code
.model small
.stack 100h

.data
;data definition

.code

main proc
; instructions

main endp
end main
Some Instructions
MOV

Transfer data between registers, between register and a memory location or move a number
directly to a register or a memory location.
Syntax: MOV destination, source
Example: MOV AX, WORD1

ADD

To add contents of two registers, a register and a memory location, a number to a register or a
number to a memory location.
Syntax: ADD destination, source
Example: ADD WORD1, AX

SUB

To subtract the contents of two registers, a register and a memory location, a number from a
register or a number from a memory location.
Syntax: SUB destination, source
Example: SUB AX, DX
Instructions
INC

To increment one to the contents of a register or memory location.


Syntax: INC destination
Example: INC WORD1

DEC
To decrement one from the contents of a register or memory location.
Syntax: DEC destination
Example: DEC BYTE1

NEG
To negate the contents of destination.
Syntax: NEG destination
Example: NEG BX

XCHG

Exchange the contents of two registers or register and a memory location.


Syntax: XCHG destination, source
Example: XCHG AH, BL
C vs Assembly

C Assembly

A= 5–A MOV AX, 5


SUB AX, A
MOV A, AX

A= B – 2 *A MOV AX, B
SUB AX, A
SUB AX, A
MOV A, AX
Basic input output Program
.model small
.stack 100h
.data
.code
main proc
mov ah,1 ;takes input in al
int 21h ; interrupt
mov dl,al

mov ah,2 ;shows output from dl


int 21h ; interrupt

mov ah,4ch ;exit program


int 21h
main endp
end main
Working with variables
.model small
.stack 100h
.data
a db 5
.code
main proc
mov ax,@data ; for data
mov ds,ax
mov ah,1 ; takes input in al
int 21h ; interrupt
sub al, 48
add al,a
add al,48
mov dl,al
mov ah,2 ; shows output from dl
int 21h ; interrupt
mov ah,4ch ; exit program
int 21h
main endp
end main
ASCII Codes
Branching
Label

Labels are needed in situations where one instruction refers to another. Labels
end with a colon. Label usually placed on a line by themselves. They refer to
the instruction that follows. The JMP instruction
The JMP instruction causes an unconditional jump. JMP can be used to get around the
range restriction of a conditional jump.
Syntax: JMP destination_label

Conditional Jump
If the conditions for the jump instruction, that is the combination of status flag
settings are true, the CPU adjusts the IP to point to the destination label so that
the instruction at this label will be executed next. If the jump condition is false,
then IP is not altered. If the condition for the jump is true, the next instruction
to be executed is the one at destination_label. If the condition is false, the
instruction immediately following the jump is done next.
Syntax: JGE destination_label
Signed Conditional jump

Symbol Description

Jump if greater than


JG/JNLE Jump if not less than or equal to

Jump if greater than or equal to

JGE/JNL
Jump if not less than or equal to

Jump if less than


JL/JNGE Jump if not greater than or equal

Jump if less than or equal


JLE/JNG
Jump if not greater than
Unsigned Conditional jump

Symbol Description

Jump if above

JA/JNBE Jump if not below or equal

Jump if above or equal


JAE/JNB
Jump if not below

Jump if below

JB/JNAE Jump if not above or equal

Jump if below or equal


JBE/JNA
Jump if not above
Unsigned Conditional jump
Symbol Description

Jump if equal
JE/JZ
Jump if equal to or zero
Jump if not equal
JNE/JNZ
Jump if not zero
JC Jump if carry

JNC Jump if no carry

JO Jump if overflow

JNO Jump if no overflow

JS Jump if sign negative


JNS Jump if nonnegative sign
JP/JPE Jump if parity even
JNP/JPO Jump if parity odd
Compare (if-else)
CMP
The jump condition is often provided by the CMP (compare) instruction.
This instruction subtracts the source from the destination. The result is not
stored. Only the flags are affected. The operands of CMP may not both be
memory locations. Destination operand may not be a constant.
Syntax: CMP destination, source

Pseudocode Algorithm Assembly Code

IF AX<0 THEN CMP AX,0


replace AX by –AX JNL END_IF
END_IF NEG AX
END_IF:
Loop
Loop
This is a loop structure in which the loop statements are repeated a known number
of times. The counter for the loop is the register CX which is initialized to
loop_count. Execution of LOOP instruction causes CX to be decremented
automatically.

Syntax: LOOP destination_label

Pseudocode Algorithm Assembly Code

FOR 80 times DO MOV CX,80


display ‘*’ MOV AH,2
END_FOR MOV DL, ‘*’
TOP:
INT 21H
LOOP TOP
Arrays
One dimensional Array
• Ordered list of elements
• Same type
• Ex: A dw 10,20,30,40,50,60

Address Content
1 A[1]
A 10
2 A[2]
A+2 20
3 A[3] A+4 30
4 A[4] A+6 40
5 A[5] A+8 50
6 A[6] A+10 60
DUP Operator
• Causes the value to be repeated the number of times specified
• Ex: gamma dw 100 dup(0)
• Dup can be nested
• Ex: line db 5,4,3 dup (2,3 dup(0),1)
• line db 5,4,2,0,0,0,1,2,0,0,0,1,2,0,0,0,1
DUP Operator
• 5,4, 3 dup (2,3 dup(0),1)
• 5,4, 3 dup (2, 0, 0, 0, 1)
• 5, 4 , 2, 0, 0, 0, 1, 2, 0, 0, 0, 1, 2, 0, 0, 0, 1
Location of Array Elements
• A is an array and S denotes the number of bytes in an element.
(S=1 for byte array, S=2 for word array)
• The position of A can be determined as follows
Position Location
1 A
2 a= 1 * S
3 a= 2 * S
. .
. .
N a= (N-1) * S
Math
• Exchange 10th and 25th elements in a word array W.
Solution:
W[10] is located at address W + 9*2 = W + 18 and
W[25] is located at address W + 24*2 = W + 48,

We can exchange as follows:


MOV AX, W+18 ; AX has W[10]
XCHG W+48, AX ; AX has W[25]
MOV W+18, AX ; Complete exchange
Input in an array of size 5
.model small
.stack 100h
.data
arr db 5 dup (?)
.code
main proc
mov ax,@data
mov ds,ax
mov si, offset arr ;taking the initial address of the array to si
mov cx,5 ; as the value of cx is 5 so the loop will iterate 5 times
mov ah,1

L1:
int 21h
mov [si],al
inc si
loop L1

Exit:
mov ah,4ch ;kind of like return 0
int 21h
main endp
end main
Output from a given array
.model small
.stack 100h
.data
arr db 'a', 'b', 'c', 'd', 'e'
.code
main proc
mov ax,@data
mov ds,ax
mov si,offset arr
mov cx,5
mov ah,2

L1:
mov dl ,[si]
int 21h
inc si
loop L1

Exit:
mov ah,4ch
int 21h
main endp
end main
Divisible by 2/Odd- Even
include 'emu8086.inc'
.model small
.stack 100h
.data
.code
main proc
mov ax,@data
mov ds,ax
mov ah,1
int 21h
sub al,48
mov bh,2
div bh
cmp ah, 00h
JZ print1
print 'Odd'
jmp Exit

print1:
print 'Even'

Exit:
mov ah,4ch
int 21h
main endp
end main
Linear Search
include 'emu8086.inc' print 'Enter the character you want to search:‘
.model small mov ah,1
.stack 100h int 21h
mov cx,5
.data
mov si,offset arr
;arr db "Nazmul"
arr db 'N', 'a', 'z', 'm', 'u', 'l' Search:
.code cmp al,[si]
main proc JZ found
mov ax,@data inc si
mov ds,ax Loop Search
JMP Exit
mov ah,2
mov si,offset arr
found:
mov cx,6 mov bh,al
mov ah,2
L1: mov dl,bh
mov dl, [si] int 21h
int 21h print 'Found'
inc si
Exit:
loop L1 mov ah,4ch
Mov ah,2 //for newline int 21h
mov dl,13 main endp
int 21h end main
mov dl,10
int 21h
THANK YOU

You might also like