Lecture 17 18
Lecture 17 18
Fundamentals
• Basic Elements of Assembly Language
• Example: Adding and Subtracting Integers
• Assembling, Linking, and Running Programs
• Defining Data
• Symbolic Constants
• Integer constants
• Integer expressions
• Character and string constants
• Reserved words and identifiers
• Directives and instructions
• Labels
• Mnemonics and Operands
• Comments
main PROC
mov eax, 5 ; move 5 to the EAX register
add eax, 6 ; add 6 to the EAX register
call WriteInt ; display value in EAX
exit; quit
main ENDP
• Examples:
Precedence Examples:
4+5*2 Multiply, add
12 – 1 MOD 5 Modulus, subtract
-5 + 2 Unary minus, add
(4 + 2) * 6 Add, multiply
• Represented as decimal real.
• Decimal real contains optional sign followed by integer,
decimal point, and optional integer that expresses a
fractional and an optional exponent
• [sign] integer.[integer] [exponent]
• Sign {+, -}
• Exponent E[{+, -}] integer
• Examples
• 2.
• +3.0
• -44.2E+05
• 26.E5
• Enclose character in single or double quotes
• 'A', "x"
• Enclose strings in single or double quotes
• "ABC"
• 'xyz'
• Each character occupies a single byte
• Embedded quotes:
• 'Say "Goodnight," Gracie'
• Reserved words cannot be used as identifiers
• Instruction mnemonics
• MOV, ADD, MUL,, …
• Register names
• Directives – tells assembler how to assemble programs to
reserve space in the program (for variable for example).
• type attributes – provides size and usage information
for variables and operands.
• BYTE, WORD
• Operators – used in constant expressions
• predefined symbols – @data, which return constant integer
values
• Identifiers
• Programmer-chosen name to identify a variable, constant,
procedure, or code label
• 1-247 characters, including digits
• not case sensitive
• first character must be a letter (A..Z, a..z), _, @, ?, or $
• Cannot be the same as a reserved word
• @ is used by assembler as a prefix for predefined symbols,
so avoid it in your own identifiers.
• Examples
• Var1, Count, $first, _main, MAX, open_file, myFile, xVal,
_12345
• Directives can define variables, macros, and
procedures.
• They can assign names to memory segments and
perform many other tasks related to the assembler.
• It is Commands that are recognized and acted upon
by the assembler
• Not part of the Intel instruction set
• Used to declare code, data areas, select memory
model, declare procedures, etc.
• not case sensitive
• Different assemblers have different directives
myVar DWORD 26 ; DWORD directive, reserve
; enough space for double word
• Code label
• target of jump and loop instructions
• example: target:
target: (followed by colon)
mov ax, bx
…
jmp target
• Instruction Mnemonics
• is short word that identifies an instruction.
• examples: MOV, ADD, SUB, MUL, INC, DEC
• Operands
• constant 96
• constant expression 2+4
• register eax
• memory (data label) count
INC instruction
inc eax ; add 1 to EAX
MOV instruction
mov count, ebx ; move EBX to count
; first operand is destination
; second is the source
IMUL instruction (three operands)
imul eax, ebx, 5 ; ebx multiplied by 5, product in EAX
• Comments are good!
• explain the program's purpose
• when it was written, and by whom
• revision information
• tricky coding techniques
• application-specific explanations
• Single-line comments
• begin with semicolon (;) or TITLE directive
• Multi-line comments
• begin with COMMENT directive and a programmer-
chosen character
• end with the same programmer-chosen character
• Single line comment
• inc eax ; single line at end of instruction
• ; single line at beginning of line
• Multiline comment
COMMENT !
This line is a comment
This line is also a comment
!
COMMENT &
This is a comment
This is also a comment
&
• Basic Elements of Assembly Language
• Example: Adding and Subtracting Integers
• Assembling, Linking, and Running Programs
• Defining Data
• Symbolic Constants
TITLE Add and Subtract (AddSub.asm)
; This program adds and subtracts 32-bit
.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)
The TITLE directive marks the entire line as a comment. You can put
anything you want on this line.
INCLUDE Irvine32.inc
The .code directive marks the beginning of the code segment, where all
executable statements in a program are located.
main PROC
The MOV instruction moves (copies) the integer 10000h to the EAX
register.
add eax,40000h ; EAX = 50000h
The CALL statement calls a procedure that displays the current values of
the CPU registers.
Exit
main ENDP
END main
The END directive marks the last line of the program to be assembled.
Program output, showing registers and flags:
ExitProcess PROTO,
dwExitCode:DWORD DumpRegs PROTO
.code
main PROC
mov eax,10000h ; EAX = 10000h
add eax,40000h ; EAX = 50000h
sub eax,20000h ; EAX = 30000h
call DumpRegs
INVOKE ExitProcess,0
main ENDP END
main
.386
identifies the minimum CPU required for this program (Intel386,
the first x86 processor).
.MODEL flat,stdcall
used to identify the segmentation model used by the program and it
identifies the convention used for passing parameters to procedures.
The flat keyword tells the assembler to generate code for a
protected mode program. The stdcall keyword enables the calling of
MS- Windows functions.
.STACK 4096
identifies the area of a program holding the runtime stack, and setting its
size 4096.
ExitProcess PROTO,
dwExitCode:DWORD DumpRegs PROTO
Two PROTO directives declare prototypes for procedures used by this
program:
ExitProcess is an MS-Windows function that halts the current program, and
DumpRegs is a procedure from the Irvine32 link library that displays
registers.
INVOKE ExitProcess,0
INVOKE is directive that calls a procedure function. ExitProcess
function ends the program.
TITLE Program Template (Template.asm)
;***************************************************
; Program Name:
; Program Description:
; Author:
; Version:
; Date:
; Other Information:
;***************************************************
INCLUDE Irvine32.inc
.data
; (insert variables here)
.code
main PROC
; (insert executable instructions here)
exit
main ENDP
; (insert additional procedures here)
END main
• Basic Elements of Assembly Language
• Example: Adding and Subtracting Integers
• Assembling, Linking, and Running Programs
• Defining Data
• Symbolic Constants
• Real-Address Mode Programming
• Assemble-Link-Execute Cycle
• Listing File
• Map File
• Assembly language program must be translated to machine
language for the target processor.
• The following diagram describes the steps from creating a
source program through executing the compiled program.
• If the source code is modified, Steps 2 through 4 must
be repeated.
Link
Library
Step 2: Step 3: Step 4:
Source assembler Object linker Executable OS loader
Output
File File File
Listing Map
Step 1: text editor File File
Step 1: A programmer uses a text editor to create the source
file.
Step 3: The linker reads the object file and checks to see if
the program contains any calls to procedures in a link library,
combines them with the object file, and produces the
executable file.
• REAL4
• 4-byte short real
• REAL8
• 8-byte long real
• REAL10
• 10-byte extended real
• A data definition statement sets aside storage in
memory for a variable.
• It creates variables based on intrinsic data types
• Syntax:
[name] directive initializer [,initializer] . . .
value1 BYTE 10
list WORD
1000h,2000h,3000h,4000h
ListSize = ($ - list) / 2
Divide total number of bytes by 4 (the size of a
doubleword)
list DWORD
10000000h,20000000h
ListSize = ($ - list) / 4
• Define a symbol as either integer expression, symbol
or text .
• Cannot be redefined
name EQU expression
name EQU symbol
name EQU <text>