Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Computers Organization & Assembly Language Chapter 2 ASSEMBLY LANGUAGE PROGRAMMING An Assemble Program Form. Control Transfer Instructions."— Presentation transcript:

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

2 Computers Organization & Assembly language 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 1. Assembly Language Instructions such as ADD and MOV or 2. Pseudo-Instructions (or Directives) such as.MODEL SMALL

3 Computers Organization & Assembly language 3 An Assemble Program ; An assemble program using simplified segment definition.MODELSMALL.STACK64.DATA DATA1DB52 H DATA2DB29 H SUMDB?.CODE MAINPROCFAR ; This is the program entry point MOVAX, @DATA; Load the data segment address MOVDS, AX; Assign value to DS MOVAL, DATA1 ; get the first operand MOVBL, DATA2 ; get the second operand ADDAL, BL; add the operands MOVSUM, AL; store the result in location SUM MOVAH, 4CH ; set up to return to DOS INT21H MAINENDP ENDMAIN; This is the program exit point

4 Computers Organization & Assembly language 4 Shell of an Assembly Program ; An assemble program using simplified segment definition.MODELSMALL.STACK64.DATA; ; place data definitions here ;.CODE MAINPROCFAR ; This is the program entry point MOVAX, @DATA; Load the data segment address MOVDS, AX; Assign value to DS ; ; place code here ; MOVAH, 4CH ; set up to return to DOS INT21H MAINENDP ENDMAIN; This is the program exit point

5 Computers Organization & Assembly language 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. Directives do not generate any machine codes but assembly instructions do.

6 Computers Organization & Assembly language 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.MODEL SMALL uses a maximum of 64K bytes of memory for code and another 64K bytes of memory for data. The.MODEL MEDIUM uses a maximum of 64K bytes of memory for data and the code can exceed 64K bytes of memory. The.MODEL COMPACT uses a maximum of 64K bytes of memory for code and the data can exceed 64K bytes of memory.

7 Computers Organization & Assembly language 7 Directives or Pseudo-Instructions The.MODEL LARGE both data & code can exceed 64K bytes of memory but no single set of data should exceed 64k bytes. The.MODEL HUGE both data & code can exceed 64K bytes of memory and data items such as arrays can exceed 64k bytes. The.MODEL TINY used with COM files in which data & code must fit into 64k bytes.

8 Computers Organization & Assembly language 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 Computers Organization & Assembly language 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. ORG0030H DATA7DB0FF H, 0FF H, 0FF H, 0FF H, 0FF H, 0FF H ORG0030H DATA8DB6DUP(0FF H); fill 6 bytes with FF

10 Computers Organization & Assembly language 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. COUNTEQU25; not occupied memory locations. COUNTDB25; 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 Computers Organization & Assembly language 11 More Sample Programs TITILPROG2-1 adding 5 bytes of DATA.MODELSMALL.STACK64.DATA DATA_INDB25H, 12H, 15H, 1FH, 2BH SUMDB?.CODE MAINPROCFAR MOVAX, @DATA MOVDS, AX MOVCX, 05 ; counter CX = 5 MOVBX, OFFSET DATA_IN ; pointer BX MOVAL, 0; initialize AL

12 Computers Organization & Assembly language 12 AGAIN:ADDAL, [BX]; add next data item to AL INCBX; make BX point next data item DECCX; decrement loop counter JNZ AGAIN ; jump if loop counter not zero MOVSUM, AL; load result into sum MOVAH, 4CH; set up return INT21H; return to DOS MAINENDP ENDMAIN The 80x86 can use any general-purpose register to do arithmetic and logic operations. BX is used to point and access data elements.

13 Computers Organization & Assembly language 13 TITILPROG2-2 adding 4 words of DATA.MODELSMALL.STACK64.DATA DATA_INDW234DH, 1DE6H, 3BC7H, 566AH ORG10H SUMDW?.CODE MAINPROCFAR MOVAX, @DATA MOVDS, AX MOVCX, 04 ; set up loop counter CX = 4 MOVDI, OFFSETDATA_IN ; set up data pointer DI MOVBX, 00; initialize BX

14 Computers Organization & Assembly language 14 ADD_LP:ADDBX, [DI] ; add data pointed by [DI] to BX INCDI; increment DI twice INCDI; to point to next word DECCX; decrement loop counter JNZ ADD_LP ; jump if loop counter not zero MOVSI, OFFSET SUM; load pointer for sum MOV[SI], BX; store in data segment MOVAH, 4CH; set up return INT21H; return to DOS MAINENDP ENDMAIN 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 Computers Organization & Assembly language 15 TITILPROG2-3 transferring 6 bytes of DATA.MODELSMALL.STACK64.DATA ORG10H DATA_INDB25H, 4FH, 85H, 1FH, 2BH, 0C4H ORG28H COPYDB6 DUP (?).CODE MAINPROCFAR MOVAX, @DATA MOVDS, AX

16 Computers Organization & Assembly language 16 MOVSI, OFFSET DATA_IN ; SI points to data copied MOVDI, OFFSET COPY; DI points to copy of data MOVCX, 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 INCSI; increment DATA pointer INCDI ; increment COPY pointer DECCX; decrement loop counter JNZ MOV_LOOP ; jump if loop counter not zero MOVAH, 4CH; set up return INT21H; return to DOS MAINENDP ENDMAIN

17 Computers Organization & Assembly language 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 1. 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. 2. 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 Computers Organization & Assembly language 18 Conditional Jumps The 8086 Conditional Jump Instructions are: "Jump IF …"Condition TestedMnemonic above/ not below nor zero(CF = 0) and (ZF = 0)JA/JNBE above or equal/ not belowCF = 0JAE/JNB below/ not above nor equalCF = 1JB/JNAE below or equal/ not above(CF or ZF) = 1JBE/JNA carryCF = 1JC equal/ zeroZF = 1JE/JZ greater / not less nor equal((SF xor OF) or ZF) = 0JG/JNLE greater or equal/ not less(SF xor OF) = 0JGE/JNL less /not greater nor equal(SF xor OR) = 1JL/JNGE

19 Computers Organization & Assembly language 19 "Jump IF …"Condition TestedMnemonic less or equal/not greater((SF xor OF) or ZF) = 1JLE/JNG not carryCF = 0JNC not equal/ not zeroZF = 0JNE/JNZ not overflowOF = 0JNO not parity/ parity oddPF = 0JNP/JPO not signSF = 0JNS overflowOF = 1JO parity/ parity equalPF = 1JP/JPE signSF = 1JS

20 Computers Organization & Assembly language 20 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.

21 Computers Organization & Assembly language 21 An Example of a Backward Jump 1067:0000B86610MOV AX, 1066 1067:00038ED8MOV DS, AX 1067:0005B90500MOV CX, 0005 1067:0008BB0000MOV BX, 0000 1067:000D0207ADD AL, [BX] 1067:000F43INC BX 1067:001049DEC CX 1067:001175FAJNZ 000D 1067:0013A20500MOV [0005], AL 1067:0016B44CMOV AH, 4C 1067:0018CD21INT 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.

22 Computers Organization & Assembly language 22 An Example of a Forward Jump 00058A 47 02AGAIN:MOV AL, [BX] + 2 00083C 61CMP AL, 61H 000A72 06JB NEXT 000C3C 7ACMP AL, 7AH 000E77 02JA NEXT 001024 DFAND AL, 0DFH 001288 04NEXT: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.

23 Computers Organization & Assembly language 23 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: 1. 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.

24 Computers Organization & Assembly language 24 2. 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 BXIP = BX.

25 Computers Organization & Assembly language 25  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. 3. 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.

26 Computers Organization & Assembly language 26 CALL Statements CALL instruction is used to call a procedure. It is used to perform tasks that need to be performed 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: The following is a NEAR CALL example: (different IP, same CS) 12B0:0200BB1295MOV BX, 9512 12B0:0203 E8FA00CALL0300 12B0:0206 B82F14MOV AX, 142F

27 Computers Organization & Assembly language 27 The 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:030053PUSH BX 12B0:0301........................ 12B0:03095BPOP BX 12B0:030AC3RET

28 Computers Organization & Assembly language 28 Assembly Language Subroutines.CODE MAINPROCFAR; This is the entry point for DOS MOVAX, @DATA MOVDS, AX CALLSUBR1 CALLSUBR2 CALLSUBR3 CALLSUBR1 MOVAH, 4CH; set up return INT21H; return to DOS MAINENDP

29 Computers Organization & Assembly language 29 ;---------------------------------------------------------------------------------- SUBR1PROC... RET SUBR1ENDP ;---------------------------------------------------------------------------------- SUBR2PROC... RET SUBR2ENDP ;---------------------------------------------------------------------------------- ;---------------------------------------------------------------------------------- SUBR3PROC... RET SUBR3ENDP ;---------------------------------------------------------------------------------- ENDMAIN; This is the exit point

30 Computers Organization & Assembly language 30 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 $.

31 Computers Organization & Assembly language 31 The End


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

Similar presentations


Ads by Google