CONTROL TRANSFER INSTRUCTIONS FAR and NEARCONTROL TRANSFER INSTRUCTIONS FAR and NEAR §In the sequence of instructions, it is often necessary to transfer program control to a different location. oIf control is transferred to a memory location within the current code segment, it is NEAR. •Sometimes called intrasegment. (within segment) oIf control is transferred outside the current code segment, it is a FAR jump. •Or intersegment. (between segments)
CONTROL TRANSFER INSTRUCTIONS FAR and NEARCONTROL TRANSFER INSTRUCTIONS FAR and NEAR §As the CS:IP registers always point to the address of the next instruction to be executed, they must be updated when a control transfer is executed. oIn a NEAR jump, the IP is updated and CS remains the same, since control is still inside the current code segment. oIn a FAR jump, because control is passing outside the current code segment, both CS and IP have to be updated to the new values.
CONTROL TRANSFER INSTRUCTIONS conditional jumpsCONTROL TRANSFER INSTRUCTIONS conditional jumps §Conditional jumps have mnemonics such as JNZ (jump not zero) and JC (jump if carry). oIn the conditional jump, control is transferred to a new location if a certain condition is met. oThe flag register indicates the current condition. §For example, with "JNZ label", the processor looks at the zero flag to see if it is raised. oIf not, the CPU starts to fetch and execute instructions from the address of the label. oIf ZF = 1, it will not jump but will execute the next instruction below the JNZ.
CONTROL TRANSFER INSTRUCTIONS short jumpsCONTROL TRANSFER INSTRUCTIONS short jumps §All conditional jumps are short jumps. oThe address of the target must be within -128 to +127 bytes of the IP. §The conditional jump is a two-byte instruction. oOne byte is the opcode of the J condition. oThe second byte is a value between 00 and FF. •An offset range of 00 to FF gives 256 possible addresses. §In a jump backward, the second byte is the 2's complement of the displacement value
CONTROL TRANSFER INSTRUCTIONS short jumpsCONTROL TRANSFER INSTRUCTIONS short jumps §To calculate the target address, the second byte is added to the IP of the instruction after the jump. –"JNZ AGAIN" was assembled as "JNZ 000D", and 000D is the address of the instruction with the label AGAIN. •"JNZ 000D" has the opcode 75 and the target address FA.
CONTROL TRANSFER INSTRUCTIONS short jumps–The IP value of MOV,0013, is added to FA to calculate the address of label AGAIN, and the carry is dropped. •FA is the 2's complement of -6. CONTROL TRANSFER INSTRUCTIONS short jumps §This is followed by "MOV SUM,AL", which is located beginning at offset address 0013.
CONTROL TRANSFER INSTRUCTIONS short jumpsCONTROL TRANSFER INSTRUCTIONS short jumps §Calculate a forward jump target address by adding the IP of the following instruction to the operand. oThe displacement value is positive, as shown. –"JB NEXT" has the opcode 72, the target address 06 and is located at IP = 000A and 000B. •The jump is 6 bytes from the next instruction, is IP = 000C. •Adding gives us 000CH + 0006H = 0012H, which is the exact address of the NEXT label.
CONTROL TRANSFER INSTRUCTIONS short jumpsCONTROL TRANSFER INSTRUCTIONS short jumps §For conditional jumps, the address of the target address can never be more than -128 to +127 bytes away from the IP associated with the instruction following the jump. oAny attempt is made to violate this rule will generate a "relative jump out of range" message.
CONTROL TRANSFER INSTRUCTIONS unconditional jumpsCONTROL TRANSFER INSTRUCTIONS unconditional jumps §An unconditional jump transfers control to the target location label unconditionally, in the following forms: oSHORT JUMP - in the format "JMP SHORT label". •A jump within -128 to +127 bytes of memory relative to the address of the current IP, opcode EB. oNEAR JUMP - the default, has the format "JMP label". •A jump within the current code segment, opcode E9. •The target address can be any of the addressing modes of direct, register, register indirect, or memory indirect: oDirect JUMP - exactly like the short jump. •Except that the target address can be anywhere in the segment in the range +32767 to -32768 of the current IP.
CONTROL TRANSFER INSTRUCTIONS unconditional jumpsCONTROL TRANSFER INSTRUCTIONS unconditional jumps §An unconditional jump transfers control to the target location label unconditionally, in the following forms: –Register indirect JUMP - target address is in a register. •In "JMP BX", IP takes the value BX. –Memory indirect JMP - target address is the contents of two memory locations, pointed at by the register. •"JMP [DI]" will replace the IP with the contents of memory locations pointed at by DI and DI+1. –FAR JUMP - in the format "JMP FAR PTR label". register. •A jump out of the current code segment •IP and CS are both replaced with new values.
CONTROL TRANSFER INSTRUCTIONS CALL statementsCONTROL TRANSFER INSTRUCTIONS CALL statements §The CALL instruction is used to call a procedure, to perform tasks that need to be performed frequently. oThe target address could be in the current segment, in which case it will be a NEAR call or outside the current CS segment, which is a FAR call. §The microprocessor saves the address of the instruction following the call on the stack. oTo know where to return, after executing the subroutine. •In the NEAR call only the IP is saved on the stack. •In a FAR call both CS and IP are saved.
CONTROL TRANSFER INSTRUCTIONS CALL statements–Since this is a NEAR call, only IP is saved on the stack. •The IP address 0206, which belongs to the "MOV AX,142F" instruction, is saved on the stack. –Since this is a NEAR call, only IP is saved on the stack. •The IP address 0206, which belongs to the "MOV AX,142F" instruction, is saved on the stack. CONTROL TRANSFER INSTRUCTIONS CALL statements §For control to be transferred back to the caller, the last subroutine instruction must be RET (return). oFor NEAR calls, the IP is restored. oFor FAR calls, CS & IP are restored. §Assume SP = FFFEH:
CONTROL TRANSFER INSTRUCTIONS short jumpsCONTROL TRANSFER INSTRUCTIONS short jumps §The last instruction of the called subroutine must be a RET instruction that directs the CPU to POP the top 2 bytes of the stack into the IP and resume executing at offset address 0206. oThe number of PUSH and POP instructions (which alter the SP) must match. •For every PUSH there must be a POP.
CONTROL TRANSFER INSTRUCTIONS assembly language subroutinesCONTROL TRANSFER INSTRUCTIONS assembly language subroutines It is common to have one main program and many subroutines to be called from the main. Each subroutine can be a separate module, tested separately, then brought together. If there is no specific mention of FAR after the directive PROC, it defaults to NEAR.
CONTROL TRANSFER INSTRUCTIONS rules for names in Assembly languageCONTROL TRANSFER INSTRUCTIONS rules for names in Assembly language §The names used for labels in Assembly language programming consist of… oAlphabetic letters in both upper- and lowercase. oThe digits 0 through 9. oQuestion mark (?); Period (.); At (@) oUnderline (_); Dollar sign ($) •Each label name must be unique. –They may be up to 31 characters long. •The first character must be an alphabetic or special character. –It cannot be a digit.
DATA TYPES AND DATA DEFINITION x86 data typesDATA TYPES AND DATA DEFINITION x86 data types §The 8088/86 processor supports many data types. oData types can be 8- or 16-bit, positive or negative. •The programmer must break down data larger than 16 bits (0000 to FFFFH, or 0 to 65535 in decimal). oA number less than 8 bits wide must be coded as an 8-bit register with the higher digits as zero. •A number is less than 16 bits wide must use all 16 bits.
DATA TYPES AND DATA DEFINITION ORG originDATA TYPES AND DATA DEFINITION ORG origin §ORG is used to indicate the beginning of the offset address. oThe number 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.
DATA TYPES AND DATA DEFINITION DB define byteDATA TYPES AND DATA DEFINITION DB define byte §One of the most widely used data directives, it allows allocation of memory in byte-sized chunks. oThis is the smallest allocation unit permitted. oDB can define numbers in decimal, binary, hex, & ASCII. •D after the decimal number is optional. •B (binary) and H (hexadecimal) is required. •To indicate ASCII, place the string in single quotation marks. §DB is the only directive that can be used to define ASCII strings larger than two characters. oIt should be used for all ASCII data definitions.
DATA TYPES AND DATA DEFINITION DB define byteDATA TYPES AND DATA DEFINITION DB define byte §Some DB examples: – Single or double quotes can be used around ASCII strings. •Useful for strings, which should contain a single quote, such as "O'Leary".
DATA TYPES AND DATA DEFINITION EQU equate•When executing the instructions "MOV CX,COUNT", the register CX will be loaded with the value 25. –In contrast to using DB: DATA TYPES AND DATA DEFINITION EQU equate §EQU associates a constant value with a data label. oWhen the label appears in the program, its constant value will be substituted for the label. oDefines a constant without occupying a memory location. §EQU for the counter constant in the immediate addressing mode:
DATA TYPES AND DATA DEFINITION EQU equateDATA TYPES AND DATA DEFINITION EQU equate §When executing the same instruction "MOV CX,COUNT" it will be in the direct addressing mode. oEQU can also be used in the data segment: –Assume a constant (a fixed value) used in many different places in the data and code segments. •By use of EQU, one can change it once and the assembler will change all of them.
DATA TYPES AND DATA DEFINITION DD define doublewordDATA TYPES AND DATA DEFINITION DD define doubleword §The DD directive is used to allocate memory locations that are 4 bytes (two words) in size. oData is converted to hex & placed in memory locations •Low byte to low address and high byte to high address. •List file for DD examples.
DATA TYPES AND DATA DEFINITION DT define ten bytes DATA TYPES AND DATA DEFINITION DT define ten bytes §DT is used for memory allocation of packed BCD numbers. oThis directive allocates 10 bytes. •A maximum of 18 digits can be entered. oThe "H" after the data is not needed. •List file for DT examples.
DATA TYPES AND DATA DEFINITION directivesDATA TYPES AND DATA DEFINITION directives §Figure 2-7 shows the memory dump of the data section, including all the examples in this section. oIt is essential to understand the way operands are stored in memory.
DATA TYPES AND DATA DEFINITION directivesDATA TYPES AND DATA DEFINITION directives §All of the data directives use the little endian format. oFor ASCII data, only DB can define data of any length. •Use of DD, DQ, or DT directives for ASCII strings of more than 2 bytes gives an assembly error.
DATA TYPES AND DATA DEFINITION directivesDATA TYPES AND DATA DEFINITION directives §Review "DATA20 DQ 4523C2", residing in memory starting at offset 00C0H. oC2, the least significant byte, is in location 00C0, with 23 in 00C1, and 45, the most significant byte, in 00C2.
DATA TYPES AND DATA DEFINITION directivesDATA TYPES AND DATA DEFINITION directives §When DB is used for ASCII numbers, it places them backwards in memory. oReview "DATA4 DB '2591'" at origin 10H:32, •ASCII for 2, is in memory location 10H;35; for 5, in 11H; etc.
FULL SEGMENT DEFINITION segment definitionFULL SEGMENT DEFINITION segment definition §The SEGMENT and ENDS directives indicate the beginning &ending of a segment, in this format: –The label, or name, must follow naming conventions and be unique. •The [options] field gives important information to the assembler for organizing the segment, but is not required. –The ENDS label must be the same label as in the SEGMENT directive. •In full segment definition, the ".MODEL" directive is not used.
FULL SEGMENT DEFINITION segment definitionFULL SEGMENT DEFINITION segment definition §The directives ".STACK", ".DATA", and ".CODE" are replaced by SEGMENT and ENDS directives that surround each segment. oFigure 2-8 shows the full segment definition and simplified format, side by side. •Followed by programs 2-2 and 2-3.
FULL SEGMENT DEFINITION stack segment definitionFULL SEGMENT DEFINITION stack segment definition §The stack segment shown contains the line "DB 64 DUP (?)" to reserve 64 bytes of memory for the stack. oThe following three lines in full segment definition are comparable to ".STACK 64" in simple definition:
FULL SEGMENT DEFINITION data segment definitionFULL SEGMENT DEFINITION data segment definition §In full segment definition, the SEGMENT directive names the data segment and must appear before the data. oThe ENDS segment marks the end of the data segment: •The code segment also begins and ends with SEGMENT and ENDS directives:
FULL SEGMENT DEFINITION code segment definitionFULL SEGMENT DEFINITION code segment definition §Immediately after PROC, the ASSUME directive, associates segments with specific registers. oBy assuming 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. oASSUME tells the assembler which of the segments, defined by SEGMENT, should be used. •Also helps the assembler to calculate the offset addresses from the beginning of that segment. §In "MOV AL, [BX] " the BX register is the offset of the data segment.
FULL SEGMENT DEFINITION code segment definitionFULL SEGMENT DEFINITION code segment definition §On transfer of control from OS to the program, of the three segment registers, only CS and SS have the proper values. oThe DS value (and ES) must be initialized by the program.
FULL SEGMENT DEFINITION the emu8086 assemblerFULL SEGMENT DEFINITION the emu8086 assembler §A simple, popular assembler for 8086 Assembly language programs is called emu8086. See emu8086 screenshots on page 80 - 82 of your textbook.
FULL SEGMENT DEFINITION the emu8086 assemblerFULL SEGMENT DEFINITION the emu8086 assembler Download the emu8086 assembler from this website: http://www.emu8086.com See a Tutorial on how to use it at: http://www.MicroDigitalEd.com
Thank you for your comment.