intro to assembly

25
Introduction to Assembly Language

Upload: ranaateeq

Post on 29-Sep-2015

224 views

Category:

Documents


1 download

DESCRIPTION

microprocessor lecture

TRANSCRIPT

Introduction to Assembly Language

Introduction toAssembly LanguagePresentation OutlineBasic Elements of Assembly LanguageFlat Memory Program TemplateExample: Adding and Subtracting IntegersAssembling, Linking, and Debugging ProgramsDefining DataDefining Symbolic ConstantsData-Related Operators and DirectivesAssembly Language StatementsThree types of statements in assembly languageTypically, one statement should appear on a lineExecutable InstructionsGenerate machine code for the processor to execute at runtimeInstructions tell the processor what to doAssembler DirectivesProvide information to the assembler while translating a programUsed to define data, select memory model, etc.Non-executable: directives are not part of instruction setMacrosShorthand notation for a group of statementsSequence of instructions, directives, or other macros InstructionsAssembly language instructions have the format:[label:] opcode [operands] [;comment]Instruction Label (optional)Gives symbolic name to address of the instruction, must have a colon :Used to transfer program execution to a labeled instruction opcodeIdentifies the operation (e.g. MOV, ADD, SUB, JMP, CALL)OperandsSpecify the data required by the operationExecutable instructions can have zero to three operandsOperands can be registers, memory variables/symbolic names of memory locations, or constantsCommentsComments are very important!anything after a semicolon is a comment ignored by assemblerused by humans to document/understand programstips for useful comments:avoid restating the obviousprovide additional insight, as in accumulate product in R6Single-line commentsBegin with a semicolon ; and terminate at end of lineMulti-line commentsBegin with COMMENT directive and a chosen characterEnd with the same chosen characterNext . . .Basic Elements of Assembly LanguageProgram TemplateExample: Adding and Subtracting IntegersAssembling, Linking, and Debugging ProgramsDefining DataDefining Symbolic ConstantsData-Related Operators and DirectivesProgram TemplateINCLUDE Irvine32.inc

.DATA; (insert variables here)

.CODE

main PROC; (insert executable instructions here)exitmain ENDP

; (insert additional procedures here)

END main.DATA, & .CODE DirectivesDirectives/Pseudo-operationsdo not refer to operations executed by programused by assembler.DATA directiveDefines an area in memory for the program dataThe program's variables should be defined under this directiveAssembler will allocate and initialize the storage of variables.CODE directiveDefines the code section of a program containing instructionsAssembler will place the instructions in the code area in memoryINCLUDE, PROC, ENDP, and ENDINCLUDE directiveCauses the assembler to include code from another fileWe will include Irvine32.inc provided by the author Kip IrvineDeclares procedures implemented in the Irvine32.lib libraryTo use this library, you should link Irvine32.lib to your programsPROC and ENDP directivesUsed to define proceduresAs a convention, we will define main as the first procedureAdditional procedures can be defined after mainEND directiveMarks the end of a programIdentifies the name (main) of the programs startup procedureNext . . .Basic Elements of Assembly LanguageFlat Memory Program TemplateExample: Adding and Subtracting IntegersAssembling, Linking, and Debugging ProgramsDefining DataData-Related Operators and DirectivesINCLUDE Irvine32.inc

.CODE

main PROCmov eax,10000h; EAX = 10000hadd eax,40000h; EAX = 50000hsub eax,20000h; EAX = 30000hcall DumpRegs; display registersexitmain ENDP

END mainAdding and Subtracting IntegersExample of Console OutputProcedure DumpRegs is defined in Irvine32.lib library It produces the following console output,showing registers and flags:EAX=00030000 EBX=7FFDF000 ECX=00000101 EDX=FFFFFFFFESI=00000000 EDI=00000000 EBP=0012FFF0 ESP=0012FFC4EIP=00401024 EFL=00000206 CF=0 SF=0 ZF=0 OF=0

Next . . .Basic Elements of Assembly LanguageFlat Memory Program TemplateExample: Adding and Subtracting IntegersAssembling, Linking, and Debugging ProgramsDefining DataDefining Symbolic ConstantsData-Related Operators and DirectivesProgram Life CycleEditorWrite new (.asm) programsMake changes to existing onesAssembler: ML.exe programTranslate (.asm) file into object (.obj) file in machine languageCan produce a listing (.lst) file that shows the work of assemblerLinker: LINK32.exe programCombine object (.obj) files with link library (.lib) filesProduce executable (.exe) fileCan produce optional (.map) file EditAssembleLinkRunprog.asmprog.objprog.lstprog.exeprog.maplibrary.libDebugNext . . .Basic Elements of Assembly LanguageFlat Memory Program TemplateExample: Adding and Subtracting IntegersAssembling, Linking, and Debugging ProgramsDefining DataDefining Symbolic ConstantsData-Related Operators and DirectivesBYTE, SBYTE8-bit unsigned integer8-bit signed integerWORD, SWORD16-bit unsigned integer16-bit signed integerDWORD, SDWORD32-bit unsigned integer32-bit signed integerQWORD, TBYTE64-bit integer80-bit integerIntrinsic Data TypesData Definition StatementSets aside storage in memory for a variableMay optionally assign a name (label) to the dataSyntax:[name] directive initializer [, initializer] . . .

val1 BYTE 10

All initializers become binary data in memoryDefining BYTE and SBYTE Datavalue1 BYTE 'A'; character constantvalue2 BYTE 0; smallest unsigned bytevalue3 BYTE 255; largest unsigned bytevalue4 SBYTE -128; smallest signed bytevalue5 SBYTE +127; largest signed bytevalue6 BYTE ?; uninitialized byteEach of the following defines a single byte of storage:MASM does not prevent you from initializing a BYTE with a negative value, but it's considered poor style.If you declare a SBYTE variable, the Microsoft debugger will automatically display its value in decimal with a leading sign.Defining Byte Arrayslist1 BYTE 10,20,30,40list2 BYTE 10,20,30,40 BYTE 50,60,70,80 BYTE 81,82,83,84list3 BYTE ?,32,41h,00100010blist4 BYTE 0Ah,20h,'A',22hExamples that use multiple initializersDefining StringsA string is implemented as an array of charactersFor convenience, it is usually enclosed in quotation marksIt is often terminated with a NULL char (byte value = 0)Examples:str1 BYTE "Enter your name", 0str2 BYTE 'Error: halting program', 0str3 BYTE 'A','E','I','O','U'greeting BYTE "Welcome to the Encryption " BYTE "Demo Program", 0Defining 16-bit and 32-bit DataDefine storage for 16-bit and 32-bit integersSigned and UnsignedSingle or multiple initial valuesword1 WORD 65535 ; largest unsigned 16-bit valueword2 SWORD 32768 ; smallest signed 16-bit valueword3 WORD "AB" ; two characters fit in a WORDarray1 WORD 1,2,3,4,5 ; array of 5 unsigned wordsarray2 SWORD 5 DUP(?) ; array of 5 signed wordsdword1 DWORD 0ffffffffh ; largest unsigned 32-bit valuedword2 SDWORD 2147483648 ; smallest signed 32-bit valuearray3 DWORD 20 DUP(?) ; 20 unsigned double wordsarray4 SDWORD 3,2,1,0,1; 5 signed double wordsQWORD, TBYTE, and REAL Dataquad1 QWORD 1234567812345678hval1 TBYTE 1000000000123456789AhQWORD and TBYTEDefine storage for 64-bit and 80-bit integersSigned and UnsignedAdding Variables to AddSubINCLUDE Irvine32.inc

.DATAval1 DWORD 10000hval2 DWORD 40000hval3 DWORD 20000hresult DWORD ?

.CODEmain PROCmov eax,val1; start with 10000hadd eax,val2; add 40000hsub eax,val3; subtract 20000hmov result,eax; store the result (30000h)call DumpRegs; display the registersexitmain ENDP

END main

Next . . .Basic Elements of Assembly LanguageFlat Memory Program TemplateExample: Adding and Subtracting IntegersAssembling, Linking, and Debugging ProgramsDefining DataDefining Symbolic ConstantsData-Related Operators and DirectivesNext . . .Basic Elements of Assembly LanguageFlat Memory Program TemplateExample: Adding and Subtracting IntegersAssembling, Linking, and Debugging ProgramsDefining DataData-Related Operators and Directives