computers organization & assembly language chapter 2 assembly language programming an assemble...

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

Upload: annabelle-barber

Post on 15-Jan-2016

262 views

Category:

Documents


1 download

TRANSCRIPT

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

Computers OrganizationComputers Organization& &

Assembly LanguageAssembly Language

Chapter 2Chapter 2

ASSEMBLY LANGUAGE PROGRAMMINGASSEMBLY LANGUAGE PROGRAMMING

An Assemble Program Form.An Assemble Program Form.

Control Transfer Instructions.Control Transfer Instructions.

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

Computers Organization & Assembly language Computers Organization & Assembly language 22

An Assemble Program FormAn Assemble Program Form

What are the components of an assemble program?What are the components of an assemble program?

A program components are a series of statements or A program components are a series of statements or lines, which are:lines, which are:

eithereither

1.1. Assembly Language InstructionsAssembly Language Instructions such as such as

ADDADD and and MOVMOV

oror

2.2. Pseudo-InstructionsPseudo-Instructions (or (or DirectivesDirectives) such as ) such as

.MODEL SMALL.MODEL SMALL

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

Computers Organization & Assembly language Computers Organization & Assembly language 33

An Assemble ProgramAn Assemble Program; An assemble program using simplified segment definition; An assemble program using simplified segment definition

.MODEL.MODEL SMALLSMALL

.STACK.STACK 6464

.DATA.DATADATA1DATA1 DBDB 52 H52 HDATA2DATA2 DBDB 29 H29 HSUMSUM DBDB ??

.CODE.CODEMAINMAIN PROCPROC FARFAR ; This is the program entry point ; This is the program entry point

MOVMOV AX, @DATAAX, @DATA ; Load the data segment address; Load the data segment addressMOVMOV DS, AXDS, AX ; Assign value to DS; Assign value to DSMOVMOV AL, DATA1 AL, DATA1 ; get the first operand; get the first operandMOVMOV BL, DATA2 BL, DATA2 ; get the second operand; get the second operandADDADD AL, BLAL, BL ; add the operands; add the operandsMOVMOV SUM, ALSUM, AL ; store the result in location SUM; store the result in location SUMMOVMOV AH, 4CHAH, 4CH ; set up to return to DOS ; set up to return to DOSINTINT 21H21H

MAINMAIN ENDPENDPENDEND MAINMAIN ; This is the program exit point; This is the program exit point

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

Computers Organization & Assembly language Computers Organization & Assembly language 44

Shell of an Assembly ProgramShell of an Assembly Program; An assemble program using simplified segment definition; An assemble program using simplified segment definition

.MODEL.MODEL SMALLSMALL

.STACK.STACK 6464

.DATA.DATA;;; place data definitions here; place data definitions here;;

.CODE.CODEMAINMAIN PROCPROC FARFAR ; This is the program entry point ; This is the program entry point

MOVMOV AX, @DATAAX, @DATA ; Load the data segment address; Load the data segment addressMOVMOV DS, AXDS, AX ; Assign value to DS; Assign value to DS

;;; place code here; place code here;;

MOVMOV AH, 4CHAH, 4CH ; set up to return to DOS ; set up to return to DOSINTINT 21H21H

MAINMAIN ENDPENDPENDEND MAINMAIN ; This is the program exit point; This is the program exit point

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

Computers Organization & Assembly language Computers Organization & Assembly language 55

The Instruction’s FieldsThe Instruction’s FieldsThe four fields of an assembly instruction are:The four fields of an assembly instruction are:

[label:][label:] mnemonic mnemonic [operands][operands] [;comment][;comment]

Brackets indicates that the field is optional.Brackets indicates that the field is optional.The Label field refers to a line of code by name.The Label field refers to a line of code by name.The label up to 31 characters.The label up to 31 characters.The label must end with a colon : in case of The label must end with a colon : in case of assembly instructions but not in case of directives.assembly instructions but not in case of directives.Directives or Pseudo-instructions are used by Directives or Pseudo-instructions are used by assemblers to organize the programs as well as assemblers to organize the programs as well as other output files.other output files. Directives do not generate any machine codes but Directives do not generate any machine codes but assembly instructions do.assembly instructions do.

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

Computers Organization & Assembly language Computers Organization & Assembly language 66

Directives or Pseudo-Directives or Pseudo-InstructionsInstructions

.MODEL.MODEL This directive selects the size of the This directive selects the size of the memory model.memory model.The MODEL options are The MODEL options are SMALLSMALL,, MEDIUM MEDIUM,, COMPACTCOMPACT, , LARGELARGE, , HUGEHUGE and and TINY TINY..The The .MODEL.MODEL SMALLSMALL uses a maximum of 64K bytes uses a maximum of 64K bytes of memory for code and another 64K bytes of of memory for code and another 64K bytes of memory for data.memory for data.The The .MODEL.MODEL MEDIUMMEDIUM uses a maximum of 64K bytes uses a maximum of 64K bytes of memory for data and the code can exceed 64K of memory for data and the code can exceed 64K bytes of memory.bytes of memory.The The .MODEL.MODEL COMPACTCOMPACT uses a maximum of 64K uses a maximum of 64K bytes of memory for code and the data can exceed bytes of memory for code and the data can exceed 64K bytes of memory.64K bytes of memory.

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

Computers Organization & Assembly language Computers Organization & Assembly language 77

Directives or Pseudo-Directives or Pseudo-InstructionsInstructions

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.

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

Computers Organization & Assembly language Computers Organization & Assembly language 88

Segment DefinitionSegment DefinitionThe 80X86 CPU has The 80X86 CPU has CSCS (Code segment), (Code segment), DSDS (Data (Data segment, segment, SSSS (Stack segment), (Stack segment), ESES (Extra segment) (Extra segment) registers. registers. Every line in an assembly program must correspond to Every line in an assembly program must correspond to one of these segments. one of these segments. In the In the simplified segment definitionsimplified segment definition format “ format “.CODE.CODE”, ”, ““.DATA.DATA”, and “”, and “.STACK.STACK” correspond to ” correspond to CSCS, , DSDS, and , and SSSS registers respectively.registers respectively.There is another older There is another older full segment definitionfull segment definition format formatAn assemble program consists of at least 3 segments:An assemble program consists of at least 3 segments:

.STACK.STACK ; marks the beginning of the stack segment; marks the beginning of the stack segment

.DATA.DATA ; marks the beginning of the data segment; marks the beginning of the data segment

.CODE.CODE ; marks the beginning of the code segment; marks the beginning of the code segment

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

Computers Organization & Assembly language Computers Organization & Assembly language 99

Data Types and Data DefinitionData Types and Data DefinitionNone of the data types are larger than 16 bits wide since the size None of the data types are larger than 16 bits wide since the size of registers is 16 bits.of registers is 16 bits.

Programmer must break down data types larger than 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. Data types in 80x86 may be 8-bit or 16-bit positive or negative.

There are some data directives for data types:There are some data directives for data types:

DB (define byte) directive DB (define byte) directive ; allocates memory in byte-sized ; allocates memory in byte-sized chunks. It defines numbers in decimal (using D is optionally), chunks. It defines numbers in decimal (using D is optionally), binary (B), Hex (H), or ASCII (use a single quotation marks).binary (B), Hex (H), or ASCII (use a single quotation marks).

DW (define word) directiveDW (define word) directive ; allocates memory in word-sized ; allocates memory in word-sized chunks.chunks.

DUP (duplicate)DUP (duplicate) ; duplicates a given number of characters. ; duplicates a given number of characters.

ORGORG 0030H0030H

DATA7DATA7 DBDB 0FF H, 0FF H, 0FF H, 0FF H, 0FF H, 0FF H0FF H, 0FF H, 0FF H, 0FF H, 0FF H, 0FF H

ORGORG 0030H0030H

DATA8DATA8 DBDB 66 DUP(0FF H)DUP(0FF H) ; fill 6 bytes with FF; fill 6 bytes with FF

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

Computers Organization & Assembly language Computers Organization & Assembly language 1010

DD (define double word) directiveDD (define double word) directive ; allocates memory in two ; allocates memory in two words in size. It defines numbers in decimal, binary , or Hex.words in size. It defines numbers in decimal, binary , or Hex.

DQ (define quade word) directiveDQ (define quade word) directive ; allocates memory in four ; allocates memory in four words (8 bytes) in size. It can represent any variable up to 64 bits words (8 bytes) in size. It can represent any variable up to 64 bits wide.wide.

DT (define ten bytes) directiveDT (define ten bytes) directive; allocates memory of packed BCD ; allocates memory of packed BCD numbers. H after data is not needed. The maximum of 18 digits numbers. H after data is not needed. The maximum of 18 digits can be entered.can be entered.

EQU directiveEQU directive ; defines a constant without occupying a memory ; defines a constant without occupying a memory location. It can be used outside the data segment, e.g. at the location. It can be used outside the data segment, e.g. at the middle of the code segment.middle of the code segment.

COUNTCOUNTEQUEQU 2525 ; not occupied memory locations.; not occupied memory locations.

COUNTCOUNTDBDB 2525 ; occupied a memory location.; occupied a memory location.

ORG ORG ; indicates the beginning of the offset address. This ; indicates the beginning of the offset address. This address may be expressed in Hex or in decimal.address may be expressed in Hex or in decimal. The offset The offset address may be used in data or code segments.address may be used in data or code segments.

.STACK 64.STACK 64 ; reserves 64 bytes of memory for the ; reserves 64 bytes of memory for the stack.stack.

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

Computers Organization & Assembly language Computers Organization & Assembly language 1111

More Sample ProgramsMore Sample ProgramsTITILTITIL PROG2-1 adding 5 bytes of DATAPROG2-1 adding 5 bytes of DATA

.MODEL.MODEL SMALLSMALL

.STACK.STACK 6464

.DATA.DATA

DATA_INDATA_IN DBDB 25H, 12H, 15H, 1FH, 2BH25H, 12H, 15H, 1FH, 2BH

SUMSUM DBDB ??

.CODE.CODE

MAINMAIN PROCPROC FARFAR

MOVMOV AX, @DATAAX, @DATA

MOVMOV DS, AXDS, AX

MOVMOV CX, 05CX, 05 ; counter CX = 5; counter CX = 5

MOVMOV BX, OFFSET DATA_IN BX, OFFSET DATA_IN ; pointer BX; pointer BX

MOVMOV AL, 0AL, 0 ; initialize AL; initialize AL

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

Computers Organization & Assembly language Computers Organization & Assembly language 1212

AGAIN:AGAIN: ADDADD AL, [BX]AL, [BX] ; add next data item to AL; add next data item to AL

INCINC BXBX ; make BX point next data ; make BX point next data itemitem

DECDEC CXCX ; decrement loop counter; decrement loop counter

JNZJNZ AGAIN AGAIN ; jump if loop counter not ; jump if loop counter not zerozero

MOVMOV SUM, ALSUM, AL ; load result into sum; load result into sum

MOVMOV AH, 4CHAH, 4CH ; set up return; set up return

INTINT 21H21H ; return to DOS; return to DOS

MAINMAIN ENDPENDP

ENDEND MAINMAIN

The 80x86 can use any general-purpose register to do The 80x86 can use any general-purpose register to do

arithmetic and logic operations.arithmetic and logic operations.

BX is used to point and access data elements.BX is used to point and access data elements.

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

Computers Organization & Assembly language Computers Organization & Assembly language 1313

TITILTITIL PROG2-2 adding 4 words of DATAPROG2-2 adding 4 words of DATA

.MODEL.MODEL SMALLSMALL

.STACK.STACK 6464

.DATA.DATA

DATA_INDATA_IN DWDW 234DH, 1DE6H, 3BC7H, 566AH234DH, 1DE6H, 3BC7H, 566AH

ORGORG 10H10H

SUMSUM DWDW ??

.CODE.CODE

MAINMAIN PROCPROC FARFAR

MOVMOV AX, @DATAAX, @DATA

MOVMOV DS, AXDS, AX

MOVMOV CX, 04CX, 04 ; set up loop counter CX = ; set up loop counter CX = 44

MOVMOV DI, OFFSETDI, OFFSET DATA_IN DATA_IN

; set up data pointer DI; set up data pointer DI

MOVMOV BX, 00BX, 00 ; initialize BX; initialize BX

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

Computers Organization & Assembly language Computers Organization & Assembly language 1414

ADD_LP:ADD_LP: ADDADD BX, [DI]BX, [DI] ; add data pointed by [DI] to BX ; add data pointed by [DI] to BXINCINC DIDI ; increment DI twice; increment DI twiceINCINC DIDI ; to point to next word; to point to next wordDECDEC CXCX ; decrement loop counter; decrement loop counterJNZJNZ ADD_LP ADD_LP ; jump if loop counter not ; jump if loop counter not

zerozeroMOVMOV SI, OFFSET SUMSI, OFFSET SUM ; load pointer for sum; load pointer for sumMOVMOV [SI], BX[SI], BX ; store in data segment; store in data segmentMOVMOV AH, 4CHAH, 4CH ; set up return; set up returnINTINT 21H21H ; return to DOS; return to DOS

MAINMAIN ENDPENDPENDEND MAINMAIN

The 16-bit data (a word) is stored with low-order byte first.The 16-bit data (a word) is stored with low-order byte first.

The address pointer is incremented twice, since the operand The address pointer is incremented twice, since the operand

being accessed is a word (two bytes).being accessed is a word (two bytes).

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

Computers Organization & Assembly language Computers Organization & Assembly language 1515

TITILTITIL PROG2-3 transferring 6 bytes of DATAPROG2-3 transferring 6 bytes of DATA

.MODEL.MODEL SMALLSMALL

.STACK.STACK 6464

.DATA.DATA

ORGORG 10H10H

DATA_INDATA_IN DBDB 25H, 4FH, 85H, 1FH, 2BH, 0C4H25H, 4FH, 85H, 1FH, 2BH, 0C4H

ORGORG 28H28H

COPYCOPY DBDB 6 DUP (?)6 DUP (?)

.CODE.CODE

MAINMAIN PROCPROC FARFAR

MOVMOV AX, @DATAAX, @DATA

MOVMOV DS, AXDS, AX

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

Computers Organization & Assembly language Computers Organization & Assembly language 1616

MOVMOV SI, OFFSET DATA_IN ; SI points to data SI, OFFSET DATA_IN ; SI points to data copiedcopied

MOVMOV DI, OFFSET COPYDI, OFFSET COPY ; DI points to copy of ; DI points to copy of datadata

MOVMOV CX, 06HCX, 06H ; loop counter = 6; loop counter = 6

MOV_LOOP:MOV_LOOP: MOV MOV AL, [SI]AL, [SI] ; ; move the next byte from DATA to ALmove the next byte from DATA to AL

MOV MOV [DI], AL[DI], AL ; ; move the next byte to COPY areamove the next byte to COPY area

INCINC SISI ; increment DATA pointer; increment DATA pointer

INCINC DIDI ; increment COPY pointer ; increment COPY pointer

DECDEC CXCX ; decrement loop counter; decrement loop counter

JNZJNZ MOV_LOOP MOV_LOOP ; jump if loop counter not zero; jump if loop counter not zero

MOVMOV AH, 4CHAH, 4CH ; set up return; set up return

INTINT 21H21H ; return to DOS; return to DOS

MAINMAIN ENDPENDP

ENDEND MAINMAIN

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

Computers Organization & Assembly language Computers Organization & Assembly language 1717

Control Transfer InstructionsControl Transfer InstructionsIn an assembly program, it is often necessary to In an assembly program, it is often necessary to

transfer program control to a different location. transfer program control to a different location.

There are many instructions to achieve this.There are many instructions to achieve this.

The concept of The concept of FARFAR and and NEARNEAR

1.1. If control is transferred to a memory location within If control is transferred to a memory location within

the current code segment, it is the current code segment, it is NEARNEAR. This is called . This is called

IntrasegmentIntrasegment. Only IP register must be updated.. Only IP register must be updated.

2.2. If control is transferred outside the current code If control is transferred outside the current code

segment, it is segment, it is FARFAR. This is called . This is called IntersegmentIntersegment

jump CS and IP registers must be updated.jump CS and IP registers must be updated.

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

Computers Organization & Assembly language Computers Organization & Assembly language 1818

Conditional JumpsConditional JumpsThe 8086 Conditional Jump Instructions are:The 8086 Conditional Jump Instructions are:

Mnemonic Condition Tested "Jump IF …"

JA/JNBE (CF = 0) and (ZF = 0) above/ not below nor zero

JAE/JNB CF = 0 above or equal/ not below

JB/JNAE CF = 1 below/ not above nor equal

JBE/JNA (CF or ZF) = 1 below or equal/ not above

JC CF = 1 carry

JE/JZ ZF = 1 equal/ zero

JG/JNLE ((SF xor OF) or ZF) = 0 greater / not less nor equal

JGE/JNL (SF xor OF) = 0 greater or equal/ not less

JL/JNGE (SF xor OR) = 1 less /not greater nor equal

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

Computers Organization & Assembly language Computers Organization & Assembly language 1919

Mnemonic Condition Tested "Jump IF …"

JLE/JNG ((SF xor OF) or ZF) = 1 less or equal/not greater

JNC CF = 0 not carry

JNE/JNZ ZF = 0 not equal/ not zero

JNO OF = 0 not overflow

JNP/JPO PF = 0 not parity/ parity odd

JNS SF = 0 not sign

JO OF = 1 overflow

JP/JPE PF = 1 parity/ parity equal

JS SF = 1 sign

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

Computers Organization & Assembly language Computers Organization & Assembly language 2020

Control is transferred to a new memory location if a certain Control is transferred to a new memory location if a certain condition is met. The condition is met. The Flag registerFlag register is one that indicates the is one that indicates the current condition.current condition.

All conditional jumps are All conditional jumps are SHORTSHORT jumps. The target address jumps. The target address must be within -128 (must be within -128 (backwardbackward) to + 127 () to + 127 (forwardforward) bytes of ) bytes of the the IPIP..

The conditional jump is a two-byte instruction; one The conditional jump is a two-byte instruction; one op-codeop-code and the other is a value between and the other is a value between 0000 to to FF FF (offset address (offset address range).range).

In a In a backwardbackward jump, the second byte is the 2’s complement jump, the second byte is the 2’s complement of the displacement value. The of the displacement value. The target address = IPtarget address = IP of the of the instruction after the jump instruction + the second byte value. instruction after the jump instruction + the second byte value.

Similarly, in a Similarly, in a forwardforward jump, the target address = IP of the jump, the target address = IP of the following instruction + the second byte value.following instruction + the second byte value.

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

Computers Organization & Assembly language Computers Organization & Assembly language 2121

An Example of a Backward JumpAn Example of a Backward Jump1067:00001067:0000 B86610B86610 MOV AX, 1066MOV AX, 10661067:00031067:0003 8ED88ED8 MOV DS, AXMOV DS, AX1067:00051067:0005 B90500B90500 MOV CX, 0005 MOV CX, 0005 1067:00081067:0008 BB0000BB0000 MOV BX, 0000MOV BX, 00001067:000D1067:000D 02070207 ADD AL, [BX]ADD AL, [BX]1067:000F1067:000F 4343 INC BXINC BX1067:00101067:0010 4949 DEC CXDEC CX1067:00111067:0011 75FA75FA JNZ 000DJNZ 000D1067:00131067:0013 A20500A20500 MOV [0005], ALMOV [0005], AL1067:00161067:0016 B44CB44C MOV AH, 4CMOV AH, 4C1067:00181067:0018 CD21CD21 INT 21INT 21

The jump or label address isThe jump or label address is(0013 + FA = 000D).(0013 + FA = 000D).FAFA is the is the 2’s complement2’s complement of of -6.-6.The target address is The target address is -6 bytes from the IP-6 bytes from the IP of the of the next instruction.next instruction.

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

Computers Organization & Assembly language Computers Organization & Assembly language 2222

An Example of a Forward JumpAn Example of a Forward Jump

00050005 8A 47 028A 47 02 AGAIN:AGAIN: MOV AL, [BX] + 2MOV AL, [BX] + 2

00080008 3C 613C 61 CMP AL, 61HCMP AL, 61H

000A000A 72 0672 06 JB NEXTJB NEXT

000C000C 3C 7A3C 7A CMP AL, 7AH CMP AL, 7AH

000E000E 77 0277 02 JA NEXTJA NEXT

00100010 24 DF24 DF AND AL, 0DFHAND AL, 0DFH

00120012 88 0488 04 NEXT:NEXT: MOV [SI], ALMOV [SI], AL

The The NEXTNEXT label address is label address is(000CH + 0006H = 0012).(000CH + 0006H = 0012).

66 is the is the target address.target address.

The target address is The target address is 6 bytes from the IP6 bytes from the IP of the of the next instruction.next instruction.

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

Computers Organization & Assembly language Computers Organization & Assembly language 2323

Unconditional JumpsUnconditional Jumps

JMP labelJMP label It is an unconditional jump in which control is transferred to It is an unconditional jump in which control is transferred to the target location label.the target location label.

Unconditional jumps can take the following forms:Unconditional jumps can take the following forms:

1.1. SHORT jumpsSHORT jumps It is specified by the form:It is specified by the form:

JMP SHORT labelJMP SHORT label The address within the -128 (The address within the -128 (backwardbackward) to + 127 () to + 127 (forwardforward) )

bytes of the current bytes of the current IPIP.. The op-code is The op-code is EBEB and the other is a value between and the other is a value between 0000 to to

FF.FF. The directive The directive SHORTSHORT makes the jump more efficient and makes the jump more efficient and

makes it as 2-bytes instruction not 3-byte one.makes it as 2-bytes instruction not 3-byte one.

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

Computers Organization & Assembly language Computers Organization & Assembly language 2424

2.2. NEAR jumpsNEAR jumps It is specified by the form:It is specified by the form:

JMP labelJMP label The address within the current code segment. The address within the current code segment. The target address can be any of addressing The target address can be any of addressing

modes modes direct, register indirect or memory direct, register indirect or memory indirectindirect. .

Direct jumpDirect jump It is exactly like It is exactly like SHORTSHORT Jump except the Jump except the target address can be any where in the target address can be any where in the segment range from -32768 (segment range from -32768 (backwardbackward) to + ) to + 32767 (32767 (forwardforward) bytes of the current ) bytes of the current IPIP. .

Register indirect jumpRegister indirect jump, the target address is , the target address is in a register. Example:in a register. Example: JMP BXJMP BX IP = BX.IP = BX.

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

Computers Organization & Assembly language Computers Organization & Assembly language 2525

MemoryMemory indirect jump indirect jump, the target address is , the target address is the contents of two memory locations. the contents of two memory locations. Example: Example: JMP [DI]JMP [DI] IP = IP = The contents The contents of memory locations pointed by DI and DI + 1of memory locations pointed by DI and DI + 1 ..

3.3. FAR jumpsFAR jumps It is specified by the form:It is specified by the form:

JMP FAR PTR labelJMP FAR PTR label The target address out of the current code The target address out of the current code

segment.segment. CSCS and and IPIP must be changed. must be changed.

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

Computers Organization & Assembly language Computers Organization & Assembly language 2626

CALL StatementsCALL StatementsCALLCALL instruction is used to call a procedure. instruction is used to call a procedure.

It is used to perform tasks that need to beIt is used to perform tasks that need to be performed performed frequently.frequently.

It makes programs more structured.It makes programs more structured.

CALLCALL may be may be NEARNEAR (i.e. the target address in the (i.e. the target address in the current segment) or current segment) or FAR FAR (i.e. the target address out the (i.e. the target address out the current segment).current segment).

The following is a NEAR CALL example: The following is a NEAR CALL example: (different IP, same CS)

12B0:020012B0:0200 BB1295BB1295 MOV BX, 9512MOV BX, 951212B0:0203 12B0:0203 E8FA00E8FA00 CALLCALL 0300030012B0:0206 12B0:0206 B82F14B82F14 MOV AX, 142FMOV AX, 142F

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

Computers Organization & Assembly language Computers Organization & Assembly language 2727

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:030012B0:0300 5353 PUSH BXPUSH BX12B0:0301 12B0:0301 ...... ...... ......... ... ... ... ... ... ... ... ......12B0:030912B0:0309 5B5B POP BX POP BX 12B0:030A12B0:030A C3C3 RETRET

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

Computers Organization & Assembly language Computers Organization & Assembly language 2828

Assembly Language SubroutinesAssembly Language Subroutines.CODE.CODE

MAINMAIN PROCPROC FARFAR ; This is the entry point for DOS; This is the entry point for DOS

MOVMOV AX, @DATAAX, @DATA

MOVMOV DS, AXDS, AX

CALLCALL SUBR1SUBR1

CALLCALL SUBR2SUBR2

CALLCALL SUBR3SUBR3

CALLCALL SUBR1SUBR1

MOVMOV AH, 4CHAH, 4CH ; set up return; set up return

INTINT 21H21H ; return to DOS; return to DOS

MAINMAIN ENDPENDP

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

Computers Organization & Assembly language Computers Organization & Assembly language 2929

;----------------------------------------------------------------------------------;----------------------------------------------------------------------------------SUBR1SUBR1 PROCPROC

. . . . . .

. . . . . . RETRET

SUBR1SUBR1 ENDPENDP;----------------------------------------------------------------------------------;----------------------------------------------------------------------------------SUBR2SUBR2 PROCPROC

. . . . . .

. . . . . . RETRET

SUBR2SUBR2 ENDPENDP ;----------------------------------------------------------------------------------;----------------------------------------------------------------------------------SUBR3SUBR3 PROCPROC

. . . . . .

. . . . . . RETRET

SUBR3SUBR3 ENDPENDP;----------------------------------------------------------------------------------;----------------------------------------------------------------------------------

ENDEND MAINMAIN ; This is the exit point; This is the exit point

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

Computers Organization & Assembly language Computers Organization & Assembly language 3030

Rules for Names in Assembly LanguageRules for Names in Assembly Language

NamesNames make programs easier to read and make programs easier to read and maintain.maintain.

Label name Label name must be unique.must be unique.

Names Names consist of alphabetic letters upper consist of alphabetic letters upper and lower case, the digits from 0 to 9, the and lower case, the digits from 0 to 9, the special characters include ?, . , @, special characters include ?, . , @, underline _, and $.underline _, and $.

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

Computers Organization & Assembly language Computers Organization & Assembly language 3131

The EndThe End