微机原理ch03ASSEMBLYLANGUAGEPROGRAMMING.doc_第1页
微机原理ch03ASSEMBLYLANGUAGEPROGRAMMING.doc_第2页
微机原理ch03ASSEMBLYLANGUAGEPROGRAMMING.doc_第3页
微机原理ch03ASSEMBLYLANGUAGEPROGRAMMING.doc_第4页
微机原理ch03ASSEMBLYLANGUAGEPROGRAMMING.doc_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

3 ASSEMBLY LANGUAGE PROGRAMMING3.1 DIRECTIVES AND SAMPLE PROGRAMA given assembly language program is a series of statements, or lines, which are either assembly language instructions such as MOV or ADD, or statements called directives.;THE FORM OF AN ASSEMBLY LANGUAGE PROGRAM;NOTE: USING SIMPLIFIED SEGMENT DEFINITION.model small.stack 64.datadata1db52hdata2db29hsumdb?.codemainprocfar;this is the program entry pointmovax, data;load the data segmentmovds, ax;assign value to DSmoval, data1;get the first operandmovbl, data2;get the second operandaddal, bl;add the operandsmovsum, al;store the result in location SUMmovah, 4ch;set up to return DOSint21hmainendpp1procfarp1endpp2procfarp2endpend main;this is the program exit point图31 Simple assembly language programDirectives (also called 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 ;commentBrackets indicate that the field is optional.l The label field allows program to refer to a line of code by name. The label field cannot exceed 31 characters.l Labels for directives do not need end with a colon.l 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 with this code segment.l The assembly language mnemonic (instruction) and operand(s) fields together perform the real work of the program and accomplish the tasks for which the program was written. In assembly language such as:add al, blmov ax, 6764ADD and MOV are the mnemonic opcodes, and “al, bl” and “ax, 6764” are the operands.These two fields could contain assembler pseudo-instructions, or directives. They are used by the assembler to organize the program as well as other output files.Directives do not generate any machine code and are used only by the assembler, such as DB, END.l The comment field begins with a “;” the assembler ignores comments 3.1.1 Model definitionThe first statement in 图31 after the comments is the MODEL directive. This directive selects the size of the memory model. Among the options for the memory model are:.model small;this directive defines the mode as smallSMALL is one of the most widely used memory models for assembly language programs and is sufficient for the programs in our examples.The small model uses a maximum of 64K of memory for code and another 64K bytes for data.model medium;the data must fit into 64K bytes;but the code can exceed 64K bytes of memory.model compact;the data can exceed 64K bytes;but the code cannot exceed 64K bytes of memory.model large;both data and code can exceed 64K bytes;but no single data set can exceed 64K.model huge;both data and code can exceed 64K bytes;data items(such as arrays) can exceed 64K.model tiny;used with COM files in which data and code;must fit into 64K bytes3.1.2 Segments of programNormally a program consists of at least three segments:l the stack segmentl the data segmentl the code segment.stack;marks the beginning of the stack segment.data;marks the beginning of the data segment.code;marks the beginning of the code segment3.1.3 Stack segment definition.stack 64This statement directive reserves 64 bytes of memory for the stack.3.1.4 Data segment definition.data;place data definition here;The data items defined in the data segment will be accessed in code segment by their labels.3.1.5 Code segment definitionThe last segment of the program in 图31 is the code segment. The first line of the segment after the .CODE directive is the PROC directive. A procedure is a group of instructions designed to accomplish a specific function. A code segment can consist of only one procedure, but usually is organized into several small procedures in order to make the program more structured. 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 differences between a FAR and a NEAR procedure, as well as where and why each is used, are explained later. For now, just remember that in order to run a program, FAR must be used at the program entry point.What value is actually assigned to the CS, DS, and SS registers for execution of the program?When the program begins executing, 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:mov ax, data;data refers to the start of the data segmentmov ds, axRemember that no segment register can be loaded directly. That is the reason the two lines of code above are needed. You cannot code:mov ds, data;illegalThe two last instructions in the shell are:mov ah, 4chint 21hTheir purpose is to return control to the operating system.The END pseudo-instruction ends the entry program by indicating to DOS that the entry point MAIN has ended. Every program must have an entry point. To identify that entry point the assembler relies on the END directive. The label for the entry and end point must be match.HERE:mov ax, dataseg;entry point of the program.end HERE;exit point of the program3.1.6 Shell of an assembly language program;THE FORM OF AN ASSEMBLY LANGUAGE PROGRAM;USING SIMPLIFIED SEGMENT DEFINITION.model small.stack 64.data;place data definition here;.codemainprocfar;this is the program entry pointmovax, data;load the data segmentmovds, ax;assign value to DS;place code here;movah, 4ch;set up to int21h;return DOSmainendpend main;this is the program exit point3.2 CONTRL TRANSFER INSTRUCTIONSIn the sequence of instructions to be executed, it is often necessary to transfer program control to a different location. There are many instructions in the 80x86 to achieve this.3.2.1 FAR and NEARIf control is transferred to a memory location within the current segment, it is NEAR. This sometimes called intrasegment (within segment).If control is transferred outside the current code segment, it is a FAR or intersegment (between segments) jump.Since the CS:IP registers always point to the address of the next instruction to be executed, they must be updated when a control transfer instruction is executed. In a NEAR jump, the IP is updated and CS remains the same, since control is still inside the current code segment.In a FAR jump, because the control is passing outside the current code segment, both CS and IP have to be updated to the new values.In other words, in any control transfer instructions such as jump or call, the IP must be changed, but only in the FAR case is the CS changed, too.3.2.2 Conditional jumpsIn the conditional jump, control is transferred to a new location if a certain condition is met. The flag register is the one that indicated the current condition.表 31 8086 Conditional jump instructionsMnemonicCondition Tested“Jump If .”JA/JNBE(CF = 0) and (ZF = 0)above/not below nor equalJAE/JNBCF = 0above or equal/not belowJB/JNAECF = 1below/not above nor equalJBE/JNA(CF or ZF) = 1below or equal/not aboveJCCF = 1carryJE/JZZF = 1equal/zeroJG/JNLE(SF xor OF) or ZF) = 0greater/not less nor equalJGE/JNL(SF xor OF) = 0greater or equal/not lessJL/JNGE(SF xor OF) = 1less/not greater nor equalJLE/JNG(SF xor OF) or ZF) = 1less or equal/not greaterJNCCF = 0not carryJNE/JNZZF = 0not equal/not zeroJNOOF = 0not overflowJNP/JPOPF = 0not parity/parity oddJNSSF = 0not signJOOF = 1overflowJP/JPEPF = 1parity/parity evenJSSF = 1signNote:Above and below refer to the relationship of two unsigned values;Greater and less refer to the relationship of two signed values.3.2.3 Short jumpsAll conditional jumps are short jumps. In a short jump, the address of the target must be within -128 to +127 bytes of the IP. In other words, the conditional jump is two-byte instruction: one byte is opcode of the J condition and the second byte is a value between 00 and FF. An offset range of 00 to FF gives 256 possible address; these are split between backward jumps (to -128) and forward jumps (to +127).In a jump backward, the second byte is the 2s complement of the displacement value. To calculate the target address, the second byte is added to the IP of the instruction after the jump.图32 Illustration of the JNE instructionThe instruction “JNZ AGAIN” was assembled as “JNZ 000D”, and 000D is the address of the instruction with the label AGAIN. The instruction “JNZ 000D” has the opcode 75 and the target address FA, which is located at offset address 0011 and 0012. This is followed by “MOV SUM, AL”, which is located beginning at offset 0013. The IP value of “MOV SUM, AL”, 0013, is added to FA to calculate the address of label AGAIN (0013 + FA = 000D, the carry is dropped).3.2.4 Unconditional jumps“JMP label” is an unconditional jump in which control is transferred unconditionally to the target label.3.2.4.1 Short jumpFormat:jmp short labelThis is a jump in which the address of the target location is within -128 to +127 bytes of memory relative to the address of the current IP.In this case, the opcode is EB and the operand is 1 byte in the range 00 to FF.Coding the directive “short” makes the jump more efficient in that it will be assembled into 2-byte instruction instead of 3-byte instruction.3.2.4.2 Near jumpFormat:jmp labelThis is a near jump (within the current segment) and has the opcode E9. The target address can be any of the addressing modes of direct, register indirect, or memory indirect:l Direct JUMP is exactly like the short jump explained earlier, except that the target address can be anywhere in the segment within the range -32768 to +32767 of the current IP.l Register indirect JUMP; the target is in register, for example:jmp bx;jump to the address found in BXIP takes the value BX.l Memory indirect JUMP; the target address is the contents of two memory locations pointed at by the register. jmp di;jump to the address found at the address in DIwill replace the IP with contents of memory locations pointed at by DI and DI + 1.3.2.4.3 FAR jumpFormat:jmp far ptr labelThis is a jump out of the current code segment, meaning that not only the IP but the CS is replaced with new values.3.2.5 Call and return statementsAnother control transfer instruction is the CALL instruction, which is used to call a procedure. CALLs to procedures are used to perform tasks that need to be performed frequently. This makes a program more structured.The target address could be in the current segment, in which case it will be NEAR call or outside the current the CS segment, which is a FAR call.When a subroutine is called, control is transferred to that subroutine and the processor saves the IP (and CS in case of a FAR call) and begins to fetch instructions in the new location.After finishing execution of the subroutine, for control to be back to the caller, the last instruction in the called subroutine must be RET (return).3.2.5.1 Near callIf calling a near procedure (the procedure is in the same code segment as the CALL instruction) then the content of the IP register (which is the address of the instruction after the CALL) is pushed onto the stack and the SP decremented by 2. Then IP is loaded with a new value, which is the offset of the procedure.At the end of the procedure when the RET is executed, IP is popped off the stack, which returns control to the instruction after the CALL.There are three ways to code the address of the called NEAR procedure:l Direct:call proc1proc1proc near.retproc1endpl Register indirect:call si;transfer control to address in SIl Memory indirect:call word ptr di;DI points to the address that;contains IP address of proc3.2.5.2 FAR callWhen calling a far procedure (the procedure is in a different segment from the CALL instruction), the SP is decremented by 4 after CS:IP of the instruction following the CALL is pushed onto the stack. CS:IP is the loaded with the segment and offset address of the called procedure. In pushing CS:IP onto the stack, CS is pushed first and then IP.When RETF is executed, CS and IP are restored from the stack and execution continues with the instruction following the CALL.The following addressing modes are supported:l Direct (but outside the present segment):call proc1proc1proc far.retfproc1endpl Memory indirect:call dword ptr di;transfer control to CS:IP where;DI and DI+1 point to location of IP,;DI+2 and DI+3 point to location of CS3.2.5.3 Return from a procedureFormat:ret n;return from procedureThe instruction RET is used to return from a procedure previously entered by a CALL instruction.The IP is restored from the stack and the SP incremented by 2. If the procedure was far, then RETF (return far) is used, and in addition to restored the IP, the CS is restored from the stack and SP is again incremented by 2.The RET instruction may be followed by a number that will be added to the SP after the SP has been incremented. This is done to skip over any parameters being passed back to the calling program segment.3.2.6 Assembly language subroutineThe main program is the entry point from DOS and is FAR, but the subroutines called within the main program can be FAR or NEAR.Remember that NEAR routines are in the same code segment, while FAR routines are outside the current code segment.If there is no specific mention of FAR after the directive PROC, it defaults to NEAR.;THE FORM OF AN ASSEMBLY LANGUAGE PROGRAM;USING SIMPLIFIED SEGMENT DEFINITION.model small.stack 64.data;place data definition here;.codemainprocfar;this is the program entry pointmovax, data;load the data segmentmovds, ax;assign value to DS;.call subr1call subr2call subr3;.movah, 4ch;set up to int21h;return DOSmainendp;-subr1proc far;place code hereretsubr1endp;-subr2proc;.retsubr2endp;-subr3proc;.retsubr3endp;-end main;this is the program exit point3.2.7 Rules for names in assembly languageBy choosing label names that are meaningful, a programmer can make a program much easier to read and maintain.Each label name must be unique.Label in 80x86 assembly language for MSAM 5.1 and higher must follow these rules:l Name can be composed of:l alphabetic characters: A Z and a zl digits: 0 9l special characters:l question mark (?)l period (.)l at ()l underline (_)l dollar sign ($)l Name can b up to 31 characters long.l The special character period (.) can only be used as the first character.l Uppercase and lowercase are treated the same.l Reserved words cannot Best wishes, a name.Assembly language programs have five types of labels or names:l Code labels, which give symbolic names to instructions so that other instructions (such as jumps) may refer to them.l Procedure labels, which assign a name to a procedurel Segment labels, which assign a name to a segmentl Data labels, which assign names to data iteml Labels created with the LABEL directive3.3 DATA TYPS AND DATA DEFINITION3.3.1 ORG (origin)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. If the number is not followed by H, it is decimal and the assembler will convert it to hex.3.3.2 DB (define byte)The DB directive is one of the most widely used data directives in the assembler. It allows allocation of memory in byte-sized chunks. This is indeed the smallest allocation unit permitted.DB can be used to define numbers in:l decimal, the D after the decimal number is optionall binary, the B after the binary number is requiredl hex, the H after the hexdecimal number is requiredl ASCII, place string in single quotation marks (like this)either single or double quotation can be used around string, this can be useful for string which should contain a single quote such as ”OLeary”Example:000019data1db 25;decimal000189data2db 10001001b;binary000212data3db 12h;hex0010org 0010h001032 35 39 31data4db 2591;ASCII numbers0018org0018h0018?data5db ?;set aside a byte0020org0020h00204D 79 20 6E 61 6Ddata6db My name is Joe;ASCII characters65 20 69 73 20 4A6F 653.3.3 DUP (duplicate)DUP is used to duplicate a given number of characters. This can avoid a lot of typing.Example:0030org0030h0030FF FF FF FF FF FFdata7db 0ffh, 0ffh, 0ffh, 0ffh, 0ffh, 0ffh0038org0038h00380006FFdata8db 6 dup(0ffh);fill 6 bytes with FF0040org0040h00400020?data9db 32 dup(?);set aside 32 bytes0060org0060h00600005000263data10db 5 dup(2 dup(99);fill 10 bytes with 993.3.4 DW (define word)DW is used to allocate memory 2 bytes (one word) at a time. DW is widely used in the 8088/86 and 80286 microprocessors since the registers are 16 bits wide.Example:0070org70h0070BA03da

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论