Upload
muzaffar-salik
View
225
Download
0
Embed Size (px)
Citation preview
8/7/2019 Assembly Language Chap # 03
1/59
Computer Organization &Computer Organization &
Assembly LanguageAssembly Language
Assembly Language Fundamentals
Adapted from the slides prepared by Kip Irvine for the book,Assembly Language for Intel-Based Computers, 5th Ed.
8/7/2019 Assembly Language Chap # 03
2/59
Chapter OverviewChapter Overview
Basic Elements of Assembly LanguageBasic Elements of Assembly Language
Example: Adding and Subtracting IntegersExample: Adding and Subtracting Integers
Assembling, Linking, and Running ProgramsAssembling, Linking, and Running Programs
Defining DataDefining Data
Symbolic ConstantsSymbolic Constants
RealReal--Address Mode ProgrammingAddress Mode Programming
8/7/2019 Assembly Language Chap # 03
3/59
Basic Elements of AssemblyBasic Elements of Assembly
LanguageLanguage
Integer constantsInteger constants
Integer expressionsInteger expressions
Character and string constantsCharacter and string constants
Reserved words and identifiersReserved words and identifiers Directives and instructionsDirectives and instructions
LabelsLabels
Mnemonics and OperandsMnemonics and Operands
CommentsComments ExamplesExamples
8/7/2019 Assembly Language Chap # 03
4/59
8/7/2019 Assembly Language Chap # 03
5/59
Integer ExpressionsInteger Expressions
Operators and precedence levels:Operators and precedence levels:
Examples:Examples:
8/7/2019 Assembly Language Chap # 03
6/59
Real Number ConstantsReal Number Constants
[{+|[{+|--}]}] integerinteger. [. [integerinteger] [] [exponentexponent]]
Exponent:E[{+|Exponent:E[{+|--}]}]integerinteger
Examples: 2., +3.0,Examples: 2., +3.0, --44.2E+0544.2E+05
Encoded RealsEncoded Reals
IEEE floatingIEEE floating--point format (e.g. +1.0point format (e.g. +1.0Decimal = 3F800000r)Decimal = 3F800000r)
8/7/2019 Assembly Language Chap # 03
7/59
Character and String ConstantsCharacter and String Constants
Enclose character in single or double quotesEnclose character in single or double quotes
'A', "x"'A', "x"
ASCII character = 1 byteASCII character = 1 byte
Enclose strings in single or double quotesEnclose strings in single or double quotes "ABC""ABC"
'xyz''xyz'
Each character occupies a single byteEach character occupies a single byte
Embedded quotes:Embedded quotes:
This isnt a test"This isnt a test"
'Say "Goodnight," Gracie''Say "Goodnight," Gracie'
8/7/2019 Assembly Language Chap # 03
8/59
Reserved Words and IdentifiersReserved Words and Identifiers Reserved words cannot be used as identifiersReserved words cannot be used as identifiers
Instruction mnemonics (MOV), directives (.code), typeInstruction mnemonics (MOV), directives (.code), typeattributes (BYTE, WORD), operators (=), predefinedattributes (BYTE, WORD), operators (=), predefinedsymbols (@data)symbols (@data)
See MASM reference in Appendix ASee MASM reference in Appendix A IdentifiersIdentifiers
11--247 characters, including digits247 characters, including digits
notnot case sensitivecase sensitive
first character must be a letter, _, @, ?, or $first character must be a letter, _, @, ?, or $Examples: var1, Count, $first, _main, @@myfileExamples: var1, Count, $first, _main, @@myfile
8/7/2019 Assembly Language Chap # 03
9/59
DirectivesDirectives
Commands that are recognized and acted uponCommands that are recognized and acted uponby the assemblerby the assembler
Not part of the Intel instruction setNot part of the Intel instruction set
Used to declare code, data areas, selectUsed to declare code, data areas, selectmemory model, declare procedures, etc.memory model, declare procedures, etc.
not case sensitivenot case sensitive
Different assemblers have different directivesDifferent assemblers have different directives
NASM not the same as MASM, for exampleNASM not the same as MASM, for exampleExamples:Examples:.data, .code.data, .code
8/7/2019 Assembly Language Chap # 03
10/59
InstructionsInstructions Assembled into machine code byAssembled into machine code by
assemblerassembler
Executed at runtime by the CPUExecuted at runtime by the CPU
We use the Intel IAWe use the Intel IA--32 instruction set32 instruction set
An instruction contains:An instruction contains:
LabelLabel (optional)(optional)
MnemonicMnemonic (required)(required) OperandOperand (depends on the instruction)(depends on the instruction)
CommentComment (optional)(optional)Label: Mnemonic Operand(s) ;Comment
8/7/2019 Assembly Language Chap # 03
11/59
LabelsLabels Act as place markersAct as place markers
marks the address (offset) of code and datamarks the address (offset) of code and data
Follow identifier rulesFollow identifier rules
Data labelData label must be uniquemust be unique
example:example: count DWORD 100count DWORD 100 (not followed by colon)(not followed by colon)
Code labelCode label target of jump and loop instructionstarget of jump and loop instructions
example:example: target:target: (followed by colon)(followed by colon)
..
jmp targetjmp target
8/7/2019 Assembly Language Chap # 03
12/59
Mnemonics and OperandsMnemonics and OperandsInstruction MnemonicsInstruction Mnemonics
memory aidmemory aid
examples: MOV, ADD, SUB, MUL, INC,examples: MOV, ADD, SUB, MUL, INC,
DECDECOperandsOperands
constant (immediate value),constant (immediate value), 9696
constant expression,constant expression,2+42+4
Register,Register, eaxeax
memory (data label),memory (data label), countcount
Constants and constant ex ressions are oftenConstants and constant ex ressions are often
8/7/2019 Assembly Language Chap # 03
13/59
CommentsComments Comments are good!Comments are good!
explain the program's purposeexplain the program's purpose
when it was written, and by whomwhen it was written, and by whom
revision informationrevision information
tricky coding techniquestricky coding techniques
applicationapplication--specific explanationsspecific explanations
SingleSingle--line commentsline comments
begin with semicolon (;)begin with semicolon (;)
MultiMulti--line commentsline comments
begin with COMMENT directive and a programmerbegin with COMMENT directive and a programmer--chosenchosencharactercharacter
end with the same programmerend with the same programmer--chosen characterchosen character
COMMENT !
This is a comment
and this line is also a comment!
8/7/2019 Assembly Language Chap # 03
14/59
Instruction Format ExamplesInstruction Format Examples No operandsNo operands
stcstc ; set Carry flag; set Carry flag
One operandOne operand
inc eaxinc eax ; register ; register
inc myByteinc myByte ; memory; memory
Two operandsTwo operands
add ebx, ecxadd ebx, ecx ; register, register; register, register
sub myByte, 25sub myByte, 25 ; memory, constant; memory, constant
add eax, 36 * 25add eax, 36 * 25 ; register, constant; register, constant--
expressionexpression
8/7/2019 Assembly Language Chap # 03
15/59
What's NextWhat's Next Basic Elements of Assembly LanguageBasic Elements of Assembly Language Example: Adding and SubtractingExample: Adding and Subtracting
IntegersIntegers Assembling, Linking, and Running ProgramsAssembling, Linking, and Running Programs Defining DataDefining Data Symbolic ConstantsSymbolic Constants RealReal--Address Mode ProgrammingAddress Mode Programming
8/7/2019 Assembly Language Chap # 03
16/59
xamp e: ng an u tract ngxamp e: ng an u tract ng
IntegersIntegers
TITLE Add and Subtract (AddSub.asm)
; This program adds and subtracts 32-bit integers.
INCLUDEIrvine32.inc
.codemain PROC
mov eax,10000h ; EAX = 10000h
add eax,40000h ; EAX = 50000h
sub eax,20000h ; EAX = 30000h
call DumpRegs ; d isplay registers
exitmain ENDP
END main
8/7/2019 Assembly Language Chap # 03
17/59
Example OutputExample OutputProgram output, showing registers and flags:
EAX=00030000 EBX=7FFDF000 ECX=00000101 EDX=FFFFFFFF
ESI=00000000 EDI=00000000 EBP=0012FFF0 ESP=0012FFC4
EIP=00401024 EFL=00000206 CF=0 SF=0 ZF=0 OF=0
8/7/2019 Assembly Language Chap # 03
18/59
Suggested Coding StandardsSuggested Coding Standards Some approaches to capitalizationSome approaches to capitalization
capitalize nothingcapitalize nothing
capitalize everythingcapitalize everything
capitalize all reserved words, includingcapitalize all reserved words, includinginstruction mnemonics and registerinstruction mnemonics and registernamesnames
capitalize only directives and operatorscapitalize only directives and operators
Other suggestionsOther suggestions
descriptive identifier namesdescriptive identifier names
spaces surrounding arithmetic operatorsspaces surrounding arithmetic operators
blank lines between proceduresblank lines between procedures
8/7/2019 Assembly Language Chap # 03
19/59
Suggested Coding StandardsSuggested Coding Standards(cont.)(cont.)
Indentation and spacingIndentation and spacing code and data labelscode and data labels no indentationno indentation
executable instructionsexecutable instructions indent 4indent 4--55
spacesspaces comments: begin at column 40comments: begin at column 40--45,45,
aligned verticallyaligned vertically
11--3 spaces between instruction and its3 spaces between instruction and its
operandsoperands ex: mov ax,bxex: mov ax,bx
11--2 blank lines between procedures2 blank lines between procedures
8/7/2019 Assembly Language Chap # 03
20/59
8/7/2019 Assembly Language Chap # 03
21/59
Program TemplateProgram TemplateTITLE ProgramTemplate (Template.asm)
; ProgramDescription:
; Author:
; Creation Date:
; Revisions:
; Date: Modified by:
INCLUDEIrvine32.inc
.data
; (insert variables here)
.code
main PROC
; (insert executable instructions here)
exitmain ENDP
; (insert additional procedures here)
END main
8/7/2019 Assembly Language Chap # 03
22/59
What's NextWhat's Next Basic Elements of Assembly LanguageBasic Elements of Assembly Language
Example: Adding and SubtractingExample: Adding and SubtractingIntegersIntegers
Assembling, Linking, and RunningAssembling, Linking, and Running
ProgramsPrograms
Defining DataDefining Data
Symbolic ConstantsSymbolic Constants
RealReal--Address Mode ProgrammingAddress Mode Programming
8/7/2019 Assembly Language Chap # 03
23/59
, ,, ,
ProgramsPrograms
AssembleAssemble--LinkLink--Execute CycleExecute Cycle
make32.batmake32.bat
Listing FileListing File
Map FileMap File
8/7/2019 Assembly Language Chap # 03
24/59
8/7/2019 Assembly Language Chap # 03
25/59
make32.batmake32.bat
Called aCalled a batch filebatch file
Run it to assemble and link programsRun it to assemble and link programs
Contains a command that executesContains a command that executesML.EXE (the Microsoft Assembler)ML.EXE (the Microsoft Assembler)
Contains a command that executesContains a command that executesLINK32.EXE (the 32LINK32.EXE (the 32--bit Microsoft Linker)bit Microsoft Linker)
CommandCommand--Line syntax:Line syntax:make32make32 progNameprogName
(progName(progName includes the .asm extension)includes the .asm extension)(use make16.bat to assemble and link Real-mode programs)
8/7/2019 Assembly Language Chap # 03
26/59
Listing FileListing File Use it to see how your program isUse it to see how your program is
compiledcompiled
ContainsContains
source codesource code
addressesaddresses
object code (machine language)object code (machine language)
segment namessegment names symbols (variables, procedures, andsymbols (variables, procedures, and
constants)constants)
Example:Example: addSub.lstaddSub.lst
8/7/2019 Assembly Language Chap # 03
27/59
Map FileMap File Information about each programInformation about each program
segment:segment:
starting addressstarting address
ending addressending address sizesize
segment typesegment type
Example:Example: addSub.mapaddSub.map (16(16--bit version)bit version)
8/7/2019 Assembly Language Chap # 03
28/59
What's NextWhat's Next Basic Elements of Assembly LanguageBasic Elements of Assembly Language
Example: Adding and SubtractingExample: Adding and SubtractingIntegersIntegers
Assembling, Linking, and RunningAssembling, Linking, and RunningProgramsPrograms
Defining DataDefining Data
Symbolic ConstantsSymbolic Constants RealReal--Address Mode ProgrammingAddress Mode Programming
8/7/2019 Assembly Language Chap # 03
29/59
Defining DataDefining Data Intrinsic Data TypesIntrinsic Data Types Data Definition StatementData Definition Statement
Defining BYTE and SBYTE DataDefining BYTE and SBYTE Data
Defining WORD and SWORDDefining WORD and SWORDDataData
Defining DWORD and SDWORDDefining DWORD and SDWORDDataData
Defining QWORD DataDefining QWORD Data
Defining TBYTE DataDefining TBYTE Data
Defining Real Number DataDefining Real Number Data
Little Endian OrderLittle Endian Order
8/7/2019 Assembly Language Chap # 03
30/59
Intrinsic Data TypesIntrinsic Data Types (1 of 2)(1 of 2) BYTE, SBYTEBYTE, SBYTE
88--bit unsigned integer; 8bit unsigned integer; 8--bit signedbit signedintegerinteger
WORD, SWORDWORD, SWORD 1616--bit unsigned & signed integerbit unsigned & signed integer
DWORD, SDWORDDWORD, SDWORD
3232--bit unsigned & signed integerbit unsigned & signed integer QWORDQWORD
6464--bit integerbit integer
TBYTETBYTE
8/7/2019 Assembly Language Chap # 03
31/59
Intrinsic Data TypesIntrinsic Data Types (2 of 2)(2 of 2) REAL4REAL4
44--byte IEEE short realbyte IEEE short real
REAL8REAL8
88--byte IEEE long realbyte IEEE long real
REAL10REAL10
1010--byte IEEE extended realbyte IEEE extended real
8/7/2019 Assembly Language Chap # 03
32/59
Data Definition StatementData Definition Statement A data definition statement sets aside storageA data definition statement sets aside storage
in memory for a variable.in memory for a variable.
May optionally assign a name (label) to theMay optionally assign a name (label) to thedatadata
Syntax:Syntax:[[namename]]directivedirective initializerinitializer [,[,initializerinitializer] . . .] . . .
value1 BYTE 10value1 BYTE 10
All initializers become binary data in memoryAll initializers become binary data in memory
8/7/2019 Assembly Language Chap # 03
33/59
Defining BYTE and SBYTE DataDefining BYTE and SBYTE Data
value1 BYTE 'A' ; character constant
value2 BYTE 0 ; smallest unsigned byte
value3 BYTE 255 ; largest unsigned byte
value4 SBYTE -128 ; smallest signed byte
value5 SBYTE +127 ; largest signed byte
value6 BYTE ? ; uninitialized byte
Each of the following defines a single byte of storage:
A variable name is a data label that implies an offset (an address). If you declare a SBYTE variable, the Microsoft debugger will
automatically display its value in decimal with a leading sign.
8/7/2019 Assembly Language Chap # 03
34/59
Defining Byte ArraysDefining Byte Arrays
list1 BYTE 10,20,30,40
list2 BYTE 10,20,30,40
BYTE 50,60,70,80
BYTE 81,82,83,84
list3 BYTE ?,32,41h,00100010b
list4 BYTE 0Ah,20h,A,22h
Examples that use multiple initializers:
8/7/2019 Assembly Language Chap # 03
35/59
Defining StringsDefining Strings (1 of 3)(1 of 3)
A string is implemented as an array ofA string is implemented as an array ofcharacterscharacters For convenience, it is usually enclosed inFor convenience, it is usually enclosed in
quotation marksquotation marks
It often will beIt often will be nullnull--terminatedterminated
Examples:Examples:
str1 BYTE "Enter your name",0
str2 BYTE 'Error: halting program',0
str3 BYTE 'A','E','I','O','U'
greeting BYTE "Welcome to the Encryption Demo program "
BYTE "created by Kip Irvine.",0greeting2 \
BYTE "Welcome to the Encryption Demo program "
BYTE "created by Kip Irvine.",0
8/7/2019 Assembly Language Chap # 03
36/59
Defining StringsDefining Strings (cont.)(cont.) To continue a single string across multipleTo continue a single string across multiple
lines, end each line with a comma:lines, end each line with a comma:
menu BYTE "Checking Account",0dh,0ah,0dh,0ah,
"1. Create a new account",0dh,0ah,"2. Open an existing account",0dh,0ah,
"3. Credit the account",0dh,0ah,
"4. Debit the account",0dh,0ah,
"5. Exit",0ah,0ah,
"Choice> ",0
8/7/2019 Assembly Language Chap # 03
37/59
Defining StringsDefining Strings (cont.)(cont.) EndEnd--ofof--line character sequence:line character sequence:
0Dh = carriage return0Dh = carriage return
0Ah = line feed0Ah = line feed
str1 BYTE "Enter your name: ",0Dh,0AhBYTE "Enter your address: ",0
newLine BYTE 0Dh,0Ah,0
Idea: Define all strings used by your program in the samearea of the data segment.
8/7/2019 Assembly Language Chap # 03
38/59
Using the DUP OperatorUsing the DUP Operator
Use DUP to allocate (create space for) an arrayUse DUP to allocate (create space for) an arrayor string. Syntax:or string. Syntax: countercounterDUP (DUP ( argumentargument ))
CounterCounterandand argumentargumentmust be constants ormust be constants or
constant expressionsconstant expressionsvar1 BYTE 20 DUP(0) ; 20 bytes, all equal to zerovar2 BYTE 20 DUP(?) ; 20 bytes, uninitialized
var3 BYTE 4 DUP("STACK") ; 20 bytes: "STACKSTACKSTACKSTACK"
var4 BYTE 10,3 DUP(0),20 ; 5 bytes
8/7/2019 Assembly Language Chap # 03
39/59
Defining WORD and SWORDDefining WORD and SWORD
DataData Define storage for 16Define storage for 16--bit integersbit integers
or double charactersor double characters
single value or multiple valuessingle value or multiple values
word1 WORD 65535 ; largest unsigned valueword2 SWORD 32768 ; smallest signed value
word3 WORD ? ; uninitialized, unsigned
word4 WORD "AB" ; double characters
myList WORD 1,2,3,4,5 ; array of words
array WORD 5 DUP(?) ; uninitialized array
8/7/2019 Assembly Language Chap # 03
40/59
Defining DWORD andDefining DWORD and
SDWORDDataSDWORDData
val1 DWORD 12345678h ; unsigned
val2 SDWORD 2147483648 ; signed
val3 DWORD 20 DUP(?) ; unsigned array
val4 SDWORD 3,2,1,0,1 ; signed array
Storage definitions for signed and unsigned 32-bitintegers:
8/7/2019 Assembly Language Chap # 03
41/59
Defining QWORD, TBYTE, RealDefining QWORD, TBYTE, Real
DataData
quad1 QWORD 1234567812345678h
val1 TBYTE 1000000000123456789AhrVal1 REAL4 -2.1
rVal2 REAL8 3.2E-260
rVal3 REAL10 4.6E+4096
ShortArray REAL4 20 DUP(0.0)
Storage definitions for quadwords, tenbyte values,and real numbers:
8/7/2019 Assembly Language Chap # 03
42/59
Little Endian OrderLittle Endian Order All data types larger than a byte store theirAll data types larger than a byte store their
individual bytes in reverse order.individual bytes in reverse order.
The least significant byte occurs at theThe least significant byte occurs at the
first (lowest) memory address.first (lowest) memory address.
Example:Example:
val1 DWORD 12345678hval1 DWORD 12345678h
8/7/2019 Assembly Language Chap # 03
43/59
Adding Variables to AddSubAdding Variables to AddSubTITLE Add and Subtract, Version 2 (AddSub2.asm)
; This program adds and subtracts 32-bit unsigned
; integers and stores the sumin a variable.
INCLUDEIrvine32.inc
.data
val1 DWORD 10000h
val2DWOR
D40000
hval3 DWORD 20000h
finalVal DWORD ?
.code
main PROC
mov eax,val1 ; start with 10000h
add eax,val2 ; add 40000h
sub eax,val3 ; subtract 20000hmov finalVal,eax ; store the result (30000h)
call DumpRegs ; d isplay the registers
exit
main ENDP
END main
8/7/2019 Assembly Language Chap # 03
44/59
Declaring Uninitialized DataDeclaring Uninitialized Data Use the .data? directive to declare anUse the .data? directive to declare an
unintialized data segment:unintialized data segment:
.data?.data?
Within the segment, declare variables with "?"Within the segment, declare variables with "?"initializers:initializers:
smallArray DWORD 10 DUP(?)smallArray DWORD 10 DUP(?)
.data.data
smallArray DWORD 10 DUP(0)smallArray DWORD 10 DUP(0)
.data?.data?
bigArray DWORD 5000 DUP(?)bigArray DWORD 5000 DUP(?)
Advantage: the program's EXE file size is reduced.
8/7/2019 Assembly Language Chap # 03
45/59
Mixing code and dataMixing code and data
.code.code
mov eax, ebxmov eax, ebx
.data.data
temp DWORD ?temp DWORD ?.code.code
mov temp, eaxmov temp, eax
8/7/2019 Assembly Language Chap # 03
46/59
What's NextWhat's Next Basic Elements of Assembly LanguageBasic Elements of Assembly Language
Example: Adding and SubtractingExample: Adding and SubtractingIntegersIntegers
Assembling, Linking, and RunningAssembling, Linking, and RunningProgramsPrograms
Defining DataDefining Data
Symbolic ConstantsSymbolic Constants
RealReal--Address Mode ProgrammingAddress Mode Programming
8/7/2019 Assembly Language Chap # 03
47/59
Symbolic ConstantsSymbolic Constants EqualEqual--Sign DirectiveSign Directive
Calculating the Sizes of Arrays andCalculating the Sizes of Arrays andStringsStrings
EQU DirectiveEQU Directive
TEXTEQU DirectiveTEXTEQU Directive
8/7/2019 Assembly Language Chap # 03
48/59
EqualEqual--Sign DirectiveSign Directive namename == expressionexpression
expression is a 32expression is a 32--bit integer (expression orbit integer (expression orconstant)constant)
may be redefinedmay be redefined namename is called ais called a symbolic constantsymbolic constant
good programming style to use symbolsgood programming style to use symbols
Easier to modifyEasier to modify
Easier to understand,Easier to understand, ESC_keyESC_key
Array DWORDCOUNTDUP(0)Array DWORDCOUNTDUP(0)
COUNT=5COUNT=5
Mov al, COUNTMov al, COUNT
COUNT = 500
.
.
mov al,COUNT
8/7/2019 Assembly Language Chap # 03
49/59
Calculating the Size of a ByteCalculating the Size of a Byte
ArrayArray Current location counter: $Current location counter: $
subtract address of listsubtract address of list
difference is the number of bytesdifference is the number of bytes
list BYTE 10,20,30,40
BYTE 100 DUP(0)
ListSize = ($ - list)
8/7/2019 Assembly Language Chap # 03
50/59
Calculating the Size of a WordCalculating the Size of a Word
ArrayArrayDivide total number of bytes by 2 (the size of aDivide total number of bytes by 2 (the size of a
word)word)
list WORD 1000h,2000h,3000h,4000h
ListSize = ($ - list) / 2
a cu a ng e ze o aa cu a ng e ze o a
8/7/2019 Assembly Language Chap # 03
51/59
a cu a ng e ze o aa cu a ng e ze o a
Doubleword ArrayDoubleword Array
Divide total number of bytes by 4Divide total number of bytes by 4
(the size of a doubleword)(the size of a doubleword)
list DWORD 1,2,3,4ListSize = ($ - list) / 4
8/7/2019 Assembly Language Chap # 03
52/59
EQU directiveEQU directive name EQU expressionname EQU expression
name EQU symbolname EQU symbol
name EQU name EQU
Define a symbol as either an integer or textDefine a symbol as either an integer or textexpression.expression.
Can be useful for nonCan be useful for non--integer constantinteger constant
Cannot be redefinedCannot be redefined
8/7/2019 Assembly Language Chap # 03
53/59
EQU directiveEQU directivePIEQU
pressKey EQU
.data
prompt BYTE pressKey
Matrix1 EQU 10*10
matrix1 EQU
.data
M1 WORD matrix1 ; M1 WORD 100
M2 WORD matrix2 ; M2 WORD 10*10
8/7/2019 Assembly Language Chap # 03
54/59
TEXTEQUDirectiveTEXTEQUDirective Define a symbol as either an integer or textDefine a symbol as either an integer or text
expression.expression.
Called aCalled a text macrotext macro
Can be redefinedCan be redefinedcontinueMsg TEXTEQU
rowSize = 5
.data
prompt1 BYTE continueMsg
count TEXTEQU %(rowSize * 2) ; evaluates the expression
setupALTEXTEQU
.code
setupAL ; generates: "mov al,10"
8/7/2019 Assembly Language Chap # 03
55/59
What's NextWhat's Next Basic Elements of Assembly LanguageBasic Elements of Assembly Language
Example: Adding and SubtractingExample: Adding and SubtractingIntegersIntegers
Assembling, Linking, and RunningAssembling, Linking, and RunningProgramsPrograms
Defining DataDefining Data
Symbolic ConstantsSymbolic Constants RealReal--Address Mode ProgrammingAddress Mode Programming
R lR l Add M dAdd M d
8/7/2019 Assembly Language Chap # 03
56/59
RealReal--Address ModeAddress Mode
ProgrammingProgramming Generate 16Generate 16--bit MSbit MS--DOS ProgramsDOS Programs AdvantagesAdvantages
enables calling of MSenables calling of MS--DOS and BIOSDOS and BIOS
functionsfunctions no memory access restrictionsno memory access restrictions
DisadvantagesDisadvantages
must be aware of both segments andmust be aware of both segments andoffsetsoffsets
cannot call Win32 functions (Windows 95cannot call Win32 functions (Windows 95onward)onward)
limited to 640K program memorylimited to 640K program memory
8/7/2019 Assembly Language Chap # 03
57/59
8/7/2019 Assembly Language Chap # 03
58/59
Add and Subtract, 16Add and Subtract, 16--Bit VersionBit VersionTITLE Add and Subtract, Version 2 (AddSub2r.asm)INCLUDEIrvine16.inc
.data
val1 DWORD 10000h
val2 DWORD 40000h
val3 DWORD 20000h
finalVal DWORD ?
.code
main PROC
mov ax,@data ; initialize DS
mov ds,ax
mov eax,val1 ; get first value
add eax,val2 ; add second value
sub eax,val3 ; subtract third value
mov finalVal,eax ; store the result
call DumpRegs ; d isplay registers
exit
main ENDP
END main
8/7/2019 Assembly Language Chap # 03
59/59
SummarySummary
Integer expression, character constantInteger expression, character constant
directivedirective interpreted by the assemblerinterpreted by the assembler
instructioninstruction executes at runtimeexecutes at runtime
code, data, and stack segmentscode, data, and stack segments
source, listing, object, map, executablesource, listing, object, map, executablefilesfiles
Data definition directives:Data definition directives: BYTE, SBYTE, WORD, SWORD, DWORD,BYTE, SBYTE, WORD, SWORD, DWORD,
SDWORD, QWORD, TBYTE, REAL4, REAL8,SDWORD, QWORD, TBYTE, REAL4, REAL8,and REAL10and REAL10
$$