Lab 2 - Data Types
Lab 2 - Data Types
Character Constants
A character constant is a single character enclosed in single or double quotes. MASM stores the value in
memory as the character’s binary ASCII code. Examples are
'A'
"d"
String Constants
A string constant is a sequence of characters (including spaces) enclosed in single or double quotes:
'ABC'
'X'
"Good night, Gracie"
'4096'
Embedded quotes are permitted when used in the manner shown by the following examples:
Reserved Words
Reserved words have special meaning in MASM and can only be used in their correct context.
There are different types of reserved words:
• Instruction mnemonics, such as MOV, ADD, and MUL
• Register names
• Directives, which tell MASM how to assemble programs
• 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
Type Usage
BYTE 8-bit unsigned integer. B stands for byte
SBYTE 8-bit signed integer. S stands for signed
16-bit unsigned integer (can also be a Near pointer in real-
WORD
address mode)
SWORD 16-bit signed integer
32-bit unsigned integer (can also be a Near pointer in protected
DWORD
mode). D stands for double
SDWORD 32-bit signed integer. SD stands for signed double
FWORD 48-bit integer (Far pointer in protected mode)
QWORD 64-bit integer. Q stands for quad
TBYTE 80-bit (10-byte) integer. T stands for Ten-byte
REAL4 32-bit (4-byte) IEEE short real
REAL8 64-bit (8-byte) IEEE long real
REAL10 80-bit (10-byte) IEEE extended real
Defining BYTE and SBYTE Data
The BYTE (define byte) and SBYTE (define signed byte) directives allocate storage for one or more
unsigned or signed values. Each initializer must fit into 8 bits of storage. For example,
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
A question mark (?) initializer leaves the variable uninitialized, implying it will be assigned a value at
runtime:
value6 BYTE ?
The optional name is a label marking the variable’s offset from the beginning of its enclosing segment. For
example, if value1 is located at offset 0000 in the data segment and consumes 1 byte of storage, value2 is
automatically located at offset 0001:
Defining Strings
To define a string of characters, enclose them in single or double quotation marks. The most common
type of string ends with a null byte (containing 0). Called a null-terminated string, strings of this type are
used in many programming languages:
Each character uses a byte of storage. Strings are an exception to the rule that byte values must be
separated by commas. Without that exception, greeting1 would have to be defined as
which would be exceedingly tedious. A string can be divided between multiple lines without
having to supply a label for each line:
The hexadecimal codes 0Dh and 0Ah are alternately called CR/LF (carriage-return line-feed) or end-of-
line characters. When written to standard output, they move the cursor to the left column of the line
following the current line.
The line continuation character (\) concatenates two source code lines into a single statement. It must
be the last character on the line. The following statements are equivalent:
greeting1 BYTE "Welcome to the Encryption Demo program "
and
greeting1 \
BYTE "Welcome to the Encryption Demo program "
The DWORD can be used to declare a variable that contains the 32-bit offset of another variable.
Below, pVal contains the offset of val3:
1. Division Operation:
Mnemonics Syntax Working Example
div div op1 Divide value of eax register with op1 mov b, 5
and quotient goes to eax and mov eax, 10
remainder goes to edx. div b
10 will be divided by 5 and
quotient 2 will be stored in eax
and remainder 0 in edx.
Task 1: Ask user to enter two number and divide first number with second and print quotient and
remainder.
2. writestring:
This is used to print string on console. To do that, offset of string is copied to edx register. As a
string is a consecutive array of character stored in consecutive memory locations (bytes), we first
move offset of first byte to edx (mov edx, offset string; considering string a
variable) then the writestring will print all characters of strings.
Task 2: Declare a variable, initialize it with any string and print it on console.