intro to assembly language

Click here to load reader

Upload: harvey-james-chua

Post on 17-Jan-2016

6 views

Category:

Documents


1 download

DESCRIPTION

Assembly Language

TRANSCRIPT

Introduction to Assembly language programmingPage3

Levels of Programming Languages1) Machine LanguageConsists of individual instructions that will be executed by the CPU one at a time2) Assembly Language (Low Level Language)Designed for a specific family of processors (different processor groups/family has different Assembly Language) Consists of symbolic instructions directly related to machine language instructions one-for-one and are assembled into machine language.3) High Level Languagese.g.: C, C++ and VbasicDesigned to eliminate the technicalities of a particular computer.Statements compiled in a high level language typically generate many low-level instructions.Advantages of Assembly Language1. Shows how program interfaces with the processor, operating system, and BIOS. 2. Shows how data is represented and stored in memory and on external devices.3. Clarifies how processor accesses and executes instructions and how instructions access and process data.4. Clarifies how a program accesses external devices.Reasons for using Assembly Language1. A program written in Assembly Language requires considerably less memory and execution time than one written in a high level language.2. Assembly Language gives a programmer the ability to perform highly technical tasks that would be difficult, if not impossible in a high-level language.3. Although most software specialists develop new applications in high-level languages, which are easier to write and maintain, a common practice is to recode in assembly language those sections that are time-critical.4. Resident programs (that reside in memory while other program execute) and interrupt service routines (that handle input and output) are almost always develop in Assembly Language.Disadvantages of using Assembly Language1. Need to know detail hardware implementation2. Not portable3. Slow to development and difficult to debugBasic components in assembly Language: Instruction Directive Label CommentAssembly Language StatementsThree types of statements in assembly languageTypically, one statement should appear on a line1. Executable Instructions-Generate machine code for the processor to execute at runtime-Instructions tell the processor what to do2. 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 set3. Macros-Shorthand notation for a group of statements-Sequence of instructions, directives, or other macros Constants Integer ConstantsExamples: 10, 42d, 10001101b, 0FF3Ah, 777oRadix: b = binary, d = decimal, h = hexadecimal, and o = octalIf no radix is given, the integer constant is decimalA hexadecimal beginning with a letter must have a leading 0 Character and String ConstantsEnclose character or string in single or double quotesExamples: 'A', "d", 'ABC', "ABC", '4096'Embedded quotes: "single quote ' inside", 'double quote " inside'Each ASCII character occupies a single byteInstructions Assembly language instructions have the format:[label:] mnemonic [operands] [;comment] Instruction Label (optional)Marks the address of an instruction, must have a colon :Used to transfer program execution to a labeled instruction MnemonicIdentifies 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, or constantsInstructions Examples No operandsstc; set carry flag One operandinc ax; increment register axcall Clrscr; call procedure Clrscrjmp L1; jump to instruction with label L1 Two operandsadd ebx, ecx ; register ebx = ebx + ecxsub var1, 25; memory variable var1 = var1 - 25 Three operandsimul eax,ebx,5 ; register eax = ebx * 5

Identifiers Identifier is a programmer chosen name Identifies variable, constant, procedure, code label May contain between 1 and 247 characters Not case sensitive First character must be a letter (A..Z, a..z), underscore(_), @, ?, or $. Subsequent characters may also be digits. Cannot be same as assembler reserved word.

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

RegistersRegisters are used to control instructions being executed, to handle addressing of memory, and to provide arithmetic capabilityRegisters of Intel Processors can be categorized into:1. Segment register2. Pointer register3. General purpose register4. Index register5. Flag register

General Purpose Registers1. AX register Acts as the accumulator and is used in operations that involve input/output and arithmetic 2. BX Register Known as the base register since it is the only this general purpose register that can be used as an index to extend addressing. This register also can be used for computations BX can also be combined with DI and SI register as a base registers for special addressinglike AX, BX is also consists of EBX, BH and BL3. CX Register known as count register may contain a value to control the number of times a loops is repeated or a value to shift bits left or right CX can also be used for many computations 4. DX Register Known as data register Some I/O operations require its use Multiply and divide operations that involve large values assume the use of DX and AX together as a pair to hold the data or result of operation. Number of bits and the fractions of the register is as below :

TITLE and .MODEL Directives.TITLE line (optional)Contains a brief heading of the program and the disk file name.MODEL directiveSpecifies the memory configurationFor our purposes, the FLAT memory model will be usedLinear 32-bit address space (no segmentation)STDCALL directive tells the assembler to use Standard conventions for names and procedure callsTITLE and .MODEL Directives.686 processor directiveUsed before the .MODEL directiveProgram can use instructions of Pentium P6 architectureAt least the .386 directive should be used with the FLAT model.STACK directiveTells the assembler to define a runtime stack for the programThe size of the stack can be optionally specified by this directiveThe runtime stack is required for procedure calls.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 memory.INCLUDE directiveCauses the assembler to include code from another filePROC 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 procedure

Prepared by: Engr. Noviel A. Villamor