Chapter 6_1_Elements_of_Assembly_Language.pdf
Document Details
Uploaded by JawDroppingAestheticism
UiTM Cawangan Melaka
Tags
Full Transcript
Chapter 6: Introduction to Assembly Language LESSON OUTCOME LO1: Define & describe basic elements of assembly language in Intel 8086 Architecture LO2 :Identify the type of addressing modes. What is Assembly Language (AL)? To learn how a computer and its software really w...
Chapter 6: Introduction to Assembly Language LESSON OUTCOME LO1: Define & describe basic elements of assembly language in Intel 8086 Architecture LO2 :Identify the type of addressing modes. What is Assembly Language (AL)? To learn how a computer and its software really work, you need to view them at machine level. Assembly language is a specific set of instructions for a particular computer system. AL teaches you about the way the computer’s hardware and OS work together and how application programs communicate with the OS. It is a programming language with a one-to-one correspondence between its statement and a computer’s main machine language. Cont… Assembly Language is a specific set of instructions for a particular computer system. An Assembler is a program that translates a program written in assembly language into machine language, which may in turn be executed by the computer. (runs under the disk operating systems MS-DOS or PC-DOS). A machine instruction : is a binary code that has special meaning for a computer’s CPU which tells the CPU to perform a task. The instruction set : is the entire body of machine instructions available for a single CPU, determined by its manufacturer. Why is AL important to learn? Provides opportunity to know more about the operations of the PC Enable control of the PC – access to specific hardware features Quicker, smaller and have larger capacities compared to other HL languages Machine Instructions A machine instruction is a binary code that has a special meaning for a computer’s CPU – it tells the computer to perform a task. Each machine instruction is precisely defined when the CPU is constructed, and it is specified to that type of CPU. For example: – 00000100 Add a number to the AL register. – 10100011 Move the AX register to another register Assembly Language Instructions Assembly Language is called a low-level language because it is close to machine language in structure and function. each assembly language instruction corresponds to one machine instruction A typical 2-byte IBM-PC machine instruction might be as follows : B0 05 1st byte (B0) : 2nd byte (05) : Operand Operation code identifies it as a MOV (move) instruction The complete instruction : moves the number 5 to a register called AL. Register : high-speed storage locations inside the CPU which are used by nearly every instructions. 2-7 Assembly Language Instructions (cont.) An assembly language statement consists of : [name] [mnemonic] [operands] [;comment] Although it is possible to program directly in machine language using numbers, assembly language makes the job easier. The Assembly Language instruction to move 5 to the AL register is : destination source mov al,5 operand 2-8 Assembly Language Instructions (cont.) Mnemonic : is a short alphabetic code that ‘assist the memory’ in remembering a CPU instructions. an instruction e.g. MOV (move) a directive e.g. DB (define byte) Operand : an instructions may contain zero, one or two operands. a register e.g AX a memory variable e.g. count a n immediate value/operand e.g. 10 Example of instruction using operands are : push ax ; one register mov ax,bx ; two register add count,cl ; memory variable, register mov bx,1000h ; register, immediate value 2-9 Assembly Language Instructions (cont.) Comment : the beginning of a comment is marked by a semicolon (;) character → ignored by the assembler Samples are as follows : ; This entire line is a comment mov ax,bx ; copy the BX register into AX 2-10 The CPU Registers Registers are high-speed storage locations inside the CPU which are used by nearly every instructions They are identified by 2-letter names, such as AH, AL, AX and so on. We refer to machine instructions using hexadecimal numbers because they take up less writing space. The CPU Registers … (cont.) Registers : → special work areas inside the CPU designed to be accessed at high speed. → 16 bits long but you have the option of accessing the upper or lower halves of the four data register : Data register 16 bit: AX, BX, CX, DX 8 bit : AH, AL, BH, BL, CH, CL, DH, DL Segment register : CS, DS, SS, ES Index register : SI, DI, BP Special register : IP, SP Flags register : overflow, direction, interrupt, trap, sign, zero, auxiliary carry, parity, carry 2-12 The CPU Registers (cont.) Data register : → four registers a.k.a general purpose register → used for arithmetic and data movement → may be addressed as either 16-bit or 8-bit value → example : bits : 15…………………………………..0 16-bit AX register AH register AL register bits : 7…………….…0 7…………..….0 * Bit position are always numbered from right to left, starting with 0. 2-13 The CPU Registers (cont.) Instructions may address either 16-bit or 8-bit data registers from the following list : AX BX CX DX AH AL BH BL CH CL DH DL When a 16-bit register is modified, so is its corresponding 8-bit half register. Example : AX register contains 0000h. If we move 126Fh to AX, AL will change to 6Fh and AH will change to 12h. 2-14 Intel 8086 Architecture There are 14 internal registers in Intel 8086 All of them are represented in 16-bit mode or equivalent to 4 digits in hexadecimal They are grouped into several categories For general-purpose registers, each of these is a combination of two 8-bit registers which are separately accessible as AL, BL, CL, DL (the "low'' bytes) and AH, BH, CH, and DH (the "high'' bytes) For example, if AX contains the 16-bit number 1234h, then AL contains 34h and AH contains 12h Registers Instruction PC / Pointer IP General-purpose Special-purpose segment registers Status register registers registers AX = AH + AL, SP, BP, SI, DI CS, DS, ES, SS FLAGS BX = BH + BL , CX = CH + CL, DX = DH + DL Figure 1: 14 Registers in Intel 8086 General Purpose Registers AX BX CX DX AH AL BH BL CH CL DH DL AX (accumulator) : favored by the CPU for arithmetic operations involving i/o and most arithmetic such as MUL and DIV. BX (base) : can perform arithmetic and data movement, has special addressing abilities; can hold memory address that points to another variable. CX (counter) : acts as a counter for repeating or looping instructions; automatically repeat and decrement CX and quit when it equal to 0. Or a value to shift/rotate bits left or right DX (data) : has a special role in multiply and divide operations. Used together with AX for the word-size MUL and DIV operations 2-17 Special Purpose Registers There are 4 special-purpose registers: – Stack Pointer Register (SP) Holds add of top of stack – Base Pointer Register (BP) Holds add of ref point in the stack – Source Index Register (SI) Holds memory add of source – Destination Index Register (DI) Holds memory add of destination Segment registers There 4 segment register: – Code Segment Register (CS) Holds selector for code segment – Data Segment Register (DS) Holds selector for data segment – Extra Segment Register (ES) Holds selector for extra segment – Stack Segment Register (SS) Holds selector for stack segment Status Register The status register, FLAGS, is a collection of 1-bit values, which reflect the current state of the processor and the results of recent operations 9 of the 16 bits are used in the 8086. In this lesson you will only learn 6 flags: – Carry flag – Parity flag – Sign flag – Zero flag – Overflow flag – Auxiliary Flag Instruction Pointer The instruction pointer (sometimes called Program Counter - PC), IP, gives the address of the next instruction to be executed, relative to the code segment. A sample program : Assembly Language programs are made up primarily of instructions and operands. instructions : tell the CPU to carry out an action memory operands or variables : memory locations where data is stored Lets write a short assembly language program that adds three numbers together and stores them in a variable called sum. The line are numbered for convenience. 1: mov ax,5 ; move 5 into the AX register 2: add ax,10h ; add 10h to the AX register 3: add ax,20h ; add 20h to the AX register 4: mov sum,ax ; store AX in sum 5: int 20 ; end the program 2-22 ADDRESSING MODES Introduction Instruction contains opcode followed by operands. Operands specify where the data is. The different ways to address operands are called the addressing modes. Mode Location of data Immediate In the instruction itself Register In a register Memory At some address in memory Immediate Addressing Mode An immediate operand is just a number or a constant value. For example: MOV AH, 0Dh; 0Dh is an immediate data in hexadecimal form In this mode, data (constant) is a part of the instruction. Transfer is from a number stored immediately after the MOV opcode. Immediate value should not exceed the destination register. E.g. MOV AL, 25H or MOV CX, 23F6H E.g. MOV DS, 0123H; → illegal Register Addressing Mode In register mode, any of the following registers can be used to store data: – general and special-purpose 16-bit and 8-bit registers – segment registers. Name of the register which has data (source) and the name of the register which will have its data (destination) is specified. Transfer is from register to register. Source and destination should be of the same size. – E.g. MOV AL, CL; MOV AX, DX – E.g. MOV AX, CL; → Illegal Memory Addressing Mode In memory mode, the operand gives the address of a location in main memory to use in the operation. Example: MOV DX, [1234h] ; represents the 1234 address of a memory location Two types; direct and indirect memory addressing Memory Addressing Mode Direct Addressing Mode – Effective address is given in the instruction itself. – Transfer is between register and a memory address. – E.g. MOV AL, [2000h]; MOV [2000h], AX – E.g. MOV [2000h], [3000h]; → illegal Memory Addressing Mode Indirect Addressing Mode – Name of the base register (BX or BP) or the name of the index register (SI or DI) is specified in the instruction. – Transfer is between a register and a memory address specified in one or two registers. – E.g. MOV AL, [BX], MOV [DI], AL – E.g. MOV AH, [BX+SI] Data Definition Directives In assembly language, we define storage for variables using data definition directives. Data definition directives create storage at assembly time; can even initialize a variable to a starting value. Directive Description No. of bytes Attribute DB Define byte 1 Byte DW Define word 2 Word DD Define 4 Doubleword doubleword DQ Define quadword 8 Quadword DT Define 10 bytes 10 tenbyte * Attribute : the basic unit of storage used when the variable was defined 2-30 Data Definition Directives (cont.) Example : char db ‘A’ The DB directive creates an 8-bit variables called char. The assembler initializes the variable to a starting value, which in this example is 41h (ASCII code for the letter A). 2-31 Data Definition Directives (cont.) Define byte (DB) : - creates (allocate) storage for a byte or group of bytes and optionally assigns starting values. - the syntax : [name] DB initialvalue [,initialvalue]... - initialvalue can be one or more 8-bit numeric values, a string constant, a constant expression, or a question mark(?) - example 1 : list db 10h,20h,41h,2 - assume that the variable list is stored at location 00h : Offset : 00 01 02 03 Values : 10 20 41 02 2-32 Data Definition Directives (cont.) Define byte (DB) : - example 2 : db 10,’A’,20h,’ABC’ - each storage byte for this data is shown in hexadecimal : Values: 0A 41 20 41 42 43 - example 3 : The DUP operator repeats a single or multiple-byte value. For example, 20 bytes containing all binary zeros would be coded as: db 20 dup(0) 2-33 Data Definition Directives (cont.) Define byte (DB) : - when hexadecimal number begins with a letter (A-F), a leading zero is added. - example 4 : db A6h ; incorrect db 0A6h ; correct 2-34 Data Definition Directives (cont.) Define word (DW) : - creates storage for a word or list of words and optionally gives them starting values. - the syntax : [name] DW initialvalue [,initialvalue]... - initialvalue can be any 16-bit numeric values up to 0FFFFh (65535), a constant expression, or a question mark(?) - if initialvalue is signed, the acceptable range is -32768 to +32767 2-35 Data Definition Directives (cont.) Define word (DW) : - Reversed Storage Format : → the assembler reverses the bytes in a word value when storing it in memory; the lowest byte occurs at the lowest address - Example : When the variable 2AB6h is moved to a 16-bit register, it is stored in memory as B6 2A. Data declaration : value1 dw 2AB6h Storage : B6 2A low byte high byte 2-36 Data Definition Directives (cont.) Define word (DW) : - DUP operator : → allocated multiple occurrences of a value. - Example : The following DW directive crates a list of four 16-bit integers : intarray dw 4 dup(1234h) Assume that intarray starts at offset 00, thus the storage of bytes might be : Offset: 00 01 02 03 04 05 06 07 Contents: 34 12 34 12 34 12 34 12 2-37 Data Definition Directives (cont.) Define doubleword (DD) : - create storage for a 32-bit doubleword variable, with the option of giving it a starting value. - The syntax : [name] DD initialvalue [,initialvalue]... - initialvalue can be a binary number up to 0FFFFFFFFh, a segment-offset address, a 4-byte encoded real number, or a decimal real number. - example : the value 12345678h would be stored in memory as: Offset: 00 01 02 03 Contents: 78 56 34 12 2-38 The End Question