Learning Assembly Language - Part 01
Learning Assembly Language - Part 01
• Defining Data
2
Basic Elements of Assembly Language
3
Basic Elements of Assembly Language
• Reserved words
– Reserved words cannot be used as identifiers
– Instruction mnemonics, directives, type attributes,
operators, predefined symbols
– Examples: BYTE, MOV, ADD
• Identifiers
– To identify a variable, a constant, a label, a proc.
– Examples: $first, _main, @myfile
4
Basic Elements of Assembly Language
• Directives
– Used to declare code, data areas, select memory
model, declare procedures, etc.
– not case sensitive
– Example: .data, .code
5
Basic Elements of Assembly Language
• Instructions
– Assembled into machine code by assembler
– Executed at runtime by the CPU
– An instruction contains:
• Label (optional)
• Mnemonic (required)
• Operand (depends on the instruction)
• Comment (optional)
6
Basic Elements of Assembly Language
• Labels
– Follow identifier rules
– Data label
• must be unique
• example: myArray (not followed by colon)
– Code label
• target of jump and loop instructions
• example: L1: (followed by colon)
7
Basic Elements of Assembly Language
• Instruction Mnemonics
– Identify the operation carried out by an instruction
– examples: MOV, ADD, SUB, MUL, INC, DEC
• Operands
– constant
– constant expression
– register
– memory (data label)
– Example: MOV count, bx : INC ax
8
Basic Elements of Assembly Language
• Comments
– Single-line comments
• begin with semicolon (;)
• Example: ; my code goes here
– Multi-line comments
• begin with COMMENT directive and a programmer-
chosen character
• Example: COMMENT &
These lines are commented
&
9
An Assembly Program Template
; Program Description:
; Author:
; Creation Date:
; Revisions:
; Date: Modified by:
INCLUDE Irvine32.inc
.data
; (insert variables here)
.code
main PROC
; (insert executable instructions here)
exit
main ENDP
; (insert additional procedures here)
END main
10
Example (Assembly program)
• ; This program adds and subtracts 32-bit
integers.
• 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
11
Example Program Output
12
Basic Elements of Assembly Language
• Defining Data
13
Assembling, Linking, and Running Programs
Listing Map
Step 1: text editor File File
14
Make32.bat
15
Listing File
16
MAP File
17
Data Types
• BYTE, SBYTE
– 8-bit unsigned integer; 8-bit signed integer
• WORD, SWORD
– 16-bit unsigned & signed integer
• DWORD, SDWORD
– 32-bit unsigned & signed integer
• QWORD
– 64-bit integer
• TBYTE
– 80-bit integer
18
Defining BYTE and SBYTE Data
19
Defining Byte Arrays
20
Defining Strings
21
Defining Strings
22
Defining Strings
23
Using the DUP Operator
24
Defining WORD and SWORD Data
25
Defining QWORD, TBYTE, Real Data
REAL4
4-byte IEEE short real
REAL8
8-byte IEEE long real
REAL10
10-byte IEEE extended real
26
Little Endian Order
Example:
val1 DWORD 12345678h
27
Adding Variables to AddSub
TITLE Add and Subtract, Version 2 (AddSub2.asm)
; This program adds and subtracts 32-bit unsigned
; integers and stores the sum in a variable.
INCLUDE Irvine32.inc
.data
val1 DWORD 10000h
val2 DWORD 40000h
val3 DWORD 20000h
finalVal DWORD ?
.code
main PROC
mov eax,val1 ; start with 10000h
add eax,val2 ; add 40000h
sub eax,val3 ; subtract 20000h
mov finalVal,eax ; store the result (30000h)
call DumpRegs ; display the registers
exit
main ENDP
END main
28