1 / 31

Computers Organization & Assembly Language

Computers Organization & Assembly Language. Chapter 2 ASSEMBLY LANGUAGE PROGRAMMING An Assemble Program Form. Control Transfer Instructions. An Assemble Program Form. What are the components of an assemble program? A program components are a series of statements or lines, which are: either

diep
Télécharger la présentation

Computers Organization & Assembly Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computers Organization& Assembly Language Chapter 2ASSEMBLY LANGUAGE PROGRAMMING An Assemble Program Form. Control Transfer Instructions.

  2. An Assemble Program Form What are the components of an assemble program? • A program components are a series of statements or lines, which are: either • Assembly Language Instructions such as ADD and MOV or • Pseudo-Instructions (or Directives) such as .MODEL SMALL

  3. An Assemble Program ; An assemble program using simplified segment definition .MODEL SMALL .STACK 64 .DATA DATA1 DB 52 H DATA2 DB 29 H SUM DB ? .CODE MAIN PROC FAR ; This is the program entry point MOV AX, @DATA ; Load the data segment address MOV DS, AX ; Assign value to DS MOV AL, DATA1 ; get the first operand MOV BL, DATA2 ; get the second operand ADD AL, BL ; add the operands MOV SUM, AL ; store the result in location SUM MOV AH, 4CH ; set up to return to DOS INT 21H MAIN ENDP END MAIN ; This is the program exit point

  4. Shell of an Assembly Program ; An assemble program using simplified segment definition .MODEL SMALL .STACK 64 .DATA ; ; place data definitions here ; .CODE MAIN PROC FAR ; This is the program entry point MOV AX, @DATA ; Load the data segment address MOV DS, AX ; Assign value to DS ; ; place code here ; MOV AH, 4CH ; set up to return to DOS INT 21H MAIN ENDP END MAIN ; This is the program exit point

  5. The Instruction’s Fields The four fields of an assembly instruction are: [label:]mnemonic [operands][;comment] • Brackets indicates that the field is optional. • The Label field refers to a line of code by name. • The label up to 31 characters. • The label must end with a colon : in case of assembly instructions but not in case of directives. • Directives or Pseudo-instructions are used by assemblers to organize the programs as well as other output files. • Directives do not generate any machine codes but assembly instructions do.

  6. Directives or Pseudo-Instructions • .MODEL This directive selects the size of the memory model. • The MODEL options are SMALL, MEDIUM, COMPACT, LARGE, HUGE and TINY. • The .MODELSMALL uses a maximum of 64K bytes of memory for code and another 64K bytes of memory for data. • The .MODELMEDIUM uses a maximum of 64K bytes of memory for data and the code can exceed 64K bytes of memory. • The .MODELCOMPACT uses a maximum of 64K bytes of memory for code and the data can exceed 64K bytes of memory.

  7. Directives or Pseudo-Instructions • The .MODELLARGE both data & code can exceed 64K bytes of memory but no single set of data should exceed 64k bytes. • The .MODELHUGE both data & code can exceed 64K bytes of memory and data items such as arrays can exceed 64k bytes. • The .MODELTINY used with COM files in which data & code must fit into 64k bytes.

  8. Segment Definition • The 80X86 CPU has CS (Code segment), DS (Data segment, SS (Stack segment), ES (Extra segment) registers. • Every line in an assembly program must correspond to one of these segments. • In the simplified segment definition format “.CODE”, “.DATA”, and “.STACK” correspond to CS, DS, and SS registers respectively. • There is another older full segment definition format • An assemble program consists of at least 3 segments: .STACK ; marks the beginning of the stack segment .DATA ; marks the beginning of the data segment .CODE ; marks the beginning of the code segment

  9. Data Types and Data Definition • None of the data types are larger than 16 bits wide since the size of registers is 16 bits. • Programmer must break down data types larger than 16-bits. • Data types in 80x86 may be 8-bit or 16-bit positive or negative. • There are some data directives for data types: • DB (define byte) directive ; allocates memory in byte-sized chunks. It defines numbers in decimal (using D is optionally), binary (B), Hex (H), or ASCII (use a single quotation marks). • DW (define word) directive ; allocates memory in word-sized chunks. • DUP (duplicate) ; duplicates a given number of characters. ORG 0030H DATA7 DB 0FF H, 0FF H, 0FF H, 0FF H, 0FF H, 0FF H ORG 0030H DATA8 DB 6 DUP(0FF H) ; fill 6 bytes with FF

  10. DD (define double word) directive ; allocates memory in two words in size. It defines numbers in decimal, binary , or Hex. • DQ (define quade word) directive ; allocates memory in four words (8 bytes) in size. It can represent any variable up to 64 bits wide. • DT (define ten bytes) directive ; allocates memory of packed BCD numbers. H after data is not needed. The maximum of 18 digits can be entered. • EQU directive ; defines a constant without occupying a memory location. It can be used outside the data segment, e.g. at the middle of the code segment. COUNT EQU 25 ; not occupied memory locations. COUNT DB 25 ; occupied a memory location. • ORG ; indicates the beginning of the offset address. This address may be expressed in Hex or in decimal. The offset address may be used in data or code segments. • .STACK 64 ; reserves 64 bytes of memory for the stack.

  11. More Sample Programs TITIL PROG2-1 adding 5 bytes of DATA .MODEL SMALL .STACK 64 .DATA DATA_IN DB 25H, 12H, 15H, 1FH, 2BH SUM DB ? .CODE MAIN PROC FAR MOV AX, @DATA MOV DS, AX MOV CX, 05 ; counter CX = 5 MOV BX, OFFSET DATA_IN ; pointer BX MOV AL, 0 ; initialize AL

  12. AGAIN: ADD AL, [BX] ; add next data item to AL INC BX ; make BX point next data item DEC CX ; decrement loop counter JNZ AGAIN ; jump if loop counter not zero MOV SUM, AL ; load result into sum MOV AH, 4CH ; set up return INT 21H ; return to DOS MAIN ENDP END MAIN • The 80x86 can use any general-purpose register to do arithmetic and logic operations. • BX is used to point and access data elements.

  13. TITIL PROG2-2 adding 4 words of DATA .MODEL SMALL .STACK 64 .DATA DATA_IN DW 234DH, 1DE6H, 3BC7H, 566AH ORG 10H SUM DW ? .CODE MAIN PROC FAR MOV AX, @DATA MOV DS, AX MOV CX, 04 ; set up loop counter CX = 4 MOV DI, OFFSET DATA_IN ; set up data pointer DI MOV BX, 00 ; initialize BX

  14. ADD_LP: ADD BX, [DI] ; add data pointed by [DI] to BX INC DI ; increment DI twice INC DI ; to point to next word DEC CX ; decrement loop counter JNZ ADD_LP ; jump if loop counter not zero MOV SI, OFFSET SUM ; load pointer for sum MOV [SI], BX ; store in data segment MOV AH, 4CH ; set up return INT 21H ; return to DOS MAIN ENDP END MAIN • The 16-bit data (a word) is stored with low-order byte first. • The address pointer is incremented twice, since the operand being accessed is a word (two bytes).

  15. TITIL PROG2-3 transferring 6 bytes of DATA .MODEL SMALL .STACK 64 .DATA ORG 10H DATA_IN DB 25H, 4FH, 85H, 1FH, 2BH, 0C4H ORG 28H COPY DB 6 DUP (?) .CODE MAIN PROC FAR MOV AX, @DATA MOV DS, AX

  16. MOV SI, OFFSET DATA_IN ; SI points to data copied MOV DI, OFFSET COPY ; DI points to copy of data MOV CX, 06H ; loop counter = 6 MOV_LOOP: MOV AL, [SI] ; move the next byte from DATA to AL MOV [DI], AL ; move the next byte to COPY area INC SI ; increment DATA pointer INC DI ; increment COPY pointer DEC CX ; decrement loop counter JNZ MOV_LOOP ; jump if loop counter not zero MOV AH, 4CH ; set up return INT 21H ; return to DOS MAIN ENDP END MAIN

  17. Control Transfer Instructions • In an assembly program, it is often necessary to transfer program control to a different location. There are many instructions to achieve this. • The concept of FAR and NEAR • If control is transferred to a memory location within the current code segment, it is NEAR. This is called Intrasegment. Only IP register must be updated. • If control is transferred outside the current code segment, it is FAR. This is called Intersegment jump CS and IP registers must be updated.

  18. Conditional Jumps • The 8086 Conditional Jump Instructions are:

  19. Control is transferred to a new memory location if a certain condition is met. The Flag register is one that indicates the current condition. • All conditional jumps are SHORT jumps. The target address must be within -128 (backward) to + 127 (forward) bytes of the IP. • The conditional jump is a two-byte instruction; one op-code and the other is a value between 00 to FF (offset address range). • In a backward jump, the second byte is the 2’s complement of the displacement value. The target address = IP of the instruction after the jump instruction + the second byte value. • Similarly, in a forward jump, the target address = IP of the following instruction + the second byte value.

  20. An Example of a Backward Jump 1067:0000 B86610 MOV AX, 1066 1067:0003 8ED8 MOV DS, AX 1067:0005 B90500 MOV CX, 0005 1067:0008 BB0000 MOV BX, 0000 1067:000D 0207 ADD AL, [BX] 1067:000F 43 INC BX 1067:0010 49 DEC CX 1067:0011 75FA JNZ 000D 1067:0013 A20500 MOV [0005], AL 1067:0016 B44C MOV AH, 4C 1067:0018 CD21 INT 21 • The jump or label address is(0013 + FA = 000D). • FA is the 2’s complement of -6. • The target address is -6 bytes from the IP of the next instruction.

  21. An Example of a Forward Jump 0005 8A 47 02 AGAIN: MOV AL, [BX] + 2 0008 3C 61 CMP AL, 61H 000A 72 06 JB NEXT 000C 3C 7A CMP AL, 7AH 000E 77 02 JA NEXT 0010 24 DF AND AL, 0DFH 0012 88 04 NEXT: MOV [SI], AL • The NEXT label address is(000CH + 0006H = 0012). • 6 is the target address. • The target address is 6 bytes from the IP of the next instruction.

  22. Unconditional Jumps JMP label • It is an unconditional jump in which control is transferred to the target location label. • Unconditional jumps can take the following forms: • SHORT jumps • It is specified by the form: JMP SHORT label • The address within the -128 (backward) to + 127 (forward) bytes of the current IP. • The op-code is EB and the other is a value between 00 to FF. • The directive SHORT makes the jump more efficient and makes it as 2-bytes instruction not 3-byte one.

  23. NEAR jumps • It is specified by the form: JMP label • The address within the current code segment. • The target address can be any of addressing modes direct, register indirect or memory indirect. • Direct jump It is exactly like SHORT Jump except the target address can be any where in the segment range from -32768 (backward) to + 32767 (forward) bytes of the current IP. • Register indirect jump, the target address is in a register. Example: JMP BX IP = BX.

  24. Memory indirect jump, the target address is the contents of two memory locations. Example: JMP [DI]IP = The contents of memory locations pointed by DI and DI + 1. • FAR jumps • It is specified by the form: JMP FAR PTR label • The target address out of the current code segment.CS and IP must be changed.

  25. CALL Statements • CALL instruction is used to call a procedure. • It is used to perform tasks that need to beperformed frequently. • It makes programs more structured. • CALL may be NEAR (i.e. the target address in the current segment) or FAR (i.e. the target address out the current segment). • The following is a NEAR CALL example: (different IP, same CS) 12B0:0200 BB1295 MOV BX, 9512 12B0:0203 E8FA00 CALL 0300 12B0:0206 B82F14 MOV AX, 142F

  26. The IP address of the instruction after the CALL is saved on the stack as shown in the following figure. • IP will be 0206, which belongs to the “MOV AX, 142F” instruction. • A RET instruction directs the CPU to POP the top 2 bytes of the stack into the IP and resume executing at offset address 0206. • For every PUSH there must be a POP. 12B0:0300 53 PUSH BX 12B0:0301 ... ... ... ... ... ... ... ... 12B0:0309 5B POP BX 12B0:030A C3 RET

  27. Assembly Language Subroutines .CODE MAIN PROC FAR ; This is the entry point for DOS MOV AX, @DATA MOV DS, AX CALL SUBR1 CALL SUBR2 CALL SUBR3 CALL SUBR1 MOV AH, 4CH ; set up return INT 21H ; return to DOS MAIN ENDP

  28. ;----------------------------------------------------------------------------------;---------------------------------------------------------------------------------- SUBR1 PROC . . . . . . RET SUBR1 ENDP ;---------------------------------------------------------------------------------- SUBR2 PROC . . . . . . RET SUBR2 ENDP ;---------------------------------------------------------------------------------- SUBR3 PROC . . . . . . RET SUBR3 ENDP ;---------------------------------------------------------------------------------- END MAIN ; This is the exit point

  29. Rules for Names in Assembly Language • Names make programs easier to read and maintain. • Label name must be unique. • Names consist of alphabetic letters upper and lower case, the digits from 0 to 9, the special characters include ?, . , @, underline _, and $.

  30. The End

More Related