46
8086 Assembly Language Programming I Week 4 – Overview of the x86 assembly language prgoramming

Aaa

Embed Size (px)

DESCRIPTION

8086

Citation preview

  • 8086 Assembly Language Programming IWeek 4 Overview of the x86 assembly language prgoramming

  • Assembly language programmingLearning assembly language programming will help understanding the operations of the microprocessorTo learn:Need to know the functions of various registers Need to know how external memory is organized and how it is addressed to obtain instructions and data (different addressing modes)Need to know what operations (or the instruction set) are supported by the CPU. For example, powerful CPUs support floating-point operations but simple CPUs only support integer operations*

  • How to learn programmingC Concept L Logic thinkingP Practice Concept we must learn the basic syntax, such as how a program statement is writtenLogic thinking programming is problem solving so we must think logically in order to derive a solutionPractice write programs*

  • Assembly ProgramThe native language is machine language (using 0,1 to represent the operation)A single machine instruction can take up one or more bytes of codeAssembly language is used to write the program using alphanumeric symbols (or mnemonic), eg ADD, MOV, PUSH etc.The program will then be assembled (similar to compiled) and linked into an executable program. The executable program could be .com, .exe, or .bin files*

  • Flow of program developmentProgram.asmObject file.objExecutable file .exeAssemblelink*

  • ExampleMachine code for mov AL, 00H B4 00 (2 bytes)After assembled, the value B400 will be stored in the memoryWhen the program is executed, then the value B400 is read from memory, decoded and carry out the task*

  • Assembly Program Each instruction is represented by one assembly language statement The statement must specify which operation (opcode) is to be performed and the operandsEg ADD AX, BX ADD is the operationAX is called the destination operand BX is called the source operandThe result is AX = AX + BXWhen writing assembly language program, you need to think in the instruction level*

  • ExampleIn c++, you can do A = (B+C)*100In assembly language, only one instruction per statementA = B; only one instruction - MOVEA = A+C; only one instruction - ADDA = A*100; only one instruction - Multiply*

  • Format of Assembly languageGeneral format for an assembly language statementLabelInstructionCommentStart: MovAX, BX ; copy BX into AX

    Start is a user defined name and you only put in a label in your statement when necessary!!!!The symbol : is used to indicate that it is a label*

  • 8086 Software Model*

  • Software modelIn 8086, memory is divided into segmentsOnly 4 64K-byte segments are active and these are: code, stack, data, and extra

    When you write your assembly language program for an 8086, theoretically you should define the different segments!!!

    To access the active segments, it is via the segment register: CS (code), SS (stack), DS (data), ES (extra)So when writing assembly language program, you must make use of the proper segment register or index register when you want to access the memory

    *

  • RegistersIn assembly programming, you cannot operate on two memory locations in the same instruction So you usually need to store (move) value of one location into a register and then perform your operation After the operation, you then put the result back to the memory locationTherefore, one form of operation that you will use very frequent is the store (move) operation!!!And using registers!!!!!*

  • ExampleIn C++ A = B+C ; A, B, C are variablesIn assembly language A,B, C representing memory locations so you cannot do A = B+C MOV AL, B ; move value of B into AL registerADD, AL, C ; do the add AL = AL +CMOV A, AL ; put the result to A*

  • Data registersAX, BX, CX,and DX these are the general purpose registers but each of the registers also has special functionExample AX is called the accumulator to store result in arithmetic operationsRegisters are 16-bit but can be used as 2 8-bit storageEach of the 4 data registers can be used as the source or destination of an operand during an arithmetic, logic, shift, or rotate operation. In some operations, the use of the accumulator is assumed, eg in I/O mapped input and output operations

    *

  • Data registerIn based addressing mode, base register BX is used as a pointer to an operand in the current data segment. CX is used as a counter in some instructions, eg. CL contains the count of the number of bits by which the contents of the operand must be rotated or shifted by multiple-bit rotateDX, data register, is used in all multiplication and division, it also contains an input/output port address for some types of input/output operations*

  • Pointer and index registersStack is used as a temporary storageData can be stored by the PUSH instruction and extracted by the POP instructionStack is accessed via the SP (Stack Pointer) and BP (Base Pointer)The BP contains an offset address in the current stack segment. This offset address is employed when using the based addressing mode and is commonly used by instructions in a subroutine that reference parameters that were passed by using the stack

    *

  • Pointer and Index RegisterSource index register (SI) and Destination index register (DI) are used to hold offset addresses for use in indexed addressing of operands in memoryWhen indexed type of addressing is used, then SI refers to the current data segment and DI refers to the current extra segmentThe index registers can also be used as source or destination registers in arithmetic and logical operations. But must be used in 16-bit mode*

  • Data typesData can be in three forms: 8-bit, 16-bit, or 32-bit (double word)Integer could be signed or unsigned and in byte-wide or word-wideFor signed integer (2s complement format), the MSB is used as the sign-bit (0 for positive, 1 for negative)Signed 8-bit integer 127 to 128, For signed word 32767 to 32768Latest microprocessors can also support 64-bit or even 128-bit dataIn 8086, only integer operations are supported!!!*

  • A sample program.code ; indicate start of code segment.startup ; indicate start of programmovAX, 0 mov BX, 0000Hmov CX, 0mov SI, AXmov DI, AXmov BP, AXEND; end of fileThe flow of the program is usually top-down and instructions are executed one by one!!!*

  • Assembly programmingIn general, an assembly program must include the code segment!!Other segments, such as stack segment, data segment are notcompulsory

    There are key words to indicate the beginning of a segment aswell as the end of a segment. Just like using main(){} in C++ Programming

    ExampleDSEG segment data ; define the start of a data segment

    DSEG ENDS ; defines the end of a data segmentSegment is the keyword DSEG is the name of the segmentSimilarly key words are used to define the beginning of a program,as well as the end.

    *

  • Assembly language programmingExample

    CSEG segment codeSTART PROC FAR ; define the start of a program (procedure)

    RET ; return START ENDP ; define the end of a procedureCSEG ends End start; end of everything

    Different assembler may have different syntax for the definitionof the key words !!!!!Start is just a name it could be my_prog, ABC etc*

  • More sampleStacksg segment para stack .; define the stack segmentStacksg endsDatasg segment para ; declare data inside the data segmentDatasg endsCodesg segment para codeMain proc far; assume ss:stacksg, ds: datasg, cs:codesgmov ax, datasgmov ds, ax.mov ax, 4c00Hint 21HMain endpCodesg endsend mainEnd of everything*

  • DefinitionsTo declare a segment, the syntax is:segment_name SEGMENT alignment classExample Stacksg segment PARA (this statement is used in previous slide)PARA define the alignment of the segment base address, the segment with a starting addressing that is evenly Divisible by 16. But the default value is also base address divisible by 16 so the key word PARA can be ignored!

    *

  • Definitiondata, code class entry. Is used to group related segments when linking. The linker automatically groups segments of the same class in memory PROC define procedures (similar to a function) inside the code segment. Each procedure must be identified by an unique name. At the end of the procedure, you must include the ENDP

    *

  • DefinitionsFAR is related to program execution. When you request execution of a program, the program loader uses this procedure as the entry point for the first instruction to execute.Assume to associate, or to assign, the name of a segment with a segment registerIn some assembler, you need to move the base address of a segment directly into the segment register!!!END ends the entire program and appears as the last statement. Usually the name of the first or only PROC designated as FAR is put after END*

  • Syntax of a simple assembly language programIf you are doing something simple then you do not need to define the segment Everything will be stored in the code segment *

  • start: mov DL, 0H; move 0H to DL mov CL, op1; move op1 to CL mov AL, data; move data to AL step: cmp AL, op1 ; compare AL and op1 jc label1; if carry =1 jump to label1 sub AL, op1; AL = AL op1 inc DL ; DL = DL+1 jmp step ; jump to step label1: mov AH, DL; move DL to AH HLT ; Halt end of programdata db 45 ; define a variable called dataop1 db 6; define a variable called op1*

  • Assembler for 8086

    Emu8086 (http:// www.emu8086.com) there is a trial version but it does not support all the features such as interruptThe emu8086 consists of a tutorial and the reference for a complete instruction set

    Keil www.keil.com*

  • Defining data in a programData is usually stored in the data segment You can define constants, work areas (a chunk of memory )Data can be defined in different length (8-bit, 16-bit)8-bit then use DB16-bit then use DW

    The definition for data:

    [name] Dn expression ; Dn is either DB or DW

    Name a program that references a data item by means of a name. The name of an item is otherwise optional Dn this is called the directives. It defines length of the dataExpression define the values (content) for the data*

  • Examples for data FLDA DB ? ; define an uninitialized item called FLDA 8-bit

    FLDBDB25 ; initialize a data to 25

    Define multiple data under the same name (like an array)

    FLDCDB21, 22, 23, 34 ; the data are stored in adjacent bytes

    FLDC stores the first value FLDC + 1 stores the second value

    You can do mov AL, FLDC+3*

  • Example for data definitionDUP duplicate DUP can be used to define multiple storagesDB 10 DUP (?) ; defines 10 bytes not initializeDB 5 DUP (12) ; 5 data all initialized to 12

    String : DB this is a test

    EQU this directive does not define a data item; instead, it definesa value that the assembler can use to substitute in other instructions(similar to defining a constant in C programming or using the #define )factor EQU 12movCX, factor

    *

  • Assembly ProgramAssembly language should be more effective and it will take up less memory space and run fasterIn real-time application, the use of assembly program is required because program that is written in a high-level language probably could not respond quickly enoughYou can also put assembly codes into your C++ program in order to reduce the execution time!!!!*

  • Assembly language programmingThe syntax for different microprocessor may be different but the concept is the same so once you learn the assembly programming for one microprocessor, you can easily program other kinds of systemFor example, programming the 8051 series is very similar to the 8086

    *

  • Addressing ModesFunction of the addressing modes is to access the operandsAvailable modes (9 modes): register addressing, immediate addressing, direct addressing, register indirect addressing, based addressing, indexed addressing, based indexed addressing, string addressing, and port addressing Addressing modes provide different ways of computing the address of an operand *

  • Why addressing mode is important?In c++, you can define an array, or a variable int x[10], y, *z; Then to access different elements, you can do Z = x ; *(x+2); x[0] = y

    How this can be done using assembly language programming? This is via different addressing modes!!!!*

  • Register addressing modeThe operand to be accessed is specified as residing in an internal register of the 8086EgMOVAX, BXMove (MOV) contents of BX (the source operand), to AX (the destination operand) Both operands are in the internal registers*

  • *

  • Pay attention to the value of IP and content of AX, BX*

  • Immediate addressing modeSource operand is part of the instruction Usually immediate operands represent constant dataThe operands can be either a byte or word e.g MOVAL, 1515 is a byte wide immediate source operandOr it could be MOV AL, #15The immediate operand is stored in program storage memory (i.e the code segment)This value is also fetched into the instruction queue in the BIUNo external memory bus cycle is initiated!*

  • *

  • *

  • Direct addressing modeMove a byte or word between a memory location and a registerthe locations following the instruction opcode hold an effective memory address (EA) instead of dataThe address is a 16-bit offset of the storage location of the operand from the current value in the data segment registerPhyscial address = DS + offsetThe instruction set does not support a memory-to-memory transfer!*

  • Direct addressingData is assumed to be stored in the data segment so DS is used in calculating the physical address!!!External memory bus cycle is needed to do the readExample of direct addressing: mov AL, var1Where Var1 can be regarded as a variable

    *

  • Register indirect addressing modeTransfer a byte or word between a register and a memory location addressed by an index or base registerExample MOV AL, [SI]SI index registerThe symbol [] always refer to an indirect addressingThe effective address (EA) is stored either in a pointer register or an index registerThe pointer register can be either base register BX or base pointer register BPThe index register can be source index register SI, or destination index register DIThe default segment is either DS or ES*

  • Register indirect addressingEg MOVAX, [SI]Value stored in the SI register is used as the offset addressThe segment register is DS in this exampleMeaning of the above is to move the data stored in the memory location : DS + SI to the AX registerIn register indirect addressing mode, the EA (effective address) is a variable and depends on the index, or base register value Eg mov [BX], CLWhich segment register will be used for the above operation*

  • According to the memory mapThe result of the operation Mov [BX], CL will result in what???If CL = 88 and BX = 1233H and DS =0H

    Physical address = DS + BX = 01233H*

    Address (in HEX)Content

    01236190123518012342001233

    *********************************************