Assembly
Assembly
DOSSEG
.MODEL SMALL
.CODE
start: mov ah,8
int 21h
cmp al,'a'
jl donothing ASCII CHAR
cmp al,'z' .
.
jg donothing
.
sub al,20h 30 0
donothing: mov dl,al 31 1
mov ah,2 32 2
int 21h 33 3
34 4
jmp start 35 5
END 36 6
37 7
38 8
Comments about ASCII code 39 9
.
To convert from upper case to lower case add 20h .
.
To convert from lower case to upper case subtract 20h 41 A
42 B
To convert a hexadecimal digit (x) to its ASCII code (y): 43 C
44 D
x+30h if 0<x<9 45 E
46 F
y = x+37h if A<x<F
.
ERROR else .
To recover a hexadecimal digit (x) from its ASCII code (y): 61 a
62 b
y-30h if ’0’<y<’9’ 63 c
x = y-37h if ’A’<y<’F’ 64 d
y-57h if ’a’<y<’f’ 65 e
66 f
ERROR else
.
.
Registers of the 8086 µp
Flag reg. F 16-bit registers
AX ah al Accumulator
BX Pointer (DS)
General CX Counter
purpose DX I/O
registers SI Source Index Pointer(DS)
DI Dest. Index Pointer (DS,ES)
BP Pointer (SS)
SP Stack Pointer
Inst. poin. IP Instruction pointer
CS Code Segment
Segment DS Data Segment
registers ES Extra Segment
SS Stack Segment
O D I T S Z A P C
C : Carry flag
P : Parity flag (Parity of lower 8 bits of last result generated)
A : Auxiliary carry flag ( BCD operations)
Z : Zero flag
S : Sign flag
T : Trap flag ( debugging software)
I : Interrupt flag
D : Direction flag (direction of string instructions)
O : Overflow flag
AX Register: (Accumulator)
⊗ Most efficient register used in arithmetic, logic, and data movement operations
⊗ Always involved in multiplication and division
⊗ Lower and upper parts can be accessed as Al and AH, respectively
mov al,62
mov dx,1000
out dx,al ; output the content of al to the output port whose
; address is in dx
SI, DI, and BP Registers: Memory pointers ( to be explained later).
SA 3AFF SB 3AFF0
EA 0002 EA + 0002
PA 3AFF2
00000
00001
00002
BX
SI,
DI CS:[0000]
CS:[0001]
CS:[0002]
CS beginning
BP
Code Segment
SP
IP
Invalid Instructions
DOSSEG
.MODEL SMALL
.DATA
msg DB ‘Hello, EE411’,13,10,‘$’
.CODE
mov ax,@DATA
mov ds,ax
mov ah,9
mov dx, offset msg ;lea dx , msg
int 21h
mov ah,4Ch
int 21h
END
step 2:Assemble your source code program to generate the object module.
HELLO.OBJ:
TASM hello.asm
step 3:Link the object file to generate the executable program HELLO.EXE:
TLINK hello.obj
step 4:Execute the program by writing hello at the DOS prompt. You will get the
message:
Hello, EE411
Notes:
When you assembled HELLO.ASM, Turbo Assembler turned the text instructions in
HELLO.ASM into their binary equivalents in the object file HELLO.OBJ which is
an intermediate file partway between source code and an executable file.
When you linked HELLO.OBJ, TLINK converted it into the executable file
HELLO.EXE and also generated a redundant file called HELLO.MAP which
contains information about the memory usage.
Debugging your programs using TD.EXE
DOSSEG
.MODEL SMALL
.DATA
msg DB ‘Hello, EE411’,13,10,‘$’
.CODE
mov ax,@DATA
mov ds,ax
mov ah,9
mov dx,offset msg
int 21h
mov ah,4Ch
int 21h
END
File View Run Breakpoints Data Window Options READY
CPU 80386 ----------------------------------------------------------------1
cs:0000>B8BF4B mov ax,4BBF ¦ ax 0000 c=0
cs:0003 8ED8 mov ds,ax ¦ bx 0000 z=0
cs:0005 B409 mov ah,09 ¦ cx 0000 s=0
cs:0007 BA0000 mov dx,0000 ¦ dx 0000 o=0
cs:000A CD21 int 21 ¦ si 0000 p=0
cs:000C 244C mov al,4C ¦ di 0000 a=0
cs:000E CD21 int 21 ¦ bp 0000 i=1
cs:0010 48 dec ax ¦ sp FFFE d=0
cs:0011 656C insb gs: ¦ ds 4BAE
cs:0013 6C insb ¦ es 4BAE
cs:0014 6F outsw ¦ ss 4BBE
cs:0015 2C20 sub al,20 ¦ cs 4BBE
cs:0017 45 inc bp ¦ ip 0000
cs:0018 45 inc bp ¦
cs:0019 3431 xor al,31 ¦
CS:0000 B4
CS:0001 02
CS:0002 B2
This program prints CS:0003 20
CS:0004 CD
ASCII characters CS:0005 21
32-127 onto screen ! CS:0006 FE
CS:0007 C2
CS:0008 80
CS:0009 FA
CS:000A 7F
CS:000B 75
CS:000C F7
• Avoid using reserved words (AX, INT,...) for identifiers (labels &
names).
mov ax,12A3h ; OK
mov ax,1237d ; OK
mov ax,10110b ; OK
mov ax,10110 ; OK
mov ax,E2A8h ; wrong
mov ax,0E2A8h ; OK
tiny, Both code and data are in the same 64kB segment (used for .com
programs)
small, All data in 1 segment and all code in 1 segment
• DB, DW, DD, and DUP are used to allocate memory locations
msg2 DW ’Hello’
; wrong, only DB can be used for strings of any
length.
msg4 DW 24h ; inetialize 2 memory locations with data 24h (lsb) and
; 00(msb).The PA of the first byte is assigned to msg4.
CS beginning
CS:0000 EB
CS:0001 05
DOSSEG msg6
CS:0002 90
CS:0003 8A
.MODEL small CS:0004 00
.DATA CS:0005 00
msg0 DB 24h CS:0006 00
CS:0007 B4
msg1 DB ‘Hello, EE411’,13,10,‘$’ CS:0008 1A
msg4 DW 24h CS:0009
msg5 DW 1A2Bh,’H’,’e’,’l’,’l’,’o’
.CODE
jmp start msg0 DS:0000 24 DS beginning
msg6 DD 8Ah msg1 DS:0001 48
start: mov ah,1Ah DS:0002 65
DS:0003 6C
. DS:0004 6C
. DS:0005 6F
. DS:0006 2C
DS:0007 20
mov ah,4ch DS:0008 45
int 21h DS:0009 45
END DS:000A 34
DS:000B 31
DS:000C 31
DS:000D 0D
DS:000E 0A
msg4 DS:000F 24
DS:0010 24
msg5
DS:0011 00
DS:0012 2B
DS:0013 1A
DS:0014 48
DS:0015 00
DS:0016 65
DS:0017 00
DS:0018 6C
DS:0019 00
DS:001A 6C
DS:001B 00
DS:001C 6F
DS:001D 00
Revision of Addressing modes
DOSSEG
.MODEL SMALL
.DATA
n1 dw 5555h
n2 db 88h
n3 dd 11h
.CODE
mov ax,@data
mov ds,ax
jmp start
n4 dw 7777h
start: mov al,77h ; immediate operand AM
mov di,offset start ; immediate operand AM
mov si,offset n4 ; immediate operand AM
mov cx,offset n1 ; immediate operand AM
mov ds,@data ; wrong(1)
mov ax,@data ; immediate operand AM
A program example
; This program illustrates the use of mov and xchg
; instructions.
DOSSEG
.MODEL SMALL
.DATA
n1 dw 5555h
n2 dw 7777h
.CODE
mov ax,@data
mov ds,ax
mov ah,4ch
int 21h
Notes
• Store a sample assembly program on disk, and for each new program that you
want to create, COPY the sample program into a file with its correct name, and
use your editor to complete the additional instructions.
• AS you will repeat the steps (TASM fn.asm, TLINK fn.obj, fn)
frequently, it is recommended to prepare a patch file in your disk, call it
asm.bat, in which you write
TASM %1
TLINK %1
del *.map
del *.obj
%1
• A header is to be written at the beginning of your programs, which contains
name, surname, ID no, and the name (ore description) of the program.
• Programs must be well committed. It is not necessary to write a comment for
each line but any block having a particular function must be thoroughly
explained.
• Programs must be indented as much as the longest identifier.