Chapter 4.1 Introduction To Assembly Language
Chapter 4.1 Introduction To Assembly Language
3
Assembly language program must be translated to machine language for
the target processor.
The following diagram describes the steps from creating a source program
through executing the compiled program.
If the source code is modified, Steps 2 through 4 must be repeated.
Link
Library
Step 2: Step 3: Step 4:
Source assembler Object linker Executable OS loader
Output
File File File
Listing Map
Step 1: text editor File File
4
MASM
Microsoft : Macro Assembler
TASM
Borland : Turbo Assembler
NASM
Library General Public License (LGPL) [Free] :
Netwide Assembler
etc, Flat Assembler, SpAssembler
.model small
.stack 100h
.data
message db 'Hello World', 13, 10, '$'
.code
start:
mov ax, @data
mov ds, ax
mov dx, offset message ; copy address of message to dx
mov ah, 9h ; string output
int 21h ; display string
mov ax, 4c00h
int 21h
end start
6
TITLE PRGM1
.MODEL SMALL
.STACK 100H
.DATA
A DW 2
B DW 5
SUM DW ?
.CODE
MAIN PROC
; initialize DS
MOV AX, @DATA
MOV DS, AX
; add the numbers
MOV AX, A
ADD AX, B
MOV SUM, AX
; exit to DOS
MOV AX, 4C00H
INT 21H
MAIN ENDP
END MAIN
An instruction is a statement that becomes
executable when a program is assembled.
Assembled into machine code by assembler
An instruction contains:
Label (optional)
Mnemonic (required)
Operand (depends on the instruction)
Comment (optional)
Basic syntax
[label:] mnemonic [operands] [ ;comment]
Act as place markers
marks the address (offset) of code and data
Follow identifier rules
Data label
must be unique
example: my Array (not followed by colon)
count DWORD 100
Code label
target of jump and loop instructions
example: L1: (followed by colon)
target:
Mov ax, bx
…
jmp target
10
Instruction Mnemonics
memory aid
examples: MOV, ADD, SUB, MUL, INC, DEC
Operands
constant 96
constant expression 2+4
register ax
memory (data label) count
12
Comments are good!
explain the program's purpose
when it was written, and by
whom
revision information
tricky coding techniques
application-specific explanations
Single-line comments
begin with semicolon (;)
.model small
.stack 100h
.data
message db 'Hello World', 13, 10, '$'
.code
start:
Mov ax, @data
Mov ds, ax
Mov dx, offset message ; copy address of message to dx
Mov ah, 9h ; string output
int 21h ; display string
Mov ax, 4c00h
int 21h
end start
Commands that are recognized and acted
upon by the assembler
Not part of the Intel instruction set
Used to declare code, data areas, select
memory model, declare procedures, etc.
not case sensitive
Different assemblers have different
directives
NASM not the same as MASM, for example
value1 BYTE 10
K DB 5, 3, -1 allocates 3 bytes 03
FF
Offset Value
0000 10
list1
0001 20
I DW 4
J FE
FF
J DW -2
K BC
1A
K DW 1ABCH
L 31
30
L DW “01”
Enclose character in single or double quotes
'A', "x"
ASCII character = 1 byte
Enclose strings in single or double quotes
"ABC"
'xyz'
Each character occupies a single byte
Embedded quotes:
'Say "Goodnight," Gracie'
A string is implemented as an array of characters
For convenience, it is usually enclosed in quotation marks
It often will be null-terminated (ending with ,0)
Examples:
Idea: Define all strings used by your program in the same area of
the data segment.
31
32
CPU communicates with peripherals through I/O
registers called I/O ports.
Two instructions access I/O ports directly: IN and
OUT.
Used when fast I/O is essential, e.g. games.
Most programs do not use IN/OUT instructions
port addresses vary among computer models
much easier to program I/O with service routines
provided by manufacturer
Two categories of I/O service routines
Basic input/output system (BIOS) routines
Disk operating system (DOS) routines
DOS and BIOS routines invoked by INT (interrupt)
instruction.
A set of programs always present in system
BIOS routines most primitive in a computer
Talks directly to system hardware
Hardware specific - must know exact port
address and control bit configuration for I/O
devices
BIOS supplied by computer manufacturer and
resides in ROM
Provides services to O.S. or application
Enables O.S. to be written to a standard
interface
System Hardware
Non-standard interface
BIOS
Standard interface
Operating System
Standard interface
Application Program
INT21H used to invoke a large number of
DOS function.
Type of called function specified by putting a
number in AH register.
AH=1 single-key input with echo
AH=2 single-character output
AH=9 character string output
AH=8 single-key input without echo
AH=0Ah character string input
37
Input: AH=2, DL= ASCII code of character to be
output
Output: AL=ASCII code of character
To display a character
MOV AH, 2
MOV DL, ‘?’ ; displaying character ‘?’
INT 21H
To read a character and display it
MOV AH, 1
INT 21H
MOV AH, 2
MOV DL, AL
INT 21H
39
Input:AH=1
Output: AL= ASCII code if character key is
pressed, otherwise 0.
To input character with echo:
MOV AH, 1
INT 21H ; read character will be in AL register
To input a character without echo:
MOV AH, 8
INT 21H ; read character will be in AL register
.model small
.stack 100h
.data
message db 'Hello World', 13, 10, '$'
.code
start:
Mov ax, @data
Mov ds, ax
Mov dx, offset message ; copy address of message to dx
Mov ah, 9h ; string output
int 21h ; display string
Mov ax, 4c00h
int 21h
end start
41
Input: AH=9, DX= offset address of a string.
String must end with a ‘$’ character.
To display the message Hello!
MSG DB “Hello!$”
MOV AH, 9
MOV DX, offset MSG
INT 21H
OFFSET operator returns the address of a
variable
The instruction LEA (load effective address)
loads destination with address of source
LEA DX, MSG
Prompt the user to enter a lowercase letter,
and on next line displays another message
with letter in uppercase.
Enter a lowercase letter: a
In upper case it is: A
.DATA
CR EQU 0DH
LF EQU 0AH
MSG1 DB ‘Enter a lower case letter: $’
MSG2 DB CR, LF, ‘In upper case it is: ‘
Char DB ?, ‘$’
.CODE
.STARTUP ; initialize data segment
LEA DX, MSG1 ; display first message
MOV AH, 9
INT 21H
MOV AH, 1 ; read character
INT 21H
SUB AL, 20H ; convert it to upper case
MOV CHAR, AL ; and store it
LEA DX, MSG2 ; display second message and
MOV AH, 9 ; uppercase letter
INT 21H
.EXIT ; return to DOS
.DATA
String1 DB “Hello”
String2 DB 5 dup(?)
.CODE
MOV AX, @DATA
MOV DS, AX
MOV ES, AX
CLD
MOV CX, 5
LEA SI, String1
LEA DI, String2
REP MOVSB
46