Coal 3
Coal 3
Fundamentals
Muhammad Kariz Kamal
OUTLINES
•Basic Language Elements
•Defining Data
•Symbolic Constants
3.1 BASIC LANGUAGE
ELEMENTS
main PROC
main ENDP
AddTwo program
.data
sum DWORD 0
.code
xyz PROC
mov eax, 15+9
mov var1, ‘A’
add eax,16
mov sum,eax
ret
xyz ENDP
Integer Literals
•An integer literal (also known as an integer constant) is made up of
an optional leading sign, one or more digits, and an optional radix
character that indicates the number’s base:
[{+ | - }] digits [ radix ]
•Real Number Literals
[sign] integer.[integer][exponent]
Examples
• 2., +3.0, -44.2E+05, 26.E5
•Reserved words have special meaning and can only be used in their
correct context (not case sensitive).
• Instruction mnemonics, Register names, Directives
• Attributes, which provide size and usage information for variables and operands.
Examples are BYTE and WORD
• Operators, used in constant expressions
• Predefined symbols, such as @data, which return constant integer values at assembly
time.
•An identifier is a programmer-chosen name. It might identify a
variable, a constant, a procedure, or a code label.
•Rules:
• They may contain between 1 and 247 characters.
• They are not case sensitive.
• The first character must be a letter (A..Z, a..z), underscore (_), @ , ?, or $.
Subsequent
characters may also be digits.
• An identifier cannot be the same as an assembler reserved word.
JMP I1
•Labels act as place markers
• marks the address (offset) of code and data
Data label
count DWORD 100
Code label
target of jump and loop instructions
target:
mov ax, bx
add ax, bx
…
jmp target
•Instruction Mnemonics:
• .data
• .code
• .stack
DIRECTIVE VS
INSTRUCTION
myVar DWORD 26 ;DWORD directive tells the assembler to reserve
space in the program for a doubleword variable.
•Line 16 uses the END directive to mark the last line to be assembled
(end of program), and it identifies the program entry point (main).
• If you add any more lines to a program after the END directive, they will be ignored
by the
assembler.
ASSEMBLING, LINKING,
AND RUNNING
PROGRAMS
DEFINING DATA
•The assembler recognizes a basic set of intrinsic data types, which
describe types in terms of their size.
.data
value1 BYTE 'A'; character constant
value2 BYTE 0 ; smallest unsigned byte
value3 BYTE 255; largest unsigned byte
value4 SBYTE -128 ; smallest signed byte
value5 SBYTE +127 ; largest signed byte
value6 BYTE ? ; uninitialized byte
Offset Value
Multiple Initializers list 0000 10
0001 20
•If multiple initializers are used in the same data 0002 30
definition, its label refers only to the offset of the first 0003 40
initializer.
list BYTE 10,20,30,40
Within a single data definition, its initializers can use different radixes.
Character and string literals can be freely mixed. In the following example,
list1 and list2 have the same contents:
var4 10
0
var4 BYTE 10,3 DUP(0), 20 0
0
20
•Defining WORD and SWORD Data
•The WORD (define word) and SWORD (define signed word)
directives create storage for one or more 16-bit integers:
•All data types larger than a byte store their individual bytes in reverse
order. The least significant byte occurs at the first (lowest) memory
address.
.data
smallArray DWORD 10 DUP(0) ; 40 bytes
bigArray DWORD 5000 DUP(?) ; 20,000 bytes
3.5 SYMBOLIC
CONSTANTS
•A symbolic constant (or symbol definition) is created by associating an
identifier (a symbol) with an integer expression or some text.
•Symbols do not reserve storage.
•Equal-Sign Directive
name = expression
expression is a 32-bit integer (expression or constant)
may be redefined
name is called a symbolic constant
•Current Location Counter : the symbol $, is the current location counter,
that returns offset of current location:
selfPtr DWORD $
• This statement declares a variable named selfPtr and initializes it with the variable’s offset
value.
•Symbolic constant