Computers Organization & Assembly Language Chapter 2 ASSEMBLY LANGUAGE PROGRAMMING An Assemble Program Form. Control Transfer Instructions.
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
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 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
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 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
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.
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.
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.
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
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
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.
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 MOVDS, AX MOVCX, 05 ; counter CX = 5 MOVBX, OFFSET DATA_IN ; pointer BX MOVAL, 0; initialize AL
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.
Computers Organization & Assembly language 13 TITILPROG2-2 adding 4 words of DATA.MODELSMALL.STACK64.DATA DATA_INDW234DH, 1DE6H, 3BC7H, 566AH ORG10H SUMDW?.CODE MAINPROCFAR MOVDS, AX MOVCX, 04 ; set up loop counter CX = 4 MOVDI, OFFSETDATA_IN ; set up data pointer DI MOVBX, 00; initialize BX
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).
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 MOVDS, AX
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
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.
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
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
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 (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.
Computers Organization & Assembly language 21 An Example of a Backward Jump 1067:0000B86610MOV AX, :00038ED8MOV DS, AX 1067:0005B90500MOV CX, :0008BB0000MOV BX, :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( FA = 000D). FA is the 2’s complement of -6. The target address is -6 bytes from the IP of the next instruction.
Computers Organization & Assembly language 22 An Example of a Forward Jump 00058A 47 02AGAIN:MOV AL, [BX] C 61CMP AL, 61H 000A72 06JB NEXT 000C3C 7ACMP AL, 7AH 000E77 02JA NEXT DFAND AL, 0DFH NEXT:MOV [SI], AL The NEXT label address is(000CH H = 0012). 6 is the target address. The target address is 6 bytes from the IP of the next instruction.
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 (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.
Computers Organization & Assembly language 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 (backward) to (forward) bytes of the current IP. Register indirect jump, the target address is in a register. Example:JMP BXIP = BX.
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 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.
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, B0:0203 E8FA00CALL B0:0206 B82F14MOV AX, 142F
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 For every PUSH there must be a POP. 12B0:030053PUSH BX 12B0: B0:03095BPOP BX 12B0:030AC3RET
Computers Organization & Assembly language 28 Assembly Language Subroutines.CODE MAINPROCFAR; This is the entry point for DOS MOVDS, AX CALLSUBR1 CALLSUBR2 CALLSUBR3 CALLSUBR1 MOVAH, 4CH; set up return INT21H; return to DOS MAINENDP
Computers Organization & Assembly language 29 ; SUBR1PROC... RET SUBR1ENDP ; SUBR2PROC... RET SUBR2ENDP ; ; SUBR3PROC... RET SUBR3ENDP ; ENDMAIN; This is the exit point
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 $.
Computers Organization & Assembly language 31 The End