Download Chapter 3 (Part a) Assembly Language Fundamentals

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Islamic University – Gaza
Engineering Faculty
Department of Computer Engineering
ECOM 2025: Assembly Language Discussion
Chapter 3 (Part a)
Assembly Language Fundamentals
Eng. Eman R. Habib
February, 2014
2
Assembly Language Discussion
 Integer Constants:
[{+| −}] digits [radix]
Note:Elements within square brackets [..] are optional.
Elements within braces {..} require a choice of one of the enclosed elements (separated by the |
character).
Elements in italics denote items that have known definitions or descriptions.
Radix may be one of the following (uppercase or lowercase):
 h Hexadecimal
 b Binary
 q/o Octal
 d Decimal
 r Encoded real
 y Binary (alternate)
 t Decimal (alternate)
If no radix is given, the integer constant is assumed to be decimal.
Examples:
 26
 42o
 26d
 1Ah
 11010011b
 0A3h
 42q
Decimal
Octal
Decimal
Hexadecimal
Binary
Hexadecimal
Octal
 Integer Expressions:
Examples:
 -(3 - 4) * (6 - 1) = -35
 -3 - 4 * 6 - 1 = 20
 25 mod 3 = 1
3
Assembly Language Discussion
 Real Number Constants
[{+,-}] integer.[integer][ E[{+,-}]integer]
Examples:
 2.
 +3.0
 -44.2E+05
 26.E5
 Character Constants
Single character enclosed in single or double quotes.
Examples:
 'A'
 "d"
 String Constants
Sequence of characters (including spaces) enclosed in single or double quotes:
Examples:
 'ABC'
 'X'
 "Good night "
 '4096'
 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
 Identifiers
It might identify a variable, a constant, a procedure, or a code label.




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.
4
Assembly Language Discussion
 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.
 Directives can define variables, macros, and procedures.
 They can assign names to memory.
 In MASM, directives are case insensitive.
Defining Segments:
 .DATA directive identifies the area of a program containing variables.
 .CODE directive identifies the area of a program containing executable instructions.
 .STACK directive identifies the area of a program holding the runtime stack, setting its
size: .stack 100h.
 Instructions
An instruction is a statement that becomes executable when a program is assembled.
[label:] mnemonic [operands] [;comment]


Translated by the assembler into machine language.
Executed by the CPU at runtime.
An instruction contains four basic parts:
 Label (optional)
 Instruction mnemonic (required)
 Operand(s) (usually required)
 Comment (optional)
 Code Labels


Must end with a colon (:) character.
Used as targets of jumping and looping instructions.
Example:
target:
mov ax,bx
...
jmp target
 Instruction Mnemonic
Short word that identifies an instruction.
Examples:
mov Move (assign) one value to another
5
Assembly Language Discussion
add
sub
mul
jmp
call
Add two values
Subtract one value from another
Multiply two values
Jump to a new location
Call a procedure
 Operands


Instructions can have between zero and three operands.
Can be a register, memory operand, constant expression, or input-output port.
Examples:
 No operands:
stc
; set Carry flag
 One operand:
inc eax
; add 1 to EAX
 Two operands:
mov count,ebx
; move EBX to count
The first operand is called the destination. The second operand is the source.
 Three operands:
imul eax,ebx,5
; eax = ebx * 5
The first operand is the destination, and the following 2 operands are source operands.
 Comments


Single-line comments, beginning with a semicolon character (;), end by new line
Block comments, beginning with the COMMENT directive and a user-specified symbol,
end by the same user-specified symbol appears.
Example:
COMMENT !
This line is a comment.
This line is also a comment.
!
We can also use any other symbol:
COMMENT &
This line is a comment.
This line is also a comment.
&
6
Assembly Language Discussion
 Section 3.1 Review
1. Identify valid suffix characters used in integer constants.
h,q,o,d,b,r,t,y
3. (Yes/No): Does the multiplication operator (*) have a higher precedence than the
division operator (/) in integer expressions?
No (they have the same precedence)
4. Write a constant expression that divides 10 by 3 and returns the integer remainder.
10 MOD 3
5. Show an example of a valid real number constant with an exponent.
26.E5
7. Reserved words can be instruction mnemonics, attributes, operators, predefined
symbols, and directives.
8. What is the maximum length of an identifier?247 characters
13. Name the four basic parts of an assembly language instruction.
label, mnemonic, operand(s), comment
14. (True/False): MOV is an example of an instruction mnemonic.
16. Show an example of a block comment.
Comment !
This is a comment
This is also a comment
!
17. Why would it not be a good idea to use numeric addresses when writing instructions
that access variables?
Because the addresses coded in the instructions would have to be updated whenever
new variables were inserted before existing ones.
7
Assembly Language Discussion
 Example: Adding and Subtracting Integers
TITLE Add and Subtract (AddSub.asm)
; This program adds and subtracts 32-bit integers.
.386
.MODEL FLAT, STDCALL
INCLUDE Irvine32.inc
.code
main PROC
mov eax,10000h
; EAX = 10000h
add eax,40000h
; EAX = 50000h
sub eax,20000h
; EAX = 30000h
call DumpRegs
; display registers
exit
main ENDP
END main
TITLE Add and Subtract (AddSub.asm)
This line is optional. The TITLE directive marks the entire line as a comment.
; This program adds and subtracts 32-bit integers.
Comment.
.386
The .386 directive identifies the minimum CPU required for this program.
.model flat,stdcall
In the current .model directive, the flat keyword tells the assembler to generate code for a
protected mode program, and the stdcall keyword enables the calling of MS-Windows
functions.
INCLUDE Irvine32.inc
The INCLUDE directive copies necessary definitions and setup information from a text file
named Irvine32.inc.
.code
The .code directive marks the beginning of the code segment, where all executable statements
in a program are located.
main PROC
The PROC directive identifies the beginning of a procedure. The name chosen for the only
procedure in our program is main.
8
Assembly Language Discussion
mov eax,10000h
; EAX = 10000h
The MOV instruction moves (copies) the integer 10000h to the EAX register. The first operand
(EAX) is called the destination operand, and the second operand is called the source operand.
The comment on the right side shows the expected new value in the EAX register .
add eax,40000h
; EAX = 50000h
The ADD instruction adds 40000h to the EAX register.
sub eax,20000h
; EAX = 30000h
The SUB instruction subtracts 20000h from the EAX register.
call DumpRegs
; display registers
The CALL statement calls a procedure that displays the current values of the CPU registers. This
can be a useful way to verify that a program is working correctly.
exit
The exit statement (indirectly) calls a predefined MS-Windows function that halts the program.
Note that exit is not a MASM keyword; instead, it’s a macro command defined in the
Irvine32.inc include file that provides a simple way to end a program.
Expanded into a call to ExitProcess that terminates the program, ExitProcess function is defined
in the kernel32 library.
We can replace exit with the following:
push 0
; push parameter 0 on stack
call ExitProcess
; to terminate program
You can also replace exit with:
INVOKE ExitProcess, 0
There is a need to declare that ExitProcess is an external function defined outside the
addsub.asm program. We use the PROTO directive for this purpose as shown below:
ExitProcess PROTO, ExitCode:DWORD
This PROTO directive also specifies the parameters and types of a given function.
main ENDP
The ENDP directive marks the end of the main procedure.
END main
The END directive marks the last line of the program to be assembled. It identifies the name of
the program’s startup procedure (the procedure that starts the program execution).
 Section 3.2 Review
1. In the AddSub program (Section 3.2), what is the meaning of the INCLUDE directive?
The INCLUDE directive copies necessary definitions and setup information from the
Irvine32.inc text file. The data from this file is inserted into the data stream read by
the assembler.
9
Assembly Language Discussion
2. In the AddSub program, what does the .CODE directive identify?
.CODE directive identifies the area of a program containing executable instructions.
3. What are the names of the segments in the AddSub program?
code, data, and stack.
4. In the AddSub program, how are the CPU registers displayed?
call DumpRegs
5. In the AddSub program, which statement halts the program?
exit
6. Which directive begins a procedure?
PROC
7. Which directive ends a procedure?
ENDP
8. What is the purpose of the identifier in the END statement?
It identifies the name of the program’s startup procedure
9. What does the PROTO directive do?
ROTO declares the name of a procedure that is called by the current program.
 Assembling, Linking, and Running Programs
Step 1: A programmer uses a text editor to create an ASCII text file named the source file.
Step 2: The assembler reads the source file and produces an object file, a machine-language
translation of the program. Optionally, it produces a listing file. If any errors occur, the
programmer must return to Step 1 and fix the program.
Step 3: The linker reads the object file and checks to see if the program contains any calls to
procedures in a link library. The linker copies any required procedures from the link library,
combines them with the object file, and produces the executable file. Optionally, it produces
(.map) file
Step 4: The operating system loader utility reads the executable file into memory and branches
the CPU to the program’s starting address, and the program begins to execute.
10
Assembly Language Discussion
 Section 3.3 Review
1. What types of files are produced by the assembler?
Object (.OBJ) and listing (.LST) files.
2. (True/False): The linker extracts assembled procedures from the link library and
inserts them in the executable program.
3. (True/False): When a program’s source code is modified, it must be assembled and
linked again before it can be executed with the changes.
4. Which operating system component reads and executes programs?
Loader
5. What types of files is produced by the linker?
Executable (.EXE) and map (.MAP).
 Best Wishes 