Assembly Language(Lab Manual)

15
1 Lab Manual CSC159 ASSEMBLY LANGUAGE TOPIC 1 INTRODUCTION 1.1 What is Assembly Language? A specific set of instructions for a particular computer system. It provides a direct correspondence between symbolic statements and machine language. a programming language with a one-to-one correspondence between its statement and a computer s machine language 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. In contrast, high-level languages such as Pascal, BASIC, FORTRAN and COBOL contain powerful statements that are translated into many machine instructions by the compiler. 1.2 What is an Assembler? An assembler is a program that converts source-code programs into machine language. An assembler is a program that translates a program written in assembly language into machine language. Each type of computer has a different assembly language, because the computer s design influences the instructions it can execute. Assembly Language Assembler Machine Language (source code) (object code) 1.3 Why learn Assembly Language? 1. To learn about the computer s architecture and operating system. 2. To learn assembly language for its utility: a. certain types of programming are difficult or impossible to do in high-level languages. For example, direct communication with the computer s operating system. Therefore, a high speed program may have to be written suing a minimum memory space.

Transcript of Assembly Language(Lab Manual)

Page 1: Assembly Language(Lab Manual)

1 Lab Manual CSC159

ASSEMBLY LANGUAGE

TOPIC 1 INTRODUCTION

1.1 What is Assembly Language?

A specific set of instructions for a particular computer system. It provides a direct correspondence between symbolic statements and

machine language. a programming language with a one-to-one correspondence between its

statement and a computer’s machine language 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. In contrast, high-level languages such as Pascal, BASIC, FORTRAN and COBOL contain powerful statements that are translated into many machine instructions by the compiler.

1.2 What is an Assembler?

An assembler is a program that converts source-code programs into machine language.

An assembler is a program that translates a program written in assembly language into machine language.

Each type of computer has a different assembly language, because the computer’s design influences the instructions it can execute.

Assembly Language Assembler Machine Language

(source code) (object code)

1.3 Why learn Assembly Language?

1. To learn about the computer’s architecture and operating system.2. To learn assembly language for its utility:

a. certain types of programming are difficult or impossible to do in high-level languages. For example, direct communication with the computer’s operating system. Therefore, a high speed program may have to be written suing a minimum memory space.

b. write a subroutine program in assembly language and called this subroutine from the high-level language. (this is to remove restrictions

Page 2: Assembly Language(Lab Manual)

2 Lab Manual CSC159

where there are certain operations which are not allowed in a high-level language)

1.4 What is a Compiler?

A compiler is a program, that translate the high-level language programming into machine language.

A single command of high-level language is translated into many machine instructions by the compiler.

High-level language Compiler Machine Language

(source code) (object code)

1.5 Editor

An assembly language can be written using any text editor and save in a computer as a text file. Example: EDIT, notepad, etc.

1.6 Debugger

A debugger is a program that allows you to examine registers and memory and to step through a program one statement at a time to see what is going on. In assembly language, you will depend upon this ability to see what the CPU is doing.

Page 3: Assembly Language(Lab Manual)

3 Lab Manual CSC159

TOPIC 2

SYSTEMS ARCHITECTURE

2.1. CENTRAL PROCESSING UNIT (CPU)

Microcomputers use a single CPU, as shown in figure 1. The most basic tasks handled by the CPU are :

i. Find and load the next instructionii. Execute the instruction:

Fetch data from memory/registers

Store data in memory/registers

Perform calculations and comparisons

Modify the instruction pointer

Figure 1.0 CPU parts

The CPU is divided into two parts: the arithmetic logic unit (ALU) and the control unit (Figure 1.0).

ALU functions : carries out arithmetic, logical, and shifting operations.

Page 4: Assembly Language(Lab Manual)

4 Lab Manual CSC159

Control unit functions: fetches data and instructions and decodes addresses for the ALU.

2.2 DATA BUS

The bus is a series of parallel wires that transmit data between the various parts of the CPU. Both control signals and data bits are used when fetching a memory word and placing it in a register. A bus is bi-directional if it allows data to be transferred in two directions.

2.3 REGISTERS

Within the CPU are high–speed storage areas, which are directly linked to the control unit and the arithmetic logic unit. Because of this, instructions using only registers execute more quickly than do instructions using conventional memory. The Intel instruction set requires the use of at least one register for nearly all instructions.

Registers are special work areas inside the CPU, designed to be accessed at high speed. The registers are 16 bits long, but you have the option of accessing the upper or lower halves of the four data registers: AX, BX, CX and DX

Type of registers

1. Data Registers

AX, BX CX and DX

Data registers or general-purpose registers, are used for arithmetic and data movement.

Each register may be addresses as either a 16-bit or 8-bit value.

Example:

16-bit AX register

AH register AL register (8 bit) Upper bits (8 bits) lower bits

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

Page 5: Assembly Language(Lab Manual)

5 Lab Manual CSC159

For example, if AX = 126Fh, AL will equal to 6Fh.

Each general-purpose register has special attributes:

AX (accumulator). AX is called the accumulator register because it is favored by the CPU for arithmetic operations.

BX (base). BX register can perform arithmetic and data movement, and it has special addressing abilities. It can hold a memory address that points to another variable. Three other registers with this ability are SI, DI and BP.

CX (counter). The CX register acts as a counter for repeating or loping instructions. These instructions automatically repeat and decrement CX and quit when it equals 0.

DX (data) . The DX register has a special role in multiply and divide operations. When multiplying, for example, DX holds the high 16 bits of the product.

2. Segment registers

The CPU contains four segment registers, used as base locations for program instructions, data , and the stack.

The segment registers are as follows:

CS (code segment). The CS register holds the base location of all executable instructions (code) in a program.

DS (data segment). The DS register is the default base location for variables. The CPU calculates their locations using the segment value in DS.

SS (stack segment). The SS register contains the base location of the stack.

ES (extra segment). The ES register is an additional base location for memory variables.

3. Index Registers

Index registers contain the offsets of variables. The term offsets refers to the distance of a variable, label, or instruction from its base segment Index registers speed up processing of strings, array, and other data structures containing multiple elements. The index registers are:

SI (source index). This register takes its name from the string movement instructions, in which the source string is pointed to by the SI register. SI

Page 6: Assembly Language(Lab Manual)

6 Lab Manual CSC159

usually contains an offset value from the DS register, but it can address any variable.

DI (destination index). The DI register acts as the destination for string movement instructions. It usually contains an offset from the ES register, but it can address any variable.

BP (base pointer) . The BP register contains an assumed offset from the SS register, as does the stack pointer. The BP register is often used by a subroutine to locate variables that were passed on the stack by a calling program.

4. Special Registers.

IP (instruction pointer). The IP register always contains the offset of the next instruction to be executed. CS and IP combine to form the complete address of the next instruction about to be executed.

SP (stack pointer). The SP register contains the offset, or distance from the beginning of the stack segment to the top of the stack. The SS and SP registers combine to form the complete top-of-stack address.

5. Flags Register

The flags register is a special 16-bit register with individual bit positions assigned to show the status of the CPU or the results of arithmetic operations. Each relevant bit position is given a name; other positions are undefined.

Bit Position

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

x x x x O D I T S Z x A x P x C

O = Overflow S = SignD = Direction Z = ZeroI = Interrupt A = Auxiliary CarryT = Trap P = Parityx = undefined C = Carry

A flag or bit is set when it equals 1 ; it is clear (or reset) when it equals 0. The CPU sets flags by turning on individual bits in the Flags register. There are two basic types of flags : control flags and status flags.

Page 7: Assembly Language(Lab Manual)

7 Lab Manual CSC159

Flags

Control FlagsIndividual bits may be set in the Flags register by the programmer to control the CPU’s operation. These are the Direction, Interrupt, and Trap flags

The Direction flag controls the assumed direction used by string processing instructions. The flag values are 1 = up and 0 = down. The programmer controls this flag, using the STD and CLD instructions.

The Interrupt flag makes it possible for external interrupts to occur. These interrupts are caused by hardware devices such as the keyboard, disk drives, and the system clock timer. Often we briefly disable interrupts when peerforming some critical operation that cannot be interrupted. The flag values are 1 = enabled, and 0 = disabled; they are controlled by the CLI and STI instructions.

The Trap flag determines whether or not the CPU will be halted after each instruction. Debuggers set this flag so programmers can single-step (trace) through their programs one instruction at a time. The flags values are 1 = trap on and , 0 = trap off; the flag may be set by the INT 3 instruction.

Status Flags

The Status flag bits reflect the outcome of arithmetic and logical operations performed by the CPU. These are the Overflow, Sign, Zero, Auxiliary Carry, Parity, and Carry flags.

The Carry flag is set when the result of an unsigned arithmetic operation is too large to fit into the destination. For example, if the values 200 and 56 were added together and placed in an 8-bit register (such as AL ), the result would be too large to fit; the Carry flag would be set. The flag values are 1 = carry, 0 = no carry.

The Overflow flag is set when the signed result of an arithmetic operation is too large to fit the destination area. The flag values are 1 = overflow and 0 = no overflow.

The Sign flag is set when the result of an arithmetic or logical operation generates a negative result. Since a negative number always has a 1 in the highest bit position, the Sign flag is always a copy of the destination’s sign bit. The flag values are 1 = negative, 0 = positive.

Page 8: Assembly Language(Lab Manual)

8 Lab Manual CSC159

The Zero flag is set when the result of an arithmetic or logical operations generates a result of zero. The flag is used primarily by jump and loop instructions, in order to allow branching to a new location in a program based on the comparison of two values. The flag values are 1 = zero, 0 = not zero.

The Auxiliary Carry flag is set when an operation causes a carry from bit 3 to bit 4 (or borrow from bit 4 to bit 3) of an operand. It is rarely used by the programmer. The flag values are 1 = carry, and 0 = no carry.

The Parity flag reflects the number of bits in the result of an operation that are set. If there is an even number of bits, the Parity is even. If there is an odd number of bits, the Parity is odd. This flag is used by the operating system to verify memory integrity and by communications software to verify correct transmission of data.

2.4 Clock

Each of the individual operations that place within the CPU must be synchronized by a clock. The most basic unit of time for machine instructions is called the machine cycle. Each tick of the clock determines when the next machine cycle will occur. Machine instructions on Intel processors generally take between 3 and 20 clocks to execute, depending on whether they access memory or need to calculate a complex address.

Page 9: Assembly Language(Lab Manual)

9 Lab Manual CSC159

TOPIC 3ASSEMBLY LANGUAGE STATEMENTS

3.1 Data Definition Directives

Define Byte (DB) Define Word (DW) Define Doubleword (DD)

A variable is a symbolic name for a location in memory where some data are stored. In assembly language, variables are identified by labels. Each label shows the starting location of a variable.

Example:

Declaration Stored As

aList db ‘ABCD’ A B C D

0 1 2 3

So if the first letter were at offset 0, the next letter would be at offset 1, the next at 2, and so on.

We use data definition directives to allocate storage, based on the following predefined types:

Page 10: Assembly Language(Lab Manual)

10 Lab Manual CSC159

Description Bytes Attribute

DB Define byte 1 byte

DW Define word 2 word

DD Define doubleword 4 doubleword

Define Byte (DB)

The DB directives allocates storage for one or more 8-bit values. The following syntax diagram shows that name is optional, and only one initialvalue is required. If more are supplied, they must be separated by commas:

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 (?).

If the value is signes, it has a range of -128 to +127; if unsigned, the range is 0 to 255.

Examples:

char db ‘A’ ; ASCII character

signed1 db -128 ; smallest signed value

signed2 db +127 ; largest signed value

unsigned1 db 0 ; smallest unsigned value

unsigned2 db 255 ; largest unsigned value

Page 11: Assembly Language(Lab Manual)

11 Lab Manual CSC159

Multiple values

A list of 8-bit numbers may be grouped under a single label, with the values separated by commas.

Example:

list db 10, 20, 30, 40

Note:

When a hexadecimal number begins with a letter (A - F), a leading zero is added to prevent the assembler from interpreting it as a label.

Example: size db 0Ah

A variables contents may be left undefined by using the question mark (?). Or numeric expression can initialize a variable with a value that is calculated at assembly time.

Example : count db ?

ages db ?, ? , ?, ?,?

size db 10*20

A string may be assigned to a variable.

Example : c_string db “Good afternoon”

Page 12: Assembly Language(Lab Manual)

12 Lab Manual CSC159

Define Word (DW)

The DW directive creates storage for one or more 16-bit words.

Syntax:

[name] DW initialvalue [, initialvalue]

Initialvalue may be any 16-bit numeric value from 0 to 65,535 (FFFFh)

If initialvalue is signes, the acceptable range is -32,768 (8000h) to +32,767 (7FFFh).

Reverse Storage Format

The assembler reverses the bytes in a word value when storing it in memory; the lowest byte occurs at the lowest address. When the variable is moved to a 16-bit register, the re-reverses the bytes.

Example : 2AB6h is stores in memory as B6 2A

Data declaration: value1 dw 2AB6h

Storage : B6 2A

Page 13: Assembly Language(Lab Manual)

13 Lab Manual CSC159

Define Doubleword (DD)

The DD directive creates storage for one or more 32-bit doublewords.

Syntax :

[name] DD initialvalue [, initialvalue]

Example: the value 12345678h would be stored in memory as

Offset: 00 01 02 03

Value: 78 56 34 12