3 - An Introduction and Structure of Assembly Language-1
3 - An Introduction and Structure of Assembly Language-1
Sheeza Zaheer
Lecturer
University of Management and Technology
Lahore Campus
OUTLINE
2
● Introduction
■ About this course
■ About Assembly Language
■ Syntax of Assembly Language
■ Basic Instructions
■ Variables
■ Translation
■ Program Structure
● References
■ Chapter 4, Ytha Yu and Charles Marut, “Assembly
Language Programming and Organization of IBM PC
3
ASSEMBLY LANGUAGE
Computer Level Hierarchy
4
Figure Reference:
4 http://users.dickinson.edu/~braught/courses/cs251f09/topics/slides/intro.pdf
Programming Languages
5
5
High-Level Language
6
6
Machine Language
7
● The "native" language of the computer
● Numeric instructions and operands that can be stored in
memory and are directly executed by computer system.
● Each ML instruction contains an op code (operation
code) and zero or more operands.
● Examples:
7
Assembly Language
8
● Use instruction mnemonics that have one-to-one
correspondence with machine language.
An instruction is a symbolic representation of a single
machine instruction
Consists of:
label always optional
mnemonic always required
operand(s) required by some instructions
comment always optional
8
Sample Program
5. int 20
9
Essential Tools
10
● Assembler is a program that converts source-code programs
into a machine language (object file).
● Linker joins two or more object files and produces a single
executable file.
● Debugger loads an executable program, displays the source
code, and lets the programmer step through the program one
instruction at a time, and display and modify memory.
● Emulator allows you to load and run assembly language
programs, examine and change contents of registers. Example:
EMU8086
10
Why Learn Assembly Language?
11
● Learn how a processor works
■ Explore the internal representation of data and instructions
■ How to structure a program so it runs more efficiently. (High Level
Language Low Level Language)
11
12
BASIC ELEMENTS
Statements
13
● Syntax:
operation Operands
Comment
Label
; Code
Here: mov ax,count ;store count into ax
Comment
14
Name/Label Field
15
● The assembler translates names into memory addresses.
● Names can be 1 to 31 character long and may consist of letter, digit 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
15
Operation Field: Symbolic operation (Op code)
16
● Symbolic op code translated into Machine Language op code
● Examples: ADD, MOV, SUB
16
Operand Field
17
17
Comments
18
■ Optional
■ Marked by semicolon in the beginning
■ Ignored by assembler
■ Good practice
;
;initialize registers
;
MOV AX, 0
MOV BX, 0
18
Program Data
19
● Processor operates only on binary data.
● In assembly language, you can express data in:
■ Binary
■ Decimal
■ Octal
■ 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
19
■ No difference in “A” and 41h
Contd..
20
● Use a radix symbol (suffix) to select binary, octal, decimal, or
hexadecimal
6A15h ; hexadecimal
0BAF1h ; leading zero required
32o ; octal
1011b ; binary
35d ; decimal (default)
20
Variables
21
● Each variable has a data type and is assigned a memory
address by the program.
● Possible Values:
■ 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
21
Contd..
22
● Syntax
variable_name type initial_value
variable_name type value1, value2, value3
● Data Definition Directives Or Data Defining Pseudo-ops
■ DB, DW, DD, DQ, DT
myArray dw 1000h,2000h
dw 3000h,4000h
Variable name
22
Contd..
23
Pseudo-ops Description Bytes Examples
Note:
Consider
var2 DW 10h
Still in memory the value saved will be 0010h
23
Arrays
24
● Sequence of memory bytes or words
● Example 1:
B_ARRAY DB 10h, 20h, 30h
24
Example 2
25
● W_ARRAY DW 1000, 40, 29887, 329
*If W_ARRAY is assigned offset address 0300h by assembler
LETTERS DB ‘ABC’
Is equivalent to
LETTERS DB 41h, 42h, 43h
● Assembler differentiates between upper case and lower case.
● Possible to combine characters and numbers.
MSG DB ‘HELLO’, 0Ah, 0Dh, ‘$’
Is equivalent to
MSG DB 48h, 45h, 4Ch, 4Ch, 4Fh, 0Ah, 0Dh, 24h
26
Example 3
27
27
Named Constants
28
● No memory allocated
28
29
● Difference?
■ MOV AH, ‘A’
30
■ MOV AX, ‘A’
Legal Combinations of Operands for MOV
31
31
XCHG
32
● Exchange the contents of
■ Two registers (xchg ax, bx)
■ Register and a memory location (xchg ax, var1)
● Syntax
XCHG destination, source
● Example
XCHG AH, BL
Before After
1A 00 05 00
AH AL AH AL
00 05 00 1A
BH BL BH BL
32
Legal Combinations of Operands for XCHG
33
Destination Operand Source Operand Legal
AX 01BC 01BC
34
SUB Instruction
35
● To subtract the contents of:
■ Two registers (Sub ax, bx)
■ A register and a memory location (sub ax, var1)
■ A number from a register (sub ax, 1234h)
■ A number from a memory location (sub var1, 1234h)
● Syntax: SUB destination, source
● Example
SUB AX, DX
Before After
AX 0000 FFFF
DX 0001 0001
35
Legal Combinations of Operands for
ADD & SUB instructions
36
Destination Operand Source Operand Legal
36
Contd..
37
37
INC & DEC
38
● INC (increment) instruction is used to add 1 to the contents of
a register or memory location.
■ Syntax: INC destination
■ Example: INC WORD1
Before After
WORD1 0002 0003
DEC BYTE1
Before After
39
NEG
40
● Used to negate the contents of destination.
● Replace the contents by its 2’s complement.
● Syntax
NEG destination
● Example
NEG BX
Before After
BX 0002 FFFE
How?
40
41
TRANSLATION
Examples
42
● Consider instructions: MOV, ADD, SUB, INC, DEC, NEG
● A and B are two-word variables
● Translate statements into assembly language:
Statement Translation
B =A MOV AX, A
MOV B, AX
Statement Translation
43
44
PROGRAM STRUCTURE
Program Segments
45
● 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.
45
Memory Models
46
● Determines the size of data and code a program can have.
● Syntax:
.MODEL memory_model
Model Description
.DATA
WORD1 DW 2
BYTE1 DB 10h
47
Stack Segment
48
● For example:
.STACK 100h
48
Code Segment
49
.CODE name
49
Putting it Together!
50
ORG 0100h
.MODEL SMALL
.STACK 100h
.DATA
;data definition go here
.CODE
;instructions go here
50