3 Introduction to Assembly Language
3 Introduction to Assembly Language
Assembly Language
Presentation Outline
• Basic Elements of Assembly Language
• Flat Memory Program Template
• Example: Adding and Subtracting Integers
• Assembling, Linking, and Debugging
Programs
• Defining Data
• Defining Symbolic Constants
• Data-Related Operators and Directives
Constants
• Integer Constants
• Examples: –10, 42d, 10001101b, 0FF3Ah, 777o
• Radix: b = binary, d = decimal, h = hexadecimal, and o = octal
• If no radix is given, the integer constant is decimal
• A hexadecimal beginning with a letter must have a leading 0
• Character and String Constants
• Enclose character or string in single or double quotes
• Examples: 'A', "d", 'ABC', "ABC", '4096'
• Embedded quotes: "single quote ' inside", 'double quote "
inside'
• Each ASCII character occupies a single byte
Assembly Language Statements
• Three types of statements in assembly language
• Typically, one statement should appear on a line
1. Executable Instructions
• Generate machine code for the processor to execute at runtime
• Instructions tell the processor what to do
2. Assembler Directives
• Provide information to the assembler while translating a program
• Used to define data, select memory model, etc.
• Non-executable: directives are not part of instruction set
3. Macros
• Shorthand notation for a group of statements
• Sequence of instructions, directives, or other macros
Instructions
• Assembly language instructions have the format:
• Mnemonic
• Identifies the operation (e.g. MOV, ADD, SUB, JMP, CALL)
• Operands
• Specify the data required by the operation
• Executable instructions can have zero to three operands
• Operands can be registers, memory variables, or constants
Instruction Examples
• No operands
stc ; set carry flag
• One operand
inc eax ; increment register eax
call Clrscr ; call procedure Clrscr
jmp L1 ; jump to instruction with label L1
• Two operands
add ebx, ecx ; register ebx = ebx + ecx
sub var1, 25 ; memory variable var1 = var1 - 25
• Three operands
imul eax,ebx,5 ; register eax = ebx * 5
Comments
• Comments are very important!
• Explain the program's purpose
• When it was written, revised, and by whom
• Explain data used in the program
• Explain instruction sequences and algorithms used
• Application-specific explanations
• Single-line comments
• Begin with a semicolon ; and terminate at end of line
• Multi-line comments
• Begin with COMMENT directive and a chosen character
• End with the same chosen character
Next . . .
• Basic Elements of Assembly Language
• Flat Memory Program Template
• Example: Adding and Subtracting Integers
• Assembling, Linking, and Debugging
Programs
• Defining Data
• Defining Symbolic Constants
• Data-Related Operators and Directives
Flat Memory Program Template
TITLE Flat Memory Program Template (Template.asm)
; Program Description:
; Author: Creation Date:
; Modified by: Modification Date:
.686
.MODEL FLAT, STDCALL
.STACK
INCLUDE Irvine32.inc
.DATA
; (insert variables here)
.CODE
main PROC
; (insert executable instructions here)
exit
main ENDP
; (insert additional procedures here)
END main
TITLE and .MODEL Directives
• TITLE line (optional)
• Contains a brief heading of the program and the disk file name
• .MODEL directive
• Specifies the memory configuration
• For our purposes, the FLAT memory model will be used
• Linear 32-bit address space (no segmentation)
• STDCALL directive tells the assembler to use …
• Standard conventions for names and procedure calls
.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
Example of Console Output
Procedure DumpRegs is defined in Irvine32.lib library
It produces the following console output,
showing registers and flags:
.686
.MODEL flat,stdcall
.STACK 4096
.code
main PROC
mov eax,10000h ; EAX = 10000h
add eax,40000h ; EAX = 50000h
sub eax,20000h ; EAX = 30000h
push 0
call ExitProcess ; to terminate program
main ENDP
END main
Next . . .
• Basic Elements of Assembly Language
• Flat Memory Program Template
• Example: Adding and Subtracting Integers
• Assembling, Linking, and Debugging
Programs
• Defining Data
• Defining Symbolic Constants
• Data-Related Operators and Directives
Assemble-Link-Debug
• Editor
Cycle
• Write new (.asm) programs
Edit
• Make changes to existing ones
• Assembler: ML.exe program prog.asm
assembler
• Linker: LINK32.exe program Link
• Debugger: WINDBG.exe
• Trace program execution Assemble
• Either step-by-step, or
library.lib prog.obj prog.lst
• Use breakpoints
• View
Link
• Source (.asm) code
• Registers prog.exe prog.map
• Memory by name & by address
• Modify register & memory content Debug Run
• Discover errors and go back to the editor to fix the program
bugs
Listing File
• Use it to see how your program is assembled
• Contains
Object & source code in a listing file
• Source code
00000000 .code
00000000 main PROC
• Object code 00000000 B8 00060000 mov eax, 60000h
00000005 05 00080000 add eax, 80000h
• Relative addresses 0000000A 2D 00020000 sub eax, 20000h
val1 BYTE 10
.DATA
bVal BYTE ? ; Assume bVal is at 00404000h
wVal WORD ?
dVal DWORD ?
dVal2 DWORD ?
.CODE
mov esi, OFFSET bVal ; ESI = 00404000h
mov esi, OFFSET wVal ; ESI = 00404001h
mov esi, OFFSET dVal ; ESI = 00404003h
mov esi, OFFSET dVal2 ; ESI = 00404007h
ALIGN Directive
• ALIGN directive aligns a variable in memory
• Syntax: ALIGNbound
• Wherebound can be 1, 2, 4, or 16
• Address of a variable should be a multiple ofbound
• Assembler inserts empty bytes to enforce alignment
.DATA
var1 BYTE ?
var2 WORD ?
var3 DWORD ?
var4 QWORD ?
.CODE
mov eax, TYPE var1 ; eax = 1
mov eax, TYPE var2 ; eax = 2
mov eax, TYPE var3 ; eax = 4
mov eax, TYPE var4 ; eax = 8
LENGTHOF Operator
LENGTHOF operator
Counts the number of elements in a single data declaration
.DATA
array1 WORD 30 DUP(?),0,0
array2 WORD 5 DUP(3 DUP(?))
array3 DWORD 1,2,3,4
digitStr BYTE "12345678",0
.code
mov ecx, LENGTHOF array1 ; ecx = 32
mov ecx, LENGTHOF array2 ; ecx = 15
mov ecx, LENGTHOF array3 ; ecx = 4
mov ecx, LENGTHOF digitStr ; ecx = 9
SIZEOF Operator
SIZEOF operator
Counts the number of bytes in a data declaration
Equivalent to multiplying LENGTHOF by TYPE
.DATA
array1 WORD 30 DUP(?),0,0
array2 WORD 5 DUP(3 DUP(?))
array3 DWORD 1,2,3,4
digitStr BYTE "12345678",0
.CODE
mov ecx, SIZEOF array1 ; ecx = 64
mov ecx, SIZEOF array2 ; ecx = 30
mov ecx, SIZEOF array3 ; ecx = 16
mov ecx, SIZEOF digitStr ; ecx = 9
Multiple Line Declarations
A data declaration spans multiple In the following example, array
lines if each line (except the last) identifies the first line WORD
ends with a comma declaration only
The LENGTHOF and SIZEOF Compare the values returned by
operators include all lines LENGTHOF and SIZEOF here to
belonging to the declaration those on the left
.DATA .DATA
array WORD 10,20, array WORD 10,20
30,40, WORD 30,40
50,60 WORD 50,60
.CODE .CODE
mov eax, LENGTHOF array ; 6 mov eax, LENGTHOF array ; 2
mov ebx, SIZEOF array ; 12 mov ebx, SIZEOF array ; 4
PTR Operator
PTR Provides the flexibility to access part of a variable
Can also be used to combine elements of a smaller type
Syntax:Type PTR (Overrides default type of a variable)
.CODE
mov al, dval ; error – why?
mov al, BYTE PTR dval ; al = 78h
mov ax, dval ; error – why?
mov ax, WORD PTR dval ; ax = 5678h
mov eax, array ; error – why?
mov eax, DWORD PTR array ; eax = 30201000h
LABEL Directive
• Assigns an alternate name and type to a memory
location
• LABEL does not allocate any storage of its own
• Removes the need for the PTR operator
• Format:Name LABELType
.DATA blist
dval LABEL DWORD
00 10 00 20
wval LABEL WORD
wval
blist BYTE 00h,10h,00h,20h
.CODE dval
mov eax, dval ; eax = 20001000h
mov cx, wval ; cx = 1000h
mov dl, blist ; dl = 00h
Summary
• Instruction executed at runtime
• Directive interpreted by the assembler
• .STACK, .DATA, and .CODE
• Define the code, data, and stack sections of a program
• Edit-Assemble-Link-Debug Cycle
• Data Definition
• BYTE, WORD, DWORD, QWORD, etc.
• DUP operator
• Symbolic Constant
• =, EQU, and TEXTEQU directives
• Data-Related Operators
• OFFSET, ALIGN, TYPE, LENGTHOF, SIZEOF, PTR, and LABEL