Appendix C
Appendix C
Setup a Stack
.STACK 2000H
;
;
ah,prnt
dx,msg
21h
;or mov
dx,offset msg
Page 1
Page 2
05/29/00 08:36:36
Page 1 - 1
0000
= 0009
= 4C00
0000 48 65 6C 6C 6F 20
77 6F 72 6C 64
000B 0D 0A 0A 24
prnt equ
done equ
msg db
db
001C
001E
0022
0Dh,0Ah,0Ah,$
;
;
;
;
B8 ---- R
8E D8
B4 09
8D 16 0000 R
CD 21
mov
lea
int
;
;
0024
0027
9
4c00h
Hello world
;
;
0000
0017
001A
Setup a Stack
.STACK 2000H
B8 4C00
CD 21
ah,prnt
dx,msg
21h
;or mov
dx,offset msg
Page 3
09/16/00 11:49:32
Symbols 2 - 1
Size
Length
DGROUP . . . . . . . . . . . . .
GROUP
_DATA . . . . . . . . . . . . .
16 Bit
STACK . . . . . . . . . . . . .
16 Bit
_TEXT . . . . . . . . . . . . .
16 Bit
Microsoft (R) Macro Assembler Version 6.14.8444
Program Hello World by Andrew H. Andersen, Jr.
000F
2000
002D
Align
Combine Class
Symbols:
N a m e
@CodeSize
@DataSize
@Interface
@Model . .
@Startup .
@code . .
@data . .
@fardata?
@fardata .
@stack . .
done . . .
fini . . .
msg . . .
prnt . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Type
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Number
Number
Number
Number
L Near
Text
Text
Text
Text
Text
Number
L Near
Byte
0000
Number
Value
Attr
0000h
0000h
0000h
0002h
0000
_TEXT
_TEXT
DGROUP
FAR_BSS
FAR_DATA
DGROUP
4C00h
0024
_TEXT
_DATA
0009h
0 Warnings
0 Errors
In C, the Hello World program source code is much smaller, and might be like the following:
/*
#include
#include
char
*/
<stdio.h>
<string.h>
main()
{
printf(%s\n\n,msg);
return 0
}
This program would be called HELLO.C After compiling we will have an object file.
Although the source program length is shorter in C, the executable program created by a C compiler
may be over 12 times larger than the assembler version (over 8000 bytes vs. less than 600 bytes)
Page 4
( )
HT
;
:
MEANING
1 or more blanks
single quotes
parentheses
RETURN
TAB
semicolon
colon
USE
field separator or terminator field
enclose a data string
delimit an expression
statement terminator
field separator or symbol terminator
indicate start of a comment
delimiter for labels
NAME FIELD
The name is optional, and for some assemblers a colon must follow the label in the Code segment. A
label is a symbolic name used to refer to a memory address or value. The label may use any
alphanumeric character, and the first character of a label should be alphabetical. The student should use
labels of six characters or less excluding the colon. This is primarily to do with spacing of lines and
characters when printing the lst file. There are restrictions for label names. A label should not be the
same as a mnemonic. We may not use the label SUB for a subroutine label since SUB is the assembly
language mnemonic for subtract, but we could use the label SUB1. If we use a mnemonic or a single
character that is the same as a register, or any reserved characters, the Assembler will flag the
statement, and print a error message. If the assembler encounters a semicolon in the beginning of the
label field, it treats the entire line as a comment.
Page 5
*
/
NOT
AND
OR
XOR
EXAMPLE
LABEL+2
LABEL1
TOP*3
SIX/2
NOT TRUE
WQ AND SE
WQ OR SE
WQ XOR SE
EXPLANATION
Address of LABEL + 2 bytes
Address 1 byte before LABEL
Equates to the value TOP times 3
Equates to the value six divided by 2 or 3
the 1s complement of TRUE
returns the value of label WQ AND SE
returns the value of label WQ OR SE
returns the value of label WQ XOR SE
COMMENT FIELD
The comment field must begin with a semicolon and may continue only until the end of the line. If it
must continue on the next line, that line must begin with a semicolon. In the comments field the
programmer should explain what events are taking place with that instruction or group of instructions.
On line documentation is essential to enable the programmer or another individuals to determine what
is happening. No programs will be accepted without proper documentation.
Page 6
Page 7
DB
The Define Byte directive is used to define memory storage of data on an 8bit
boundary. Labels are optional, but an operand is required. Multiple operand values
may be entered in any acceptable format (numeric can be in binary, octal, decimal,
or hex) and is separated by commas. Strings are enclosed in quotes.
Unsigned integer values are in the range of 0 to 255
The following are examples of the DB directive:
VALUE
VAL2
BINUM
MSG
DB
DB
DB
DB
100
10H
01100111B
HELLO
NUM
DB
1,2,3,4
INIT
DB
DUP
The DUP operator, which is placed in the operand field, allows you to initialize
storage to one or more multiple values. The DUP operator used with the DB
directive is:
NUM1
DB 80 DUP (20h)
VAL2
DW
The Define Word directive is similar to the Define Byte, except that it is used to
define 16bit data (A word) instead of 8bit data. The syntax of the DW directive
is similar to DB except that it stores the defined data on a word boundary.
It is used to represent unsigned Integer values (0 to 65 535)
The DW format is:
NUM1
NUM2
CAL1
BLANK
BLANK
DW
DW
DW
DW
DW
13H
;DEFINE NUM1 AS THE VALUE 0013H
1000
;DEFINE NUM2 AS THE VALUE 1000
256 * 2
;ASSIGN THE VALUE 512 TO CAL1
80 DUP (?) ;RESERVE 80 WORDS UNINITIALIZED
80 DUP (20H) ;RESERVE 80 WORDS OF SPACES (0020H)
The Define DoubleWord directive is similar to DB and DW, except that it is used
to define 32bit data (A double word). The syntax of the DD directive is similar
to DB except that it stores the defined data on a double word boundary.
It is used to represent unsigned Integer values (0 to 4 294 967 295)
The DW format is:
NUM1
DD
13H
;DEFINE NUM1 AS THE VALUE 0000 0013H
DD
The Define QuadWord directive is similar to DB, except that it is used to define
64bit data.
Unsigned integer values range from 0 to 264 1 (~1.8e19)
The DW format is:
NUM1
DQ 13H
;DEFINE NUM1 AS 0000 0000 0000 0013H
DQ
Page 8
EQU
EQU
EQU
EQU
EQU
6
25
100H
SIX
SIX+2
Equal-Sign Directive
This is similar to the EQU, but is used as a redefinable equates. The value
=
associated with the = may be reassigned during assembly. Like the EQU
directive, it cannot change during program execution/
Examples are:
count = 25
.386
maxLongInt = 7FFF FFFFh (+2 147 486 647)
minLongInt = 8000 0000h (-2 147 486 648)
maxUnsignLongInt = 0 FFFF FFFFh (4 294 967 295)
The ORG directive is used to change the current address of a program to another
address. It should be used in the beginning of the program, and is usually the last
statement in the Equates Area. The addresses below 03FFH (4 groups of 256) are
used to store the RESET and RESTART (vectored interrupt) and user defined
vectored interrupt addresses. The programmer or operating system will usually
load these addresses but they are not used to store source code. The ORG
statement may be used at any point in the program, and may be used more than
once.
It could be used to define a hex lookup table for printing the ASCII value of
single hex digits that begins at a specific address.
.DATA
ORG 1000H
HEXTBL
DB
01234
DB
56789
DB
ABCDE
DB
F
ORG
END
The END assembler directive identifies the last line of the source code
program. Only one END statement may appear in a source code program as any
statements after the end statement are ignored by the assembler. The END
statement should be located in the OPCODE field, and labels and operands are
optional when using simplified segment directives.
Page 9
Page 10
The 80x86 has different segments for the Stack, Data, and Code. Each of
these three storage areas should be identified.
Defines the end of the Segment. This must occur before the END
directive
Page 11
Page 12
Page 13
Page 14
POINTS
SUM
TOTAL:
ADDUP:
.MODEL SMALL
.DATA
ORG
7000H
DB
16 DUP(?)
DB
?
.CODE
.STARTUP
MOV
AX,7000H
MOV
DS,AX
MOV
AL,0
MOV
BL,16
LEA
SI,POINTS
ADD
AL,[SI]
INC
SI
DEC
BL
JNZ
ADDUP
MOV
SUM,AL
MOV
AH,04CH
INT
21H
.EXIT
END
TOTAL
Page 15
05/20/00 08:37:41
Symbols 2 - 1
Size
Length
Align
Combine Class
GROUP
16 Bit
16 Bit
7011
0036
Word
Word
Public
Public
DATA
CODE
05/20/00 08:37:41
Symbols 3 - 1
Symbols:
N a m e
@CodeSize
@DataSize
@Interface
@Model . .
@Startup .
@code . .
@data . .
@fardata?
@fardata .
@stack . .
ADDUP . .
POINTS . .
SUM . . .
TOTAL . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Type
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Number
Number
Number
Number
L Near
Text
Text
Text
Text
Text
L Near
Byte
Byte
L Near
Value
0000h
0000h
0000h
0002h
0000
_TEXT
DGROUP
FAR_BSS
FAR_DATA
DGROUP
0024
7000
7010
0017
Attr
_TEXT
_TEXT
_DATA
_DATA
_TEXT
1 Warnings
0 Errors
Both versions of the program accomplish the same task. The second version allows the Assembler to
perform some housekeeping chores for us.
Let us briefly look at a sample line of output in the total.lst file contents and what it represents:
8000
B8 7000
TOTAL:
MOV
AX,7000H
Page 16
equ
equ
equ
equ
equ
prompt1
prompt2
get1st
get2nd
showans
0516h
0616h
0916h
0a16h
0b20h
db
db
db
db
db
.code
;
;
main:
mov
mov
ax,@data
ds,ax
Page 17
call
call
Clr
SetCur
fini:
mov
int
Clr
;
;
proc
Clear Screen with 25 line scroll.
The screen is blue and the text is yellow
Clr
mov
mov
mov
mov
mov
int
endp
ah,
al,
cx,
dx,
bh,
10h
6
0
0
1950h
1eh
SetCur
proc
;
Now set Cursor to row 5 column 30 on page 0
mov ah, 2
;The BIOS function
mov dx, 051eh ;row/column position
mov bh, 0
;video page no. 0
int 10h
;using Video BIOS
ret
SetCur
endp
.exit
end
Page 18