Sie sind auf Seite 1von 7

Lab 2 – First Assembly Language Program

Objectives
Know how to represent integer constants, expressions, real number constants, character constants, and string
constants in assembly language, know how to formulate assembly language instructions, using valid syntax be able
to code, assemble, and execute a program that adds and subtracts integers

Description
Basic Language Elements
An assembly language is a low-level language for programming computers; it is developed to make programming
easier than programming using machine language. Assembly language is a set of mnemonics (symbols) for machine
code instructions and other features that make programming easier.
[Label:] mnemonic [operands] [; comment]
Label: allows the program to refer to a line of code by a name. (Actually, it is a name for the address of the
machine code of this line.)
Mnemonic and operands: together perform the real work of the program.
Mnemonic can be an instruction (like MOV, ADD) or directive (like BYTE, END).
Comment: is any set of words preceded by a semi‐colon.
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 ]

h hexadecimal r encoded real


q/o octal t decimal (alternate)
d decimal y binary (alternate)
b binary

A hexadecimal literal beginning with a letter must have a leading zero to prevent the assembler from interpreting
it as an identifier.
Constant Integer Expressions
A constant integer expression is a mathematical expression involving integer literals and arithmetic Operators.
Each expression must evaluate to an integer, which can be stored in 32 bits (0 through FFFFFFFFh).

Operator Name Precedence Level


() Parentheses 1
+,- Unary plus, minus 2
*, / Multiply, divide 3
MOD Modulus 3
Real Number Literals
Real number literals (also known as floating-point literals) are represented as either decimal reals or encoded
(hexadecimal) reals. A decimal real contains an optional sign followed by an integer, a decimal point, an optional
integer that expresses a fraction, and an optional exponent:
[sign]integer.[integer][exponent]
Character Literals
A character literal is a single character enclosed in single or double quotes. The assembler stores the value in
memory as the character’s binary ASCII code. Examples are
'A'
"d"

String Literals
A string literal is a sequence of characters (including spaces) enclosed in single or double quotes:
'ABC'
'X'
"Good night, Gracie"
'4096'

Reserved Words
Reserved words have special meaning and can only be used in their correct context. Reserved works, by default,
are not case-sensitive. For example, MOV is the same as mov and Mov. There are different types of reserved words
(Instruction mnemonics, Register names, Directives, Attributes, Operators, Predefined symbols)
Identifiers
An identifier is a programmer-chosen name. It might identify a variable, a constant, a procedure, or a code label.
There are a few rules on how they can be formed:
 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 $.
 An identifier cannot be the same as an assembler reserved word.

Directives
A directive is a command embedded in the source code that is recognized and acted upon by the assembler.
Directives do not execute at runtime, but they let you define variables, macros, and procedures. They can assign
names to memory segments and perform many other housekeeping tasks related to the assembler.
Instruction
An instruction is a statement that becomes executable when a program is assembled. Instructions are translated
by the assembler into machine language bytes, which are loaded and executed by the CPU at runtime. An
instruction contains four basic parts:
 Label (optional)
 Instruction mnemonic (required)
 Operand(s) (usually required)
 Comment (optional)
This is how the different parts are arranged:
[label:] mnemonic [operands] [;comment]

Label
A label is an identifier that acts as a place marker for instructions and data. A label placed just before an instruction
implies the instruction’s address. Similarly, a label placed just before a variable implies the variable’s address.
There are two types of labels: Data labels and Code labels.
A data label identifies the location of a variable, providing a convenient way to reference the variable in code.
Code labels are used as targets of jumping and looping instructions. A label in the code area of a program (where
instructions are located) must end with a colon (:) character.
Operands
An operand is a value that is used for input or output for an instruction. Assembly language instructions can have
between zero and three operands, each of which can be a register, memory operand, integer expression, or input–
output port.
Comments
Comments are an important way for the writer of a program to communicate information about the program’s
design to a person reading the source code.
Comments can be specified in two ways:
• Single-line comments, beginning with a semicolon character (;).
• Block comments, beginning with the COMMENT directive and a user-specified symbol. All subsequent lines of
text are ignored by the assembler until the same user-specified symbol appears.

DUP Operator
The DUP operator allocates storage for multiple data items, using a integer expression as a counter

The NOP (No Operation) Instruction


The safest (and the most useless) instruction is NOP (no operation). It takes up 1 byte of program storage and
doesn’t do any work

General Program Template


To see how this works, let us look at a simple assembly language program that adds two numbers and saves the
result in a register. We will call it the AddTwo program:

4: .386
5: .model flat,stdcall
6: .stack 4096
7: ExitProcess PROTO, dwExitCode:DWORD
8:
9: .code
10: main PROC
11: mov eax,5 ; move 5 to the eax register
12: add eax,6 ; add 6 to the eax register
13 :
14: INVOKE ExitProcess,0
15: main ENDP

Let us go through the program one line at a time:

Line 4 .386 an assembler directive, which identifies this as a 32-bit


Line 5 MODEL selects the program’s memory model
Line 6 .stack directive defines the stack segment and its size in bytes.
Line 7 declares a prototype for the ExitProcess function, which is a standard Windows service.
Line 8 A prototype consists of the function name, the PROTO keyword, a comma, and a list of input parameters.
The input parameter for ExitProcess is named dwExitCode
Line 9 .code directive defines the code segment where assembly instructions are placed.
Line 10 starts the main procedure, the entry point for the program.
Line 11 places the integer 5 in the eax register.
Line 12 adds 6 to the value in EAX, giving it a new value of 11.
Line 14 calls a Windows service named ExitProcess that halts the program
Line 15 is the ending marker of the main procedure
Listing File
A listing file contains a copy of the program’s source code, with line numbers, the numeric address of each
instruction, the machine code bytes of each instruction (in hexadecimal), and a symbol table. The symbol table
contains the names of all program identifiers, segments, and related information.
Configuring Visual Studio to generate a listing file:
To configuring Visual Studio to generate a listing file, you need to open the project properties, and from Microsoft
Marco Assembler tab, select Listing File and modify the values as shown in the below image (You will find the .lst
file in the project directory)

Map File
The map file contains all the static symbols (functions, globals, etc.) in your code and their relative addresses.
Depending on your linker settings, it may have other information, such as the address of each line of code in your
program. To configuring Visual Studio to generate a Map file, you need to open the project properties, and from
Linker tab, Debugging tab, select Generate Map File, then build your project, the Map file will be available in the
project directory
Lab Work
Write your First program:
1. The following program demonstrates integer data definition under the .DATA section

; Defining Data
.386
.model flat,stdcall
.stack 4096
ExitProcess proto,dwExitCode:dword
.data
; ----------------- Byte Values ---------------------
byte1 BYTE 'C' ; 'C' = 67 = 43h
byte2 BYTE 0 ; smallest unsigned byte value
byte3 BYTE 255 ; largest unsigned byte value
byte4 SBYTE -128 ; smallest signed byte value
byte5 SBYTE +127 ; largest signed byte value ;
byte6 BYTE ? ; uninitialized
; ----------------- Word Values ---------------------
word1 WORD 12535
word2 SWORD -2761
word3 WORD ? ; uninitialized
word4 WORD 4 DUP (0fh)
; --------------- DoubleWord Values -----------------
dword1 DWORD 0F645A1bDh
dword2 SDWORD -1234525
; --------------- QuadWord Value --------------------
quad1 QWORD 0123456789ABCDEFh
intVal TBYTE 800000000000001234h
posVal REAL8 1.5
; --------------- Floating-Point Value --------------
rVal1 REAL4 -1.2
rVal2 REAL8 3.2E-260
rVal3 REAL10 4.6E+4096

.code
main proc
mov ax,5
add ax,word1
mov word2,ax
or bh, byte3
and bh, byte1
mov byte6, bh
invoke ExitProcess,0
main endp
end main

a. What is the total number of bytes allocated for data?


b. Watching Variables using the Windows Debugger, open the Watch window. Insert the
variable byte1, byte2 … etc under the Name column as shown below. To add a variable
to the Watch list, click in the first empty cell in the Name column, enter the name of this
variable, and press ENTER

Name Value Number of bytes Type


byte1
byte2
byte3
byte4
byte5
byte6
word1
word2
word3
word4
dword1
dword2
Quad1
intVal
posVal
rVal1
rVal2
rVal3

c. What is the value of byte6? explain your answer?


d. Insert 6 breakpoints at the beginning of each instruction, as the following:

e. Write down the values of the registers ( eax, ebx ), variables (byte1, byte3, byte6,
word1, word2), and flags that change at every breakpoint
f. Generate a listing file for the above program and write a description of the machine
code bytes generated for each instruction.
g. From the listing file, calculate the size of the code and data sections.
h. From the listing file, write down the machine code for the following:
Mov ax,5
Add ax,word1
i. Open the Disassembly window and compare the machine code you found in (h) for the
instructions. Do they match? Also, compare the size of each instruction and address
offset.
(To open the disassembly window in Debug mode, from the Debug tab > windows >
Disassembly. Make sure to set a breakpoint before the instructions)
j. Generate a Map file for the above program get the starting and end offset addresses of
the .data and .code segments and determine their sizes.
k. Compare your answer in part (a) with the size of .data segment found in the Map file.
Are they the same?

2. Write a program and declare the following:


a. Real number – 3.7 X 105 as a real number literal
b. Arrays of bytes
c. Arrays of words
d. Arrays of double words
e. Arrays of quad word
Either by explicitly using multiple initializers or by using the DUP (Duplicate) operator. Multiple
initializers are separated by commas and are used to initialize each element of the array with an
explicit number. Write is the total number of bytes allocated for each array.

3. Write a program that calculates the following expression, using registers: A = (A + B) − (C + D).
Assign integer values to the EAX, EBX, ECX, and EDX registers. ( assume the following A= 0F1h , B
= 20h, C = 0200h, D = 05h)
4. Repeat the previous program in question 2 to work in 64-bit mode, replace the used registers
with 64-bit registers.

Das könnte Ihnen auch gefallen