SlideShare a Scribd company logo
Introduction to IBM PC
Assembly Language
Outline
• Assembly language syntax
• Program data
• Variables
• Named constants
• A few basic instructions
• Translation of high-level language to assembly language
• Program structure
• Input and output instructions
2
Assembly Language Syntax
• Assembler
• Assembly language programs are translated into machine language
instructions by assembler
• Assembly language code is not case sensitive
• Statements
• Program consists of statements one per line
• Two types: instruction and assembler directive
• Statements have four fields
• name operation operand(s) comment
• Instruction- START: MOV CX,5 ;initialize counter
• Assembler directive- MAIN PROC
3
Name Field
• Used for instruction labels, procedure names and variable
names
• Assembler translates name into memory address
• Names can be 1 to 31 characters long and may consist of
letters, digits or special characters.
• If period is used, it must be first character.
• Embedded blanks are not allowed.
• May not begin with a digit.
• Not case sensitive
4
Name Field
Legal names Illegal Names
COUNTER_1 TWOWORDS
@character 2abc
.TEST A45.28
DONE? YOU&ME
5
Operation Field
• Contains symbolic (Operation code)
• Assembler translates op code translated into machine
language op code
• Examples: ADD, MOV, SUB
• In an assembler directive, the operation field represents
pseudo-op code
• Pseudo-op code is not translated into machine code, it only
tells assembler to do something.
• Example: PROC psuedo-op is used to create a procedure
6
Operand Field
• Specifies the data that are to be acted on by the operation
• An instruction may have zero, one or more operands.
• In two-operand instruction, first operand is destination,
second operand is source.
• For an assembler directive, operand field represents more
information about the directive
• Examples
NOP ;no operand, does nothing
INCAX ;one operand, adds 1 to the contents of AX
ADDWORD1, 2; two operands, adds value 2 to the contents
of memory locationWORD1
7
Comment Field
• Say something about what the statement does
• Marked by semicolon in the beginning
• Assembler ignores anything after semicolon
• Optional
• Good practice
8
Program Data
• Processor operates only on binary data.
• In assembly language, you can express data in:
• Binary
• Decimal
• Hexadecimal
• Characters
• Numbers
• For Hexadecimal, the number must begin with a decimal digit. E.g.: write
0ABCh not only ABCH.
• Cannot contain any non-digit character. E.g.: 1,234 not allowed
• Characters enclosed in single or double quotes.
• ASCII codes can be used
• No difference in “A” and 41h
9
Variables
• Each variable has a data type and is assigned a memory
address by the program.
• PossibleValues:
• 8 Bit Number Range: Signed (-128 to 127), Unsigned (0-255)
• 16 Bit Number Range: Signed (-32,678 to 32767), Unsigned (0-65,535)
• ?To leave variable uninitialized
• Mainly three types
• Byte variables
• Word variables
• Arrays
10
Data Defining Pseudo-Ops
11
Examples
Bytes
Description
Pseudo-ops
var1 DB ‘A’
Var2 DB ?
array1 DB 10, 20,30,40
1
Define Byte
DB
var2 DW ‘AB’
array2 DW 1000, 2000
2
DefineWord
DW
Var3 DD -214743648
4
Define Double
Word
DD
Var DQ ?
8
Define Quad
Word
DQ
Var DT ?
10
DefineTen Bytes
DT
Example
• Show how character string “RG 2z” is stored in memory
starting at address 0.
• Solution:
12
Address Character ASCII Code (HEX) ASCII Code (Binary)
[Memory Contents]
0 R 52 0101 0010
1 G 47 0100 0111
2 Space 20 0010 0000
3 2 32 0011 0010
4 z 7A 0111 1010
Named Constants
• Use symbolic name for a constant quantity
• Syntax:
• name EQU constant
• Example:
• LF EQU 0Ah
• No memory allocated
13
A Few Basic Instructions
• MOV
• XCHG
• ADD
• SUB
• INC
• DEC
• NEG
14
MOV
• Transfer data
• Between registers
• Between register and a memory location
• Move a number directly to a register or a memory location
• Syntax
• MOV destination, source
• Example
• MOVAX,WORD1
15
0006
0008
0008
0008
AX
WORD1
Before After
Legal Combinations of Operands for
MOV
16
Destination
Operand
Source Operand Legal
General Register General Register YES
General Register Memory Location YES
General Register Segment Register YES
General Register Constant YES
Memory Location General Register YES
Memory Location Memory Location NO
Memory Location Segment Register YES
Memory Location Constant YES
XCHG
• Exchange the contents of
• Two registers
• Register and a memory location
• Syntax
• XCHG destination, source
• Example
• XCHGAH, BL
17
1A 00
00 05
AH
BH
Before After
AL
BL
00 1A
05 00
AH AL
BH BL
Destination
Operand
Source Operand Legal
General Register General Register YES
General Register Memory Location YES
Memory Location General Register YES
Memory Location Memory Location NO
Legal Combinations of Operands for
XCHG
ADD
• To add contents of
• Two registers
• A register and a memory location
• A number to a register
• A number to a memory location
• Syntax
• ADD destination, source
• Example
• ADDWORD1,AX
19
01BC
0523
01BC
06DF
AX
WORD1
Before After
SUB
• To subtract the contents of:
• Two registers
• A register and a memory location
• A number from a register
• A number from a memory location
• Syntax
• SUB destination, source
• Example
• SUB AX, DX
20
0000
0001
FFFF
0001
AX
DX
Before After
Destination Operand Source Operand Legal
General Register General Register YES
General Register Memory Location YES
General Register Constant YES
Memory Location General Register YES
Memory Location Memory Location NO
Memory Location Constant YES
21
Legal Combinations of Operands for
ADD and SUB
Memory to Memory Instruction
• ADD BYTE1, BYTE2 Illegal instruction
• Solution?
• MOVAL, BYTE2
ADD BYTE1, AL
• What can be other possible solutions?
• How can you add two word variables?
22
INC
• INC (increment) instruction is used to add 1 to the contents of
• a register
• memory location.
• Syntax:
• INC destination
• Example:
• INCWORD1
23
0002 0003
WORD1
Before After
DEC
• DEC (decrement) instruction is used to subtract 1 from the
contents of
• a register
• memory location.
• Syntax:
• DEC destination
• Example:
• DEC BYTE1
24
FFFE FFFD
BYTE1
Before After
NEG
• Used to negate the contents of destination.
• Replace the contents by its 2’s complement.
• Syntax
• NEG destination
• Example
• NEG BX
25
0002 FFFE
BX
Before After
Translation of High-level Language to
Assembly Language
• Consider instructions: MOV, ADD, SUB, INC, DEC, NEG
• A and B are two word variables
• Translate statements into assembly language:
26
Statement Translation
B = A MOV AX, A
MOV B, AX
A = 5 - A MOV AX, 5
SUB AX, A
MOV A, AX
OR
NEG A
ADD A, 5
A = B – 2 x A MOV AX, B
SUB AX, A
SUB AX, A
MOV A, AX
Program Structure
• Machine Programs consists of
• Code
• Data
• Stack
• Each part occupies a memory segment.
• Same organization is reflected in an assembly language
program as Program Segments.
• Each program segment is translated into a memory segment
by the assembler.
27
Memory Models
• Determines the size of data and code a program can have.
• Syntax:
• .MODEL memory_model
28
Model Description
SMALL code in one segment, data in one segment
MEDIUM code in more than one segment, data in one segment
COMPACT code in one segment, data in more than one segment
LARGE Both code and data in more than one segments. No array larger than 64KB
HUGE Both code and data in more than one segments.Array may be larger than
64KB
Data Segment
• All variable definitions
• Constant definitions are often made here
• Use .DATA directive
• For Example:
• .DATA
WORD1 DW 2
BYTE1 DB 10h
29
Stack Segment
• A block of memory to store stack
• Syntax
• .STACK size
• Where size is optional and specifies the stack area size in bytes
• If size is omitted, 1 KB set aside for stack area
• For example:
• .STACK 100h
30
Code Segment
• Contains a program’s instructions
• Syntax
• .CODE name
• Where name is optional
• Do not write name when using SMALL as a memory model
• Inside a code segment instructions are organized as
procedures
31
The Format of a Code
.MODEL SMALL
.STACK 100h
.DATA
;data definition go here
.CODE
MAIN PROC
;instructions go here
MAIN ENDP
;other procedures go here
END MAIN
32
Input and Output Instructions
Function
Number
Routine Function Code
1 Single key input Input:AH=1
Output:AL=ASCIICode if character is
pressed
=0 if non-character key is
pressed
MOV AH,1 ;input key
function
INT 21H ;ASCII code in
AL
2 Single character
output
Input:AH=2
DL=ASCII code of the display
character or control character
Output:AL=ASCII code of the display
character or control character
MOV AH,2 ;display
character
function
MOV DL,’? ’;character is ?
INT 21H; display character
9 Character string
output
Input: DX=offset address of string.The
string must end with a $ character
MSG DB ‘HELLO$’
33
LEA Instruction
• It means Load Effective Address
• To get the offset address of the character string in DX we use
LEA instruction
• Syntax
• LEA destination, source
• Destination is a general register and source is a memory
location
• Example
• LEA DX, MSG
34
Assignment 04
• Write a program to input an uppercase letter and display its
corresponding lowercase letter.
• Write a program to read two decimal digits whose sum is less
than 10 and display them and their sum in the next line with
appropriate message.
35

More Related Content

Similar to Introduction to Assembly Language & various basic things (20)

PPTX
Programming the basic computer
Kamal Acharya
 
PPT
12 mt06ped008
vijaydeepakg
 
PPTX
EC8691-MPMC-PPT.pptx
Manikandan813397
 
PPTX
6 assembly language computer organization
wewiv47743
 
PPT
Assembly language programming_fundamentals 8086
Shehrevar Davierwala
 
PDF
Unit 2 assembly language programming
Kartik Sharma
 
PPTX
Computer Organization
Radhika Talaviya
 
PPTX
Assembly language progarmming
Azmeyer
 
PPT
Wk1to4
raymondmy08
 
PPT
Instruction set of 8086
Tirumalesh Nizampatnam
 
PPTX
Assembly 8086
Mustafa Salah
 
PDF
Topic 6 - Programming in Assembly Language_230517_115118.pdf
ezaldeen2013
 
PPTX
Lec06
siddu kadiwal
 
PPTX
Computer Organization - Programming the basic computer : Machine Language, As...
Maitri Thakkar
 
DOCX
Microprocessor
Bathshebaparimala
 
PPTX
Coal (1)
talhashahid40
 
PPTX
Assembly fundamentals
Syed Zaid Irshad
 
PDF
Assembly_80x86- Assembly languages programming and 80x861.pdf
ahmedmohammed246810a
 
PPT
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
Frankie Jones
 
Programming the basic computer
Kamal Acharya
 
12 mt06ped008
vijaydeepakg
 
EC8691-MPMC-PPT.pptx
Manikandan813397
 
6 assembly language computer organization
wewiv47743
 
Assembly language programming_fundamentals 8086
Shehrevar Davierwala
 
Unit 2 assembly language programming
Kartik Sharma
 
Computer Organization
Radhika Talaviya
 
Assembly language progarmming
Azmeyer
 
Wk1to4
raymondmy08
 
Instruction set of 8086
Tirumalesh Nizampatnam
 
Assembly 8086
Mustafa Salah
 
Topic 6 - Programming in Assembly Language_230517_115118.pdf
ezaldeen2013
 
Computer Organization - Programming the basic computer : Machine Language, As...
Maitri Thakkar
 
Microprocessor
Bathshebaparimala
 
Coal (1)
talhashahid40
 
Assembly fundamentals
Syed Zaid Irshad
 
Assembly_80x86- Assembly languages programming and 80x861.pdf
ahmedmohammed246810a
 
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
Frankie Jones
 

Recently uploaded (20)

PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PPTX
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PPTX
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PPTX
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PPTX
Day2 B2 Best.pptx
helenjenefa1
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
MRRS Strength and Durability of Concrete
CivilMythili
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
Design Thinking basics for Engineers.pdf
CMR University
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
Day2 B2 Best.pptx
helenjenefa1
 
Ad

Introduction to Assembly Language & various basic things

  • 1. Introduction to IBM PC Assembly Language
  • 2. Outline • Assembly language syntax • Program data • Variables • Named constants • A few basic instructions • Translation of high-level language to assembly language • Program structure • Input and output instructions 2
  • 3. Assembly Language Syntax • Assembler • Assembly language programs are translated into machine language instructions by assembler • Assembly language code is not case sensitive • Statements • Program consists of statements one per line • Two types: instruction and assembler directive • Statements have four fields • name operation operand(s) comment • Instruction- START: MOV CX,5 ;initialize counter • Assembler directive- MAIN PROC 3
  • 4. Name Field • Used for instruction labels, procedure names and variable names • Assembler translates name into memory address • Names can be 1 to 31 characters long and may consist of letters, digits or special characters. • If period is used, it must be first character. • Embedded blanks are not allowed. • May not begin with a digit. • Not case sensitive 4
  • 5. Name Field Legal names Illegal Names COUNTER_1 TWOWORDS @character 2abc .TEST A45.28 DONE? YOU&ME 5
  • 6. Operation Field • Contains symbolic (Operation code) • Assembler translates op code translated into machine language op code • Examples: ADD, MOV, SUB • In an assembler directive, the operation field represents pseudo-op code • Pseudo-op code is not translated into machine code, it only tells assembler to do something. • Example: PROC psuedo-op is used to create a procedure 6
  • 7. Operand Field • Specifies the data that are to be acted on by the operation • An instruction may have zero, one or more operands. • In two-operand instruction, first operand is destination, second operand is source. • For an assembler directive, operand field represents more information about the directive • Examples NOP ;no operand, does nothing INCAX ;one operand, adds 1 to the contents of AX ADDWORD1, 2; two operands, adds value 2 to the contents of memory locationWORD1 7
  • 8. Comment Field • Say something about what the statement does • Marked by semicolon in the beginning • Assembler ignores anything after semicolon • Optional • Good practice 8
  • 9. Program Data • Processor operates only on binary data. • In assembly language, you can express data in: • Binary • Decimal • Hexadecimal • Characters • Numbers • For Hexadecimal, the number must begin with a decimal digit. E.g.: write 0ABCh not only ABCH. • Cannot contain any non-digit character. E.g.: 1,234 not allowed • Characters enclosed in single or double quotes. • ASCII codes can be used • No difference in “A” and 41h 9
  • 10. Variables • Each variable has a data type and is assigned a memory address by the program. • PossibleValues: • 8 Bit Number Range: Signed (-128 to 127), Unsigned (0-255) • 16 Bit Number Range: Signed (-32,678 to 32767), Unsigned (0-65,535) • ?To leave variable uninitialized • Mainly three types • Byte variables • Word variables • Arrays 10
  • 11. Data Defining Pseudo-Ops 11 Examples Bytes Description Pseudo-ops var1 DB ‘A’ Var2 DB ? array1 DB 10, 20,30,40 1 Define Byte DB var2 DW ‘AB’ array2 DW 1000, 2000 2 DefineWord DW Var3 DD -214743648 4 Define Double Word DD Var DQ ? 8 Define Quad Word DQ Var DT ? 10 DefineTen Bytes DT
  • 12. Example • Show how character string “RG 2z” is stored in memory starting at address 0. • Solution: 12 Address Character ASCII Code (HEX) ASCII Code (Binary) [Memory Contents] 0 R 52 0101 0010 1 G 47 0100 0111 2 Space 20 0010 0000 3 2 32 0011 0010 4 z 7A 0111 1010
  • 13. Named Constants • Use symbolic name for a constant quantity • Syntax: • name EQU constant • Example: • LF EQU 0Ah • No memory allocated 13
  • 14. A Few Basic Instructions • MOV • XCHG • ADD • SUB • INC • DEC • NEG 14
  • 15. MOV • Transfer data • Between registers • Between register and a memory location • Move a number directly to a register or a memory location • Syntax • MOV destination, source • Example • MOVAX,WORD1 15 0006 0008 0008 0008 AX WORD1 Before After
  • 16. Legal Combinations of Operands for MOV 16 Destination Operand Source Operand Legal General Register General Register YES General Register Memory Location YES General Register Segment Register YES General Register Constant YES Memory Location General Register YES Memory Location Memory Location NO Memory Location Segment Register YES Memory Location Constant YES
  • 17. XCHG • Exchange the contents of • Two registers • Register and a memory location • Syntax • XCHG destination, source • Example • XCHGAH, BL 17 1A 00 00 05 AH BH Before After AL BL 00 1A 05 00 AH AL BH BL
  • 18. Destination Operand Source Operand Legal General Register General Register YES General Register Memory Location YES Memory Location General Register YES Memory Location Memory Location NO Legal Combinations of Operands for XCHG
  • 19. ADD • To add contents of • Two registers • A register and a memory location • A number to a register • A number to a memory location • Syntax • ADD destination, source • Example • ADDWORD1,AX 19 01BC 0523 01BC 06DF AX WORD1 Before After
  • 20. SUB • To subtract the contents of: • Two registers • A register and a memory location • A number from a register • A number from a memory location • Syntax • SUB destination, source • Example • SUB AX, DX 20 0000 0001 FFFF 0001 AX DX Before After
  • 21. Destination Operand Source Operand Legal General Register General Register YES General Register Memory Location YES General Register Constant YES Memory Location General Register YES Memory Location Memory Location NO Memory Location Constant YES 21 Legal Combinations of Operands for ADD and SUB
  • 22. Memory to Memory Instruction • ADD BYTE1, BYTE2 Illegal instruction • Solution? • MOVAL, BYTE2 ADD BYTE1, AL • What can be other possible solutions? • How can you add two word variables? 22
  • 23. INC • INC (increment) instruction is used to add 1 to the contents of • a register • memory location. • Syntax: • INC destination • Example: • INCWORD1 23 0002 0003 WORD1 Before After
  • 24. DEC • DEC (decrement) instruction is used to subtract 1 from the contents of • a register • memory location. • Syntax: • DEC destination • Example: • DEC BYTE1 24 FFFE FFFD BYTE1 Before After
  • 25. NEG • Used to negate the contents of destination. • Replace the contents by its 2’s complement. • Syntax • NEG destination • Example • NEG BX 25 0002 FFFE BX Before After
  • 26. Translation of High-level Language to Assembly Language • Consider instructions: MOV, ADD, SUB, INC, DEC, NEG • A and B are two word variables • Translate statements into assembly language: 26 Statement Translation B = A MOV AX, A MOV B, AX A = 5 - A MOV AX, 5 SUB AX, A MOV A, AX OR NEG A ADD A, 5 A = B – 2 x A MOV AX, B SUB AX, A SUB AX, A MOV A, AX
  • 27. Program Structure • Machine Programs consists of • Code • Data • Stack • Each part occupies a memory segment. • Same organization is reflected in an assembly language program as Program Segments. • Each program segment is translated into a memory segment by the assembler. 27
  • 28. Memory Models • Determines the size of data and code a program can have. • Syntax: • .MODEL memory_model 28 Model Description SMALL code in one segment, data in one segment MEDIUM code in more than one segment, data in one segment COMPACT code in one segment, data in more than one segment LARGE Both code and data in more than one segments. No array larger than 64KB HUGE Both code and data in more than one segments.Array may be larger than 64KB
  • 29. Data Segment • All variable definitions • Constant definitions are often made here • Use .DATA directive • For Example: • .DATA WORD1 DW 2 BYTE1 DB 10h 29
  • 30. Stack Segment • A block of memory to store stack • Syntax • .STACK size • Where size is optional and specifies the stack area size in bytes • If size is omitted, 1 KB set aside for stack area • For example: • .STACK 100h 30
  • 31. Code Segment • Contains a program’s instructions • Syntax • .CODE name • Where name is optional • Do not write name when using SMALL as a memory model • Inside a code segment instructions are organized as procedures 31
  • 32. The Format of a Code .MODEL SMALL .STACK 100h .DATA ;data definition go here .CODE MAIN PROC ;instructions go here MAIN ENDP ;other procedures go here END MAIN 32
  • 33. Input and Output Instructions Function Number Routine Function Code 1 Single key input Input:AH=1 Output:AL=ASCIICode if character is pressed =0 if non-character key is pressed MOV AH,1 ;input key function INT 21H ;ASCII code in AL 2 Single character output Input:AH=2 DL=ASCII code of the display character or control character Output:AL=ASCII code of the display character or control character MOV AH,2 ;display character function MOV DL,’? ’;character is ? INT 21H; display character 9 Character string output Input: DX=offset address of string.The string must end with a $ character MSG DB ‘HELLO$’ 33
  • 34. LEA Instruction • It means Load Effective Address • To get the offset address of the character string in DX we use LEA instruction • Syntax • LEA destination, source • Destination is a general register and source is a memory location • Example • LEA DX, MSG 34
  • 35. Assignment 04 • Write a program to input an uppercase letter and display its corresponding lowercase letter. • Write a program to read two decimal digits whose sum is less than 10 and display them and their sum in the next line with appropriate message. 35

Editor's Notes

  • #18: Other e.g. XCHG AX, WORD1
  • #20: Source contents remain unchanged!
  • #29: .MODEL is a directive