Overview of Assembly Language Programming in 8086 Architecture

Slide Note
Embed
Share

Assembly language programming in the 8086 architecture involves writing case-sensitive instructions represented by statements. Each statement corresponds to an 8086 instruction or an assembler directive. Instructions follow a specific format with optional components like labels and comments. Assemblers like Intel ASM and Microsoft MASM are commonly used for development, enabling the creation of .com and .exe files. Data in programs is defined in the data segment with directives specifying the name, length, and content. Various pseudo-ops are used for defining data sizes.


Uploaded on Sep 28, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Assembly Language Programming Bilal Zahoor Kashmir University North Campus

  2. 8086 Assembly language The language is case sensitive (MOV or mov is same). Each instruction is represented by one assembly language statement. There may be only one statement/line. A statement may start in any column. A statement is either An 8086 instruction or An Assembler directive: provides directions to the assembler program.

  3. Format of 8086 Instruction General format for an assembly language statement [Label:] Mnemonic [Operand1 [ ,Operand2] ] [;Comment] Start: MOV AX, BX ; copy BX into AX INC CX ; increment CX RET ; return from procedure The entities enclosed with in square brackets are optional. 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. The symbol ; is used to indicate that it is a comment.

  4. Format of an Assembler directive General format for an assembler directive [Name] Pseudo-Op [Operands] [;Comment] DB 5 ;define temp as a byte variable initialized with value 5 temp myProcedure PROC ;beginning of a procedure (myProcedure). myProcedure ENDP ;end of a procedure (myProcedure). Name is used for Variables, Procedure name etc. Pseudo-ops qualify Name as segment name, macro-name and so on. There are various Pseudo-ops used by assemblers, they have different names in different assemblers.

  5. ALP Development The most common assemblers for Assembly language programming development are: Intel ASM Microsoft MASM (MACRO Assembler). Works on Microsoft Platforms (Windows and DOS). Produces only .Com and .exe files. Borland TASM (Turbo Assembler). 16-bit and 32-bit. Supports all platforms.

  6. Defining Data in a program Data is usually stored in the data segment. You can define constants, work areas (a chunk of memory). Data can be defined in different lengths (8-bit, 16-bit, 32-bit and so on) Each byte of character is stored as its ASCII value in hexadecimal The definition for data: <Name> DX <Expression> Name name of data segment. DX this is called the directives. It defines length of the data. Expression defines the value (content) of the data segment.

  7. Defining Data in a program(CONT..) Pseudo-op Description Data size (in bytes) DB Define Byte 1 DW Define Word 2 DD Define Double word 4 DQ Define Quad-word 8 DT Define Ten bytes 10 Examples: var DW 1234H ;define var as word and initialize with value 1234(in Hex). var DB ? ;define var as Byte and its initial value is unknown.

  8. Defining Arrays Initialized Array: arr DB a , b , 2 ;initialized array of 3 elements, size of each element is 1 byte arr DB 20 dup(5) ;arr is byte array of 20 elements all initialized to value 5 Uninitialized Array: arr DB 20 dup(?) ;arr is uninitialized byte array of 20 elements DUP duplicate can be used to define multiple storages. arr stores the first value, arr + 1 stores the second value and so on. E.g Mov Al, [arr+1] ; [] used for dereferencing, store value at arr+1 in Al

  9. Named constant Directive EQU this directive does not define a data item; instead, it defines a value that the assembler can use to substitute in other instructions (similar to defining a constant in C programming or using the #define ) FACT EQU 12 ;defines FACT as a named constant MOV CX, FACT No memory is allocated. ;without dereferencing Strings are also possible.

  10. User defined Data definition directives Structure: Struct myStruct ;declares myStruct as a structure var1 DB 0 ; Var1 data byte initialized with 0 var2 DB 1 Ends myStruct Structure variable: structVar myStruct ? ;creates structure variable Acceccing structure: MOV [structVar.var1], 20 ;move 20 in var1 in mystruct ; Var2 data byte initialized with 1

  11. Segment Directive The SEGMENT directive identifies the start of a memory segment and ENDS identifies the end of a segment when full-segment definitions are in use. Syntax: <logical-segment_name> SEGMENT .. .. <logical-segment_name> ENDS E.g mySegment SEGMENT Mov Ax,BX .. .. mySegment ENDS

  12. Assume Directive The ASSUME statement tells the assembler what names have been chosen for the code, data, extra, and stack segments. Without the ASSUME statement, the assembler assumes nothing and automatically uses a segment override prefix on all instructions that address memory data. The ASSUME statement is only used with full-segment definitions Syntax: ASSUME <Physical-Segment>:<logica-segment_name> E.g: mySegment SEGMENT ASSUME CS : mySegment ;Code segment is initialized to mySegment Mov AX,BX mySegment ENDS

  13. Memory Organization The assembler uses two basic formats for developing software: 1. Using Memory Models: Memory models are unique to the MASM assembler program. The TASM assembler also uses memory models, but they differ somewhat from the MASM models. The models are easier to use for simple tasks. 2. Using full-segment definitions: The full-segment definitions are common to most assemblers, including the Intel assembler, and are often used for software development. The full-segment definitions offer better control over the assembly language task and are recommended for complex programs.

  14. Simplified Segment Directives .MODEL The .MODEL statement followed by the size of the memory system designates the Memory Model. .MODEL SMALL ;select small model .CODE E.g Designates the beginning of the CODE segment in the program. .DATA Designates the beginning of the DATA segment in the program .STACK Defines STACK segment in the program. Syntax : .STACK [memory-size] ;memory-size is optional Default memory size for stack segment is 1KB. Initializes Stack Segment(SS), Stack Pointer(SP) and Base Pointer(BP).

  15. Simplified Segment Directives .STARTUP Designates the start of the execution of the problem (similar to main() in C). Loads DATA segment and EXTRA segment. If the .STARTUP directive is used, the MOV AX,@DATA followed by MOV DS,AX statements can be eliminated. The .STARTUP directive also eliminates the need to store the starting address next to the END label. .EXIT The .EXIT 0 directive returns to DOS with an error code of 0 (no error). If no parameter is added to .EXIT, it still returns to DOS, but the error code is not defined. END: End of the FIle.

  16. Memory Model MODEL Description TINY All data and code must fit into a single 64K-byte memory segment. Tiny programs assemble as DOS.COM files and must use an origin at 0100H for the code. SMALL A two-segment model with a single code segment and a single data segment. Small programs generate DOS.EXE files and have an origin of 0000H. FLAT The flat model uses a single segment of up to 4G bytes in length. Flat programs are programs that will only function in Windows with an origin of 00000000H. All models except TINY model assemble as .exe Data segment(DS) and Extra Segment (ES) need to be initialized explicitly by programmer

  17. Standard I/O DOS function calls are used for Standard input/output in Assembly language(8086). To use a DOS function call in a DOS program, Place the function number in AH (8 bit register) and other data that might be necessary in other registers. 1. Once everything is loaded, execute the INT 21H instruction to perform the task. 2. After execution of a DOS function, it may return results in some specific registers. 01H: Read the Keyboard This function waits until a character is input from the keyboard. Returns ASCII key code of character in AL register. E.g: MOV AH,01H ;load DOS function number in AH INT 21H ;access DOS ;returns with AL = ASCII key code

  18. Standard I/O (CONT..) 02H: Write to Standard Output device This function displays single character on the video display. ASCII key code of the character to be displayed must be loaded in DL register. E.g MOV DL, A ;load ASCII key code of Character A in DL MOV AH,02H ;load DOS function number in AH INT 21H ;access DOS

  19. Standard I/O (CONT..) 09H: Display a character String This function displays a character string on the video display. The character string must end with an ASCII of symbol $ (24H). The character string can be of any length and may contain control characters such as carriage return (0DH) and line feed (0AH). DX must contain address of the character string. E.g Buf DB Hello World$ ;define character string MOV DX, offset Buf ;load address of the string in DX, offset gives address of the Buf. ;load DOS function number in AH ;access DOS MOV AH,09H INT 21H

  20. Standard I/O (CONT..) 0AH: Buffered keyboard input This function continues to read the keyboard (displaying data as typed) until either the specified number of characters are typed or until the enter key is typed. The first byte of the buffer contains the size of the buffer (up to 255). The second byte is filled with the number of characters typed upon return. The third byte through the end of the buffer contains the character string typed, followed by a carriage return (0DH). Buffer e.g: Buf DB 13, 10, Welcome$ E.g Buf DB 10, ?, 10 dup(0) ;declare a buffer. MOV DX, offset Buf ;load address of the buffer in DX, offset gives address of the Buf. MOV AH,0AH ;load DOS function number in AH INT 21H ;access DOS

  21. Procedure/Subroutines Procedures can be declared within the .CODE (code segment). However, ensure that a procedure is not executed without explicit invocation. Use CALL instruction to call the procedure. Eg. CALL procedure-name Example: .MODEL SMALL .DATA msg DB Hello World$ .STACK .CODE myProc PROC MOV DX, offset msg MOV AH, 09H INT 21H RET myProc ENDP .STARTUP CALL myProc .EXIT END

  22. Procedure/Subroutines The PROC and ENDP directives indicate the start and end of a procedure (subroutine). Both the PROC and ENDP directives require a label to indicate the name of the procedure. Syntax: <Procedure-name> PROC ;starts procedure .. ;body of the procedure .. RET <Procedure-name>ENDP ;close of the procedure When returning from procedures, use RET instruction.

  23. Macros A macro is a group of instructions that perform one task, just as a procedure performs one task. The difference is that a procedure is accessed via a CALL instruction, whereas a macro, and all the instructions defined in the macro, is inserted in the program at the point of usage. Creating a macro is very similar to creating a new opcode, which is actually a sequence of instructions, in this case, that can be used in the program. You type the name of the macro and any parameters associated with it, and the assembler then inserts them into the program. Macro sequences execute faster than procedures because there is no CALL or RET instruction to execute. The instructions of the macro are placed in your program by the assembler at the point where they are invoked.

  24. Macros (CONT..) The MACRO and ENDM directives delineate a macro sequence. The first statement of a macro is the MACRO instruction, which contains the name of the macro and any parameters associated with it. An example is MOVE MACRO A,B which defines the macro name as MOVE. This new pseudo opcode uses two parameters: A and B. The last statement of a macro is the ENDM instruction, which is placed on a line by itself. Never place a label in front of the ENDM statement. If a label appears before ENDM, the macro will not assemble. Syntax: <Macro-name> MACRO [<Arg 1> <,Arg 2> <,Arg n>] .. ENDM

  25. Macros (CONT..) Example; Adding two numbers using Macro .MODEL SMALL ;define Memory Model SUM MACRO X,Y MOV AX, X MOV BX, Y ADD AX, BX ENDM .DATA .STACK .CODE .STARTUP SUM 5,10 .EXIT END ;Macro definition SUM, X and Y are parameters ;Move value of X in AX ;Move Value of Y in BX ;Add values of X and Y ;end of macro ;start of data segment ;start of stack segment ;start of code segment ;start of execution ;call Macro with Parameters 5 and 10 ;return to DOS ;end of program file

  26. Modular Programming Many programs are too large to be developed by one person. This means that programs are routinely developed by teams of programmers. The Linker program is used so that programming modules can be linked together into a complete program. The assembler program converts a symbolic source module (file) into a hexadecimal object file. The linker program, reads the object files that are created by the assembler program and links them together into a single execution file. An execution file is created with the file name extension EXE. If a file is short enough (less than 64K bytes long), it can be converted from an execution file to a command file (.COM). The command file is slightly different from an execution file in that the program must be originated at location 0100H before it can execute.

  27. Modular Programming (CONT..) Object file1 .obj Executable file .exe Program .asm Object file2 .obj Linker Assembler Object file3 .obj

  28. Modular Programming (CONT..) The PUBLIC and EXTRN directives are very important to modular programming because they allow communications between modules. We use PUBLIC to declare that labels of code, data, or entire segments are available to other program modules. EXTRN (external) declares that labels are external to a module. The PUBLIC directive is placed in the opcode field of an assembly language statement to define a label as public, so that the label can be used (seen by) by other modules. The label declared as public can be a jump address, a data address, or an entire segment. When segments are made public, they are combined with other public segments that contain data with the same segment name.

  29. Modular Programming (CONT..) Placing MACRO Definitions in Their Own Module: Macro definitions can be placed in the program file, or they can be placed in their own macro module. A file can be created that contains only macros to be included with other program files. We use the INCLUDE directive to indicate that a program file will include a module that contains external macro definitions. When macro sequences are placed in a file (often with the extension INC or MAC), they do not contain PUBLIC statements as does a library.

  30. Modular Programming (CONT..) Example MyFun.inc file .MODEL SMALL SUM MACRO X,Y MOV AX, X MOV BX, Y ADD AX, BX ENDM .DATA .STACK .CODE INCLUDE MyFun.inc ;include myFun file .STARTUP SUM 5,10 .EXIT END

  31. Conditional Assembly (or Controlled Expansion) Conditional assembly language statements allow portions of a program to be assembled if a condition is met. These are useful for tailoring software to an application Following table shows the forms used for IF statement in the conditional assembly. Statement Function IF IFB IFE IFDEF IFNB IFNDEF IFIDN IFDIFWWW If the expression is true If argument is blank If the expression is not true If the label has been defined If argument is not blank If the label has not been defined If argument 1 equals argument 2 If argument 1 does not equal to argument 2

More Related Content