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

Slides:



Advertisements
Similar presentations
There are two types of addressing schemes:
Advertisements

ACOE2511 Assembly Language Arithmetic and Logic Instructions.
8086 Assembly Language Programming I
The 8086 Assembly Programming Data Allocation & Addressing Modes
Lecture 2 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Flow Control Instructions
8051 ASSEMBLY LANGUAGE PROGRAMMING
80x86 Processor Architecture
INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE
INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE
Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
An Introduction to 8086 Microprocessor.
Microprocessor Programming II
CDP ECE Spring 2000 ECE 291 Spring 2000 Lecture 7: More on Addressing Modes, Structures, and Stack Constantine D. Polychronopoulos Professor, ECE.
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
1/2002JNM1 Positional Notation (Hex Digits). 1/2002JNM2 Problem The 8086 has a 20-bit address bus. Therefore, it can access 1,048,576 bytes of memory.
Debug and Assembler By, B.R.Chandavarkar Lect. COMP Department NITK, Surathkal.
Introduction to 8086 Assembly Language Assembly Language Programming University of Akron Dr. Tim Margush.
Faculty of Engineering, Electrical Department,
CE302 MICROPROCESSORS Levent EREN Izmir University of Economics.
The x86 PC Assembly Language, Design, and Interfacing By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey © 2010, 2003, 2000, 1998 Pearson.
Chapter Five–80x86 Assembly program development Principles of Microcomputers 2015年11月15日 2015年11月15日 2015年11月15日 2015年11月15日 2015年11月15日 2015年11月15日 1.
Chapter 4 Requirements for Coding in Assembly Language.
Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice.
UHD:CS2401: A. Berrached1 The Intel x86 Hardware Organization.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
Addressing modes of 8086.
Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string.
In Class Program Write, assemble and test a program: –Use the DB directive to define the following list of numbers and name it array: 31h, 32h, 33h, 34h.
Microprocessors used in Personal Computers. The Memory Map of a Personal Computers Transient Program Area (TPA): Holds the operating system (interrupt.
LEA instruction The LEA instruction can be used to get the offset address of a variable Example ORG 100h MOV AL, VAR1 ; check value of VAR1 by moving it.
Review of Assembly language. Recalling main concepts.
2/20/2016CAP 2211 Flow Control Instructions. 2/20/2016CAP 2212 Transfer of Control Flow control instructions are used to control the flow of a program.
Internal Programming Architecture or Model
Selection and Iteration Chapter 8 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Assembly language programming
Instruction set Architecture
Format of Assembly language
Assembly Language programming
Instruksi Set Prosesor 8088
Microprocessor and Assembly Language
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Assembly IA-32.
Assembly Language Programming Part 2
Chapter 3 Addressing Modes
Computer Organization and Assembly Language (COAL)
Defining Types of data expression Dn [name] expression Dn [name]
Symbolic Instruction and Addressing
Introduction to Assembly Language
Data Addressing Modes • MOV AX,BX; This instruction transfers the word contents of the source-register(BX) into the destination register(AX). • The source.
Flags Register & Jump Instruction
Lecture 1 Instruction set of 8086 Лектор: Люличева И.А. 1.
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
Program Logic and Control
Program Logic and Control
Symbolic Instruction and Addressing
Flow Control Instructions
Lecture 06 Programming language.
8051 ASSEMBLY LANGUAGE PROGRAMMING
Assembler Directives end label end of program, label is entry point
Chapter 6 –Symbolic Instruction and Addressing
Assembly Language for Intel 8086 Jump Condition
Chapter 7 –Program Logic and Control
Chapter 8: Instruction Set 8086 CPU Architecture
Chapter 7 –Program Logic and Control
Introduction to 8086 Assembly Language
Presentation transcript:

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