Assembly Language Programming - PDF

Please download to get full document.

View again

of 27
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Information Report
Category:

BusinessLaw

Published:

Views: 2 | Pages: 27

Extension: PDF | Download: 0

Share
Related documents
Description
Assembly Language Programming 1 DIRECTIVES A given Assembly language program is a series of statements Assembly language instructions MOV AX, 0F8E1H ADD AX, BX Directives (pseudo-instructions) give directions
Transcript
Assembly Language Programming 1 DIRECTIVES A given Assembly language program is a series of statements Assembly language instructions MOV AX, 0F8E1H ADD AX, BX Directives (pseudo-instructions) give directions to the assembler about how it should translate the Assembly language instructions into machine code. An Assembly language instruction consists of four fields: [label:] mnemonic [operands] [;comment] 2 Fields of Assembly language instructions [label:] mnemonic [operands] [;comment] Brackets indicate that the field is optional and should not be typed The label field allows the program to refer to a line of code by name. The label field cannot exceed 31 characters. Labels for directives do not need to end with a colon. A label must end with a colon when it refers to an opcode generating instruction The colon indicates to the assembler that this refers to code within this code segment. The Assembly language mnemonic and operand (s) fields together perform the real work of the program and accomplish the tasks for which the program was written. ADD AL,BL ; ADD is the mnemonic and AL,BL are operands MOV AX,6764 ; MOV is the mnemonic and AX,6764 are operands The comment field begins with a ; they be used to describe the program, to make it easier for someone to read and understand. 3 SEGMENT Definition Label SEGMENT [options] Label ENDS ; a statement belonging to this segment here label ; a statement belonging to this segment here label ; a statement belonging to this segment here label The SEGMENT and ENDS directives indicate to the assembler the beginning and ending of a segment. Assembly language statements are grouped into segments in order to be recognized by the assembler and consequently by the CPU. The stack segment defines storage for the stack The data segment defines the data that the program will use The code segment contains the Assembly language instructions. 4 The Form of an Assembly Language Program STSEG STSEG DTSEG DTSEG CDSEG CDSEG END SEGMENT DB 64DUP(?) ENDS SEGMENT DATA1 DB 52H DATA2 DB 29H SUM DB? ENDS SEGMENT MAIN PROC FAR ASSUME CS:CDSEG, DS:DTSEG, SS:STSEG MOV AX,DTSEG MOV DS,AX MOV AL,DATA1 MOV BL,DATA2 ADD AL,BL MOV SUM,AL MOV AH,4CH INT 21H MAIN ENDP ENDS MAIN ;this is the program exit point 5 Stack Segment definition STSEG SEGMENT STSEG ENDS DB 64 DUP (?) DB directive reserves 64 bytes of memory for the stack. SEGMENT directive begins the segment ENDS directive ends the segment 6 Data Segment Definition DTSEG SEGMENT DATA1 DB 52 DATA1 DB 29 SUM DB? DTSEG ENDS The data segment defines three data items DATA1, DATA2, and SUM The DB directive is used by the assembler to allocate memory in bytesized chunks. The data items defined in the data segment will be accessed in the code segment by their labels. DATA1 and DATA2 are given initial values in the data section. SUM is not given an initial value, but storage is set aside for later use by the program. 7 Code Segment Definition CDSEG SEGMENT MAIN PROC FAR ASSUME CS:CDSEG, DS:DTSEG, SS:STSEG MOV AX,DTSEG MOV DS,AX MOV AL,DATA1 The first line of the segment after the SEGMENT directive is the PROC directive. A procedure is a group of instructions designed to accomplish a specific function. Every procedure must have a name defined by the PROC directive, followed by the assembly language instructions and closed by the ENDP directive. The PROC and ENDP statements must have the same label. The PROC directive may have the option FAR or NEAR. The operating system that controls the computer must be directed to the beginning of the program in order to execute it. 8 ASSUME Directive ASSUME CS:CDSEG, DS:DTSEG, SS:STSEG The ASSUME directive associates segment registers with specific segments by assuming that the segment register is equal to the segment labels used in the program. If an extra segment had been used, ES would also be included in the ASSUME statement. The ASSUME statement is needed because a given Assembly language program can have several code segments, one or two or three or more data segments and more than one stack segment, but only one of each can be addressed by the CPU at a given time since there is only one of each of the segment registers available inside the CPU. ASSUME directive tells the assembler which of the segments defined by the SEGMENT directives should be used. ASSUME directive also helps the assembler to calculate the offset addresses from the beginning of that segment. For example, in MOV AL,[BX] the BX register is the offset of the data segment. 9 ASSUME What value is actually assigned to the CS, DS, and SS registers for execution of the program? The operating system must pass control to the program so that it may execute, but before it does that it assigns values for the segment registers. The operating system must do this because it knows how much memory is installed in the computer, how much of it is used by the system, and how much is available. One cannot tell DOS to give the program a specific area of memory, say from 25FFF to 289E2. Therefore, it is the job of DOS to assign exact values for the segment registers. Upon taking control from DOS, of the three segment registers, only CS and SS have the proper values. The DS value (and ES, if used) must be initialized by the program. This is done as follows: MOVAX,DTSEG MOV DS,AX 10 Return Control to the Operating System MOV AH,4CH INT 21H Their purpose is to return control to the operating system. 11 The Form of an Assembly Language Program STSEG SEGMENT DB 64DUP(?) STSEG ENDS DTSEG DTSEG SEGMENT ;place data here ENDS CDSEG SEGMENT MAIN PROC FAR ;this is the program entry point ASSUME CS:CDSEG, DS:DTSEG, SS:STSEG MOV AX,DTSEG ;bring in the segment for data MOV DS,AX ;assign the DS value ;place code here MOV AH,4CH INT 21H MAIN ENDP CDSEG ENDS END MAIN 12 DATA TYPES AND DATA DEFINITION The assembler supports all the various data types of the 80x86 microprocessor by providing data directives that define the data types and set aside memory for them. The 8088/86 microprocessor supports many data types, but none are longer than 16 bits wide since the size of the registers is 16 bits. It is the job of the programmer to break down data larger than 16 bits to be processed by the CPU. The data types used by the 8088/86 can be 8-bit or 16-bit, positive or negative. If a number is less than 8 bits wide, it still must be coded as an 8-bit register with the higher digits as zero. Similarly, if the number is less than 16 bits wide it must use all 16 bits, with the rest being 0s. 13 ORG (origin) Directive ORG is used to indicate the beginning of the offset address. The number that comes after ORG can be either in hex or in decimal. The ORG directive is used extensively in the data segment to separate fields of data to make it more readable for the student, it can also be used for the offset of the code segment (IP). 14 DB (define byte) Directive The DB directive allows allocation of memory in byte-sized chunks. DB can be used to define numbers in decimal, binary, hex, and ASCII. DATA1 DB 25D ; DECIMAL DATA2 DB B ; BINARY DATA3 DB 12H ; HEX DATA4 DB M ; ASCII Regardless of which one is used, the assembler will convert them into hex. ORG 0010H DATA DB 2591 DATA5 DB? DATA6 DB My name is Ahmed ;ASCII CHARACTERS M y DUP (duplicate) Directive DUP is used to duplicate a given number of characters to avoid a lot of typing. For example, contrast the following two methods of filling six memory locations with FFH: ORG 0030H DATA7 DB 0FFH, 0FFH, 0FFH, 0FFH, 0FFH, 0FFH ;fill 6 bytes with FF ORG 38H DATA8 DB 6 DUP(0FFH) ;fill 6 bytes with FF Reserve 32 bytes of memory with no initial value given ORG 40H DATA9 DB 32DUP(?) ;set aside 32 bytes DUP can be used inside another DUP DATA10 DB 5 DUP (2 DUP (99)) ; fill 10 bytes with 99 16 DW (define word) Directive DW is used to allocate memory 2 bytes (one word) at a time. DW is used widely in the 8088/8086 and microprocessors since the registers are 16 bits wide. ORG 70H DATA11 DW 954 DATA12 DW B DATA13 DW 253FH DATA14 DW 9,2,7,0CH, B,5, HI DATA15 DW 8 DUP(?) BA F C I H EQU (equate) Directive This is used to define a constant without occupying a memory location. EQU does not set aside storage for a data item but associates a constant value with a data label so that when the label appears in the program, its constant value will be substituted for the label. EQU can also be used outside the data segment, even in the middle of a code segment. Using EQU for the counter constant in the immediate addressing mode: COUNT EQU 25 MOV CX,COUNT MOV CX, 25 ; the register CX will be loaded with the value 25. This is in contrast to using DB: When executing the same instruction MOV CX,COUNT it will be in the direct addressing mode. Now what is the real advantage of EQU? First, note that EQU can also be used in the data segment: COUNT EQU 25 COUNTER1 DB COUNT For a constant (a fixed value) used in many different places in the data and code segments. 18 DD (define double word) Directive The DD directive is used to allocate memory locations that are 4 bytes (two words) in size. Again, the data can be in decimal, binary, or hex. In any case the data is converted to hex and placed in memory locations according to the rule of low byte to low address and high byte to high address. ORG 00A0H DATA16 DD 1023 ;DECIMAL DATA17 DD B ;BINARY DATA18 DD 5C2A57F2H ;HEX DATA19 DD 23H,34789H, DQ (define quadword) Directive DQ is used to allocate memory 8 bytes (four words) in size. This can be used to represent any variable up to 64 bits wide: ORG 00C0H DATA20 DQ 4523C2H ;HEX DATA21 DQ HI ;ASCI I CHARACTERS DATA22 DQ? ;NOTHING 20 DT (define ten bytes) Directive DT is used for memory allocation of packed BCD numbers. The application of DT will be seen in the multibyte addition of BCD numbers in Chapter 3. For now, observe how they are located in memory. Notice that the H after the data is not needed. This allocates 10 bytes, but a maximum ofl8 digits can be entered. ORG OOEOH DATA23 DT ;BCD DATA24 DT? ;NOTHING DT can also be used to allocate 10-byte integers by using the D option: DEC DT 65535d ;the assembler will convert the decimal ;number to hex and store it 21 Memory model SMALL MODEL: This is one of the most widely used memory models for Assembly language programs and is sufficient for the small programs. The SMALL model uses a maximum of 64K bytes of memory for code and another 64K bytes for data. MEDIUM MODEL: In this model, the data must also fit into 64K bytes, but the code can exceed 64K bytes of memory. COMPACTMODEL: This is the opposite of the MEDIUM model. While the data can exceed 64K bytes of memory, the code cannot. LARGE MODEL: Combining the two preceding models gives the LARGE model. Although this model allows both data and code to exceed 64K bytes of memory, no single set of data (such as an array) should exceed 64K bytes. HUGE MODEL: Both code and data can exceed 64K bytes of memory, and a single set of data, such as an array, can exceed 64K bytes as well. There is another memory model called TINY. This model is used with COM files in which the total memory for both code and data must fit into 64K bytes. This model cannot be used with the simplified segment definition. 22 .MODEL SMALL.STACK 32.DATA DATA1 DW 234DH,1DE6H,3BC7H, 566AH ORG 10H SUM DW?.CODE MOV MOV DS,AX MOV CX,04 MOV DI,OFFSET DATA1 SUB BX, BX ADD BX,[DI] INC Dl INC Dl DEC CX JNZ ADD_LP MOV SI,OFFSET SUM MOV [SI],BX MOV AH,4CH INT 21H END START 23 INT 21H Option 01H Input a single character with echo MOV AH, 01 INT 21H This function waits until a character is input for the keyboard, then echoes it to the monitor. The input character (ASCII) will be in AL Without echo MOV AH, 07 INT 21H 24 INT 21H Option 02H Output a single character to monitor MOV AH, 02 MOV DL, M INT 21H DL is loaded wit the character to be displayed 25 80x86 Addressing Modes (1) Register MOV BX,DX Source and Destination should have the same size Immediate MOV AX,2550H Source is a constant Valid for any register except segment registers and flag Direct MOV DL,[2400]; Source is the contents of address is DS:2400H Register Indirect Move AL,[BX]; Source is the contents of address is DS:BX MOV [DI],AH; Destination DS:DI 26 80x86 Addressing Modes (2) Base Relative MOV CX,[BX]+10; move DS:BX+10 and move DS:BX into CX Indexed Relative MOV DX,[SI]+10; DS:SI+10 MOV CL,[DI]+10; DS:DI+10 Based Indexed Relative MOV CL,[BX][DI]+8; DS:BX+DI+8 MOV CH,[BX][SI]+8; DS:BX+SI+8 MOV AH,[BP][DI]+8; DS:BP+DI+8 MOV AH,[BP][SI]+8; DS:BP+SI+8 27
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks