You are on page 1of 13

1Lab Manual CSC159




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 computers 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.


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 computers design influences the instructions it can execute.

Assembly Language (source code)


Machine Language (object code)


Why learn Assembly Language?

1. To learn about the computers 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 computers operating system. Therefore, a high speed program may have to be written suing a minimum memory space. write a subroutine program in assembly language and called this subroutine from the high-level language. (this is to remove restrictions


2Lab 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 (source code)


Machine Language (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.

3Lab Manual CSC159




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

The most basic tasks

i. ii.

Find and load the next instruction 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.

4Lab Manual CSC159

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


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.


REGISTERS Within the CPU are highspeed 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


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 (8 bit) Upper bits AL register (8 bits) lower bits

Instructions may address either 16-bit or 8-bit data registers from the following list: AX AH AL BX BH BL CX CH CL DX DH DL

5Lab 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.


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.


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

6Lab 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.


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 x

14 x

13 x

12 x

11 O

10 D I

8 T

7 S

6 Z x

4 A x

2 P x

0 C

O D I T x

= Overflow = Direction = Interrupt = Trap = undefined

S = Sign Z = Zero A = Auxiliary Carry P = Parity 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.

7Lab Manual CSC159

Flags Control Flags Individual bits may be set in the Flags register by the programmer to control the CPUs 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 destinations sign bit. The flag values are 1 = negative, 0 = positive.

8Lab 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.

9Lab Manual CSC159



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.


Declaration aList db ABCD

Stored As A B C D

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:

10Lab Manual CSC159

Description DB DW DD Define byte Define word Define doubleword

Bytes 1 2 4 byte word



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:


[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.


char signed1 signed2 unsigned1 unsigned2

db db db db db

A -128

; ASCII character ; smallest signed value

+127 ; largest signed value 0 255 ; smallest unsigned value ; largest unsigned value

11Lab Manual CSC159

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



10, 20, 30, 40


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 ?



?, ? , ?, ?,?




A string may be assigned to a variable. db Good afternoon

Example : c_string

12Lab Manual CSC159

Define Word (DW)

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


DW initialvalue [, initialvalue]

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

If initialvalue is signes, (7FFFh).

the acceptable range is

-32,768 (8000h)



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:


dw 2AB6h

Storage :



13Lab Manual CSC159

Define Doubleword (DD)

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


DD initialvalue [, initialvalue]

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

Offset: Value:

00 78

01 56

02 34

03 12