Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 10

Page # 1

Basic Turbo Assembler program Structure


TITLE <program name>
.MODEL SMALL
.STACK 100h
.DATA
::
<var_name1> db <value>
<var_name2> db <value like 15 dup (‘ ’)>
A. Simplified segment directives <var_name3> dw <value>
(.exe program format) ::
.CODE
BEGIN: mov ax, @data ;initialize ds and es
mov ds, ax
mov es, ax
::
::
mov ah, 4ch ;code to terminate prog
int 21h
END BEGIN

TITLE <program name>


SSEG SEGMENT PARA STACK ‘STACK’
db 100h dup(?)
SSEG ENDS

DSEG SEGMENT WORD ‘DATA’


::
<var_name1> db <value>
<var_name2> db <value like 15 dup (‘ ’)>
<var_name3> dw <value>
:: B. Standard Segment directive
DSEG ENDS (.exe program format)
CSEG SEGMENT WORD ‘CODE’
ASSUME SS:SSEG,DS:DSEG,ES:DSEG,CS:CSEG
BEGIN: mov ax, @data ;initialize ds, and es
mov ds, ax
mov es, ax
::
::
mov ah, 4ch ;code to terminate prog
int 21h
CSEG ENDS
END BEGIN

TITLE <program name>


.MODEL TINY
.DATA
::
<var_name1> db <value>
<var_name2> db <value like 15 dup (‘ ’)>
C. Simplified segment directives <var_name3> dw <value>
(.com program format) ::
.CODE
ORG 100h
BEGIN:
::
::
mov ah, 4ch
int 21h
END BEGIN
Page # 2

TITLE <program name>

CSEG SEGMENT WORD ‘CODE’


ASSUME SS:SSEG,DS:DSEG,ES:DSEG,CS:CSEG
ORG 100h
BEGIN:
D. Standard Segment directive ::
(.com program format) ::
mov ah, 4ch ;code to terminate prog
int 21h
::
<var_name1> db <value>
<var_name2> db <value like 15 dup (‘ ’)>
<var_name3> dw <value>
::
CSEG ENDS
END BEGIN

1. TITLE – set title in listing file. This directive is optional


2. .MODEL – a directive that specifies the memory model of an assembler module.
There are six modules:
a. TINY – both code and data reside within the same 64-Kbyte segment.
b. SMALL – code and data reside on separate 64-Kbyte segments.
c. MEDIUM – code maybe larger than 64-Kbyte, but data resides within a single
64-Kbyte segment
d. COMPACT – code resides within a single 64-Kbyte segment, but data may be
larger than 64-Kbyte. Note that no single data array may be greater than 64-
Kbyte.
e. LARGE – both code and data may be larger than 64-Kbyte, but no single data
array may be greater than 64-Kbyte.
f. HUGE – both code and data may be larger than 64-Kbyte and data arrays may
be larger than 64-Kbyte.
3. .STACK – defines the stack segment. This directive controls the size of the stack.
4. .DATA – marks the start of the data segment.
5. .CODE – marks the start of the program’s code segment
6. END – marks the end of the program’s source code. Any lines following an END directive are
ignored by the Turbo Assembler. The optional label after END tells the CPU where
execution should begin when the program is run.
7. @data – explicitly set DS to the segment you want, which is equivalent to the segment that
starts with .DATA
8. ORG – sets the Instruction Pointer (IP) in the current segment.
9. SEGMENT – defines the start of a segment
10. ENDS – defines the end of the segment.
11. ASSUME – directive that tells the Turbo Assembler what segment a given segment register
is currently set to.
Page # 3

Steps involved in writing an 8086 assembly language:


a. Create a new program using any editor (eg. SKN, WordPad). You are producing an
Assembler Source code File. [ example: EXERCISE1.ASM ]

b. Assemble the Assembler Source Code file. It will generate as output an Object File.
[ example: EXERCISE1.OBJ ]
To assemble, use the following command:
TASM <source file>
Example:
TASM EXERCISE1
c. Link the Object File. It will generate as an output an Executable file.
[ example: EXERCISE1.EXE ]
To link, use the following command:
TLINK <object file>
Example:
TLINK EXERCISE1
d. Execute the program by just typing the filename.
[ example: EXERCISE1]
e. If your source code uses a COM program format, do steps A to C. Notice that when you link
the object file, a warning message is display: “Warning: No stack”. Then convert the
generated exe file into com file.
To convert exe file into com file:
EXE2BIN <exe source file> <com destination file>
Example:
EXE2BIN EXERCISE1 EXER1

Format of an Assembly Language Source Code line:


[ label ] < instruction/directive > [ operand ] [ ;comment ]
where:
label - is an optional symbolic name
instruction/directive – either the mnemonic for an instruction or a directive
operand – contains a combination of zero, one or two (sometimes more) constants, memory
references, register references and text strings, as required by the particular instruction or
directive.
; comment – optional comment
Page # 4

Useful Routines in Assembly Language


1. Clear the Screen (clrscr() in C language)
mov ah, 00h ; set video mode
mov al, 03h ; video mode 03 = 80 x 25
int 10h
same as writing: mov ax, 0003H
int 10h

2. Clearing some portion on screen


mov ax,0600h ; AH 06 (scroll), AL 00 (full screen)
mov bh,07 ; Attribute: white (7) on black (0)
mov ch,<row> ; Upper left row
mov cl,<col> ; Upper left column mov ax,0606h
mov bh, 14
mov dh,<row> ; Lower right row
mov cx,0000h
mov dl, <col> ; Lower right column mov dh,5
int 10h mov dl,20
Example: Clears positions <0,0> to <5, 20> int 10h
with textcolor Red (4) on b/g Blue (1).

3. Set Cursor Position (gotoxy(<col>, <row>) in C language)


mov ah, 02h ; set cursor position
mov bh, 00 ; display page 0
mov ah, <row> ; indicate row position
mov al, <col> ; indicate column position
int 10h
mov ah, 02h
Example: gotoxy(12,5); mov bh, 00
mov dh, 05
mov dl, 12
int 10h
4. Terminate Program
mov ah, 4ch ; exit
Int 21h
Or, you can write: mov ah, 0 ; program terminate
int 21h

5. Original DOS for Screen Display (printf() in C language)


<varname> db ‘<string> $’ ; declare string in DATA segment and a ‘$’
...
mov ah, 09h ; Request display
lea dx, <varname> ; Load address of the string
int 21h
example: printf(“Enter your name :”);

msg db ‘Enter your name: $’


...
mov ah, 09h
lea dx, msg
Page # 5

int 21h
Note: It does not display “$” and special control characters like
hex 07 (beep), 08 (backspace), 10 (carriage return ), 13 (first column)

6. Write one Character ( putchar() in C language )


mov ah, 02h ; print one character
mov dl, ‘<char>’
int 21h mov ah, 02h
mov dl,’A’
Example: putchar (‘A’); int 21h

mov ah, 09 ; print one character


mov al, ‘<char>’ ; write the character to display
mov bh, 00 ; page number 00
mov bl,<color> ; color attribute like red on blue (14h)
mov cx, <rep> ; number of times that the char is to be displayed
int 10h mov ah, 09
mov al, ‘*’
Example: mov bh, 00
mov bl, 0DAh
mov cx, 05
int 10h

7. Read one character with echo ( getche( ) )


mov ah, 01 ; character input with echo
; character entered by user will be stored in AL = 8-bit character
int 21h
mov ah, 01
Example: X = getche(); int 21h ; al = X

8. Read one character without echo ( getch( ) )


mov ah, 07 ; character input without echo
; character entered by user will be stored in AL = 8-bit character
int 21h
mov ah, 07
Example: X = getch(); int 21h ; al = X

9. Extended DOS for Screen display


mov ah, 40h ; Display .data
mov bx, 01 ; Output code (01) Stud db “Juan dela Cruz III”
mov cx, <length> ; # of characters to display .code
lea dx, <var> ; string variable :: ::
int 21h mov ah, 40h
mov bx, 01
mov cx, 4
Example:
lea dx, Stud
int 21h
Page # 6

10. Original DOS for Keyboard Input ( scanf(“%s”, varname )


.data
<var> label byte ; start of parameter list
vmax db 20 ; specify the max length
vlen db ? ; actual # of characters inputted
vstring db 20 dup(‘ ‘) ;string buffer

.code
:: ::
mov ah, 0Ah ; Input service
lea dx, <var> ; write the variable name
int 21h

11. Extended DOS for keyboard input


.data
<var> db 20 dup(“ “) ; Input area
.code
:: ::
mov ah, 3Fh ; Request input
mov bx, 00 ; Input code (00)
mov cx, <length> ; write max character length
lea dx, <var> ; input area
int 21h

 Helpful Codes in display:


ASCII Hex
Carriage Return 13 0Dh
Line feed 10 0Ah
Tab 09 09h

Exercises:
1. What is the hex value for the bottom rightmost location on an 80-column screen?
2. Code the instruction to set the cursor to row 12, column 8.
3. Code the instruction to clear the screen beginning at row 12, column 0, through row 22,
column 79 with text color cyan (3) on background green (2).
4. Code data items and instructions to display a message “What is the date (mm/dd/yy)?”.
Follow the message with a beep sound. Use (a) original DOS services and (b) extended
DOS services with file handles.
5. Code data items and instructions to accept data from the keyboard according to the format in
problem 4. Use (a) original DOS and (b) extended DOS
Page # 7

DOS DEBUG Command Summary:


Type the program debug at DOS prompt

1. Register – displays the hexadecimal contents of all the registers, plus the alphabetic flag
settings, and the next instruction to be executed.
- displays the hexadecimal contents of a single register with the option of changing those
contents.
Syntax:
-R or – R [registername]

Flag settings:
Flag name Set Clear
Overflow (Yes/No) OV NV
Direction (dec/inc) DN UP
Interrupt (enable/disable) EI DI
Sign (negative/positive) NG PL
Zero (yes/no) ZR NZ
Auxiliary carry (yes/no) AC NA
Parity (even/odd) PE PO
Carry (yes/no) CY NC

You can also change directly the content of the register (in hexadecimal) by typing:
- R <registername>
<registername> <value>
: <input value>

Sample source code:

To debug the sample program, at DOS prompt type:


DEBUG sample1.exe
Page # 8

2. Trace – executes one or more instructions starting with the instruction at CS:IP or at =
address if it is specified. The = must be entered. One instruction is assumed, but you
can specify more than one with value. It also displays the contents of all registers and
flags after each instruction executes.
Syntax:
- T [=address] [value]
Example:
- T = 0000 2  this will trace and display 2 instructions starting after 0000

3. Unassemble – translates the contents of memory into assembler-like statement and displays
their addresses and hexadecimal values, together with assembler-like statements. If
range is not specified, it will display the first 32 bytes.
Syntax:
- U [address] or - U [range]
Example:
- U 0000 or - U 0000 000E

4. Dump – displays the contents of a portion of memory. If range is not given, it will dump 128
bytes of data.

Syntax:
- D [address] or D [range]

5. Assemble – To assemble IBM Personal Computer Macro Assembler language statements


directly into memory.

Syntax:
- A [address]

Example:
-a
Page # 9

24A9:0000 mov ax,20

Interesting memory locations to view:


 BIOS in ROM – located at FFFF[0]
o BIOS checks the various ports to identify and initialize devices that are attached.
o Creates an Interrupt service/vector table (IST/IVT) that contains addresses for interrupts
that occur. Found in RAM location 0000[0] except for advanced processors.
o Creates Data table area (DTA) beginning at location 40[0].
o Locates OS by accessing the bootstrap loader from disk.
o OS takes over the CPU control
 Checking Memory Size - located at ROM BIOS data area at 40:13 (or actual address 413)
and 40:14 (414).
Use - D 40:13 at debug program to view memory size
Reversed Hex Corrected Hex Decimal (K)
00 01 01 00 256
80 01 01 80 384
00 02 02 00 512
80 02 02 80 640

 Checking Serial Number – embedded in ROM at location hex FE000


Use - D FE00:0 at debug program
o The screen displays a seven-digit number followed by a copyright date.

 Checking ROM BIOS Date – begins at location FFFF5, recorded as mm/dd/yy


Use - D FFFF:0005 at debug program

 Checking Copyright Notice – is at hex location FFFF:000E.


Code Model
FF IBM PC (’81, ’82)
FE PC-XT (’82), portable (’82)
FD PCjr
FC PC-AT(’82), PC-XT model 286, PS/2 models 50 and 60
FB PC-XT (’86)
FA PS/2 model 30
F9 PC convertible
Page # 10

F8 PS/2 models 70 and 80


 Check for Scroll, Num and Caps Lock – located at 40:17h
10 – if scroll lock has pressed
20 – IF Num lock has pressed
40 – if caps lock has pressed

6. Enter – has modes of operation:


1. Replaces the contents of one or more bytes starting at the specified address with
the values contained in the list.
2. Displays and allows modification of bytes in a sequential manner.

Syntax:
- E address [list]

Example: Machine Assembly


Instructions Commands

B82301 mov ax, 0123


052500 add ax,0025
8BD8 mov bx, ax
03D8 mov bx,ax
8BCB mov cx,bx
2BC8 sub cx, ax
- E CS:100 B8 23 01 05 25 00 ; writes the first 2 commands at CS:100
- E CS:106 8B D8 03 D8 8B CB ;writes the next 3 commands at CS:106

7. Fill – fills the memory locations in the range with the values in the list. If range is not given, it
will fill up 128 bytes of data.
Syntax:
- F range list or - F address list

8. Go – executes the program you are debugging. It stops the execution when the instruction at
a specified address is reached (breakpoint), and displays the registers, flags and the
next instruction to be executed.
Syntax:
- G [=address] [address [address…] ]

9. Hexarithmetic – adds the two hexadecimal values, then subtracts the second from the first.
Syntax:
- H value1 value2
10. Proceed – Causes the execution of a subroutine call, a loop instruction, an interrupt or a
repeat string instruction to stop at the next instruction.
Syntax:
- P [address] [value]

11. Quit – ends the debug program


Syntax:
-Q

You might also like