Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Introduction to Assembly Language Programming

Table of Contents

1.0 General Purpose Registers........................................................................................................................... 2


1.1 Segments ................................................................................................................................................... 3
1.2 Segment Offset.......................................................................................................................................... 4
1.3 Pointer Register......................................................................................................................................... 4
1.4 Interrupts ................................................................................................................................................... 5
2.0 Programming in Assembly ............................................................................................................................ 5
2.1 Statement .................................................................................................................................................. 5
2.2 Format of a statement .............................................................................................................................. 5
2.3 Example programs and Practical Considerations during programming................................................ 9
3.0 Real world coding using an assembler.......................................................................................................10

Faculty Of Information Technology, University Of Moratuwa 1


1.0 General Purpose Registers

The registers in the microprocessor that can be used by the programmer for, programming is called
General Purpose Registers. Other registers which is used by the microprocessor for its own internal tasks
are called Special Purpose Registers.

General purpose Register for the x86 family of processors are; AX, BX, CX, and DX. These registers are
16-bit wide while their 32-bit version is called; EAX, EBX, ECX and EDX. A 16-bit register is divided into
two 8-bit register called “high” and “low” portions. For register AX those portions are labeled as AH and
AL.

31 16 15 8 7 0

AH AL

AX

EAX

AX register Known as the Primary Accumulator is used for operations involving inputs/outputs and
arithmetic

BX register Known as the Base Register is used to hold the index in addressing. Can also be used in
computation.

CX register Known as Count Register. Used to control loops. Can also be used in computation.

DX register Known as Data Register. Used for input/output operations.

There are several other registers such as Segment Registers and Index Registers.

Faculty Of Information Technology, University Of Moratuwa 2


1.1 Segments
Segments are special areas in memory that is used in a program to hold its code, data and stack. A
segment begins on a paragraph boundary. We use special registers which we call Segment Registers to
indicate such boundaries.

SS
ssss
Stack
DS
Data Relocatable
CS
in Memory

Code
CS Offset

You may define any number of segments. To address a particular segment it is necessary to change the
value of the appropriate register. Following are the most common segments in a program.

Code Segment Contains the machine instructions that are to be executed. Code segment
address is stored in the Code Segment (CS) register.

Data Segment Contains the data defined in the programs, constants and data structures. The
Data Segment (DS) register addresses the data segment.

Stack Segment Contains any data and addresses that the program needs to save temporary or
used in sub routines. Stack Segment (SC) register holds the address.

Faculty Of Information Technology, University Of Moratuwa 3


1.2 Segment Offset
Within a program all memory locations within a segment is relative to the segments starting address.
The distance in byte from the segment address to another memory location within the segment is called
the offset (or Displacement). This sort of addressing is called “Segment plus Offset addressing”.

Example 1: What is the actual memory address if the DS register contains 0x03E0 and the offset is
0x32?.

0x03E0 + 0x32 = 0x0412

1.3 Pointer Register


Pointer registers are used to hold the offset value (i.e. Displacement). There are three Pointer Registers
which correspond to Segment registers.

Instruction Pointer (IP) Is a 16-bit register that holds the offset address of the next instruction
to be executed. IP associate with the CS register and address is
indicated as CS:IP.

Stack Pointer (SP) Is a 16-bit register that holds the offset address which points to specific
location within the stack. When associated with the SS register (SS:SP) it
refers to the current word being processed in the stack.

Base Pointer (BP) Base Pointer facilitates referencing parameters, which are data and
addresses that a program passes via a stack. BP is combined with SS
register.

Example 2 : If CS value is 0x39B and IP is 0x514 what is the address of the next instruction to be
executed ?.

= 0x39B + 514

= 8AF

Other than above mentioned register there are other registers such as SI (Source Index), DI (Destination
Index) and Flag register. Flag Register is a 16-bit register that indicates states of various activities. It will
indicate the current status of the processor and the results of processing. Many instructions that do
arithmetic operations and computations do change the status of flags. Each bit in flag register
corresponds to some specific action.

Ex: A Flag Register:

O D I T S Z A P C
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

Faculty Of Information Technology, University Of Moratuwa 4


1.4 Interrupts
Certain events cause the processor to suspend the current operation and act on the reason of that
interruption. An input from a key board is such an event. If such an event occurs the processor stops the
current operation and calls the BIOS (Basic Input Output System) routine to handle the keyboard
request. After handling the request the processor returns back to the interrupted program. Interrupts
are of two types; Hardware Interrupts and Software Interrupts. Hardware interrupts are critical and
cannot be delayed and must be handled such as division by zero or hardware error belongs to the
category of Hardware Interrupts. Software interrupts are basically called within the program and it
includes events such as displaying something on screen.

2.0 Programming in Assembly

2.1 Statement
An assembly program consists of set of statements which can be categorized as instructions and
directives. Instructions (examples: MOV, ADD) are the ones which are translated in object code.
Directives tell the assembler to perform specific action, such as define a data item.

2.2 Format of a statement

[Identifier] Operation [Operand(s)] [;Comment]

Identifier, operation, operand and comment must be separated by at least one blank space or tab
character. Tab is the most recommended one. Square brackets indicate optional entries.

Identifier Allows reference of a statement in the program. (A label to a statement)

Operation Indicates the specific assembly instruction. (The action to be performed).

Operand Provides information for the operation to act upon.

Comment Can be used by the programmer to keep notes within the program.

Sample statement:

[Identifier] Operation [Operand(s)] [;Comment]

L30: MOV AX, 6 ; put 0x06 in register AX

Faculty Of Information Technology, University Of Moratuwa 5


Example Program:

Write an assembly program to add 5 and 10

[Identifier] Operation [Operand(s)] [;Comment]

MOV AX, 5 ; put 0x05 in register AX

MOV BX, A ; put 0x0A in register BX

ADD AX, BX ; AX  AX + BX

Assembly Language instruction can be classified into several categories. Under each category many
instructions can be found. However for this course we will focus only on some of the very common
instructions.

Following table indicates some of the instructions and their meaning.

Arithmetic Instructions

ADD Add 2 binary numbers which are ADD AX, BX ; AX  AX + BX


either in registers, memory or an
immediate value ADD AX, 6 ; AX  AX +6

SUB Subtract 2 binary values SUB AX, BX ; AX  AX - BX

MUL Unsigned integer Multiply MUL BX ; AX  AX * BX

INC Increment by 1 INC AX; AX  AX + 1

DEC Decrement by 1 DEC BX; BX  BX - 1

DIV Division DIV BX, C1; Remainder in AH ,


Quotient in AL

Logical Operations

AND Logic AND (assume BL has value AND BL, 09; 0001  0101 &&
0x5 = 101) 1001

OR Logical OR OR BL,09; 1101  0101 || 1001

NOT Logical NOT NOT BL ; 11111010  not 0101

Faculty Of Information Technology, University Of Moratuwa 6


XOR Exclusive OR XOR BL,09 ; 1100  0101 &&
1001

Data Transfer Instructions

MOV Move data MOV AX, 06; AX  6

MOV AX, BX ; AX  BX

MOV [150], AX; address [150] 


AX

Brackets are addresses in


memory.

Bit Shifting Instructions

ROL Rotate Left (If BL is 10111000) ROL BL, 1 ; BL= 01110001

RCL Rotate Left through Carry (if BL is RCL BL, 1 ; BL = 01110000 &
10111000 and carry flag is 0) carry is 1

ROR Rotate Right

RCR Rotate Right through Cary

SHL Shift Left ( if BL is 10111000) SHL BL,1 ; 01110000 & carry is 1

SHR Shift Right

Comparison instruction

CMP Compare two values. Will set AF, CMP Ax, BX ; Compare AX & BX
CF, OF, PF, SF, and ZF flags. Used value
with conditional instructions.

CMPsn Compare String

Flag Operations

CLC Clear Carry Flag CLC; clear carry flag

STC Set Carry Flag STC; set carry flag

Transfer (Conditional) instructions

JE Jump if Equal CMP BX, 5 ; compare BX & 0x05

Faculty Of Information Technology, University Of Moratuwa 7


JE L20; if equal jump to label L20

JA/JB Jump if Above / Jump if Below CMP BX, 5 ; compare BX & 0x05

JA L2; if BX > 5 jump to label L2

JAE Jump if Above or equal C MP BX, 5 ; compare BX & 0x05

JAE L2; if BX >= 5 jump to label


L2

JC Jump if carry flag is set

JG/JL Jump if Greater / Jump if Less

JNA Jump if Not Above

JNB Jump if Not Below

JZ Jump if Zero

JNZ Jump if Not Zero

Transfer (Unconditional) instructions

JMP Jump unconditionally JMP L20; jump to label L20

INT Interrupt. Function Code is given MOV AH, 10 ; AH  0x10


in AH
INT 16 ; accept character from
key board

JMP 100 ; go back to the top

Faculty Of Information Technology, University Of Moratuwa 8


2.3 Example programs and Practical Considerations during programming

Example 1: Write an Assembly program to multiply 3 and 4.

[Identifier] Operation [Operand(s)] [;Comment]

MOV AX, 3 ; put 0x03 in register AX

MOV BX, 4 ; put 0x04 in register BX

MUL BX ; AX  AX * BX

Use of MUL and DIV instructions introduce lot of processing overhead. Multiplying or dividing by powers
of 2 (2, 4, 8, 16…2n) can be achieved by shifting bits with less processing overhead. This is shown in the
following example.

Example 2: Rewrite the program in Example 1 using Bit Shifting instructions.

[Identifier] Operation [Operand(s)] [;Comment]

MOV AL, 3 ; put 0x03 (0011) in register AL

SHL AL, 2 ; shift each bit in AL 2 positions

Left. (1100)= 12

Example 3: Write an assembly program to convert a given character from uppercase to lowercase or
vice versa.

A character represented in ASCII can be changed from lowercase to uppercase or vice versa by changing
the 5th bit.

A = 0 1 0 0 0 0 0 1 = 65 = 0x41

A= 0 1 1 0 0 0 0 1 = 97 = 0x61

Suppose we are putting the character into the AL register and the character to be changed is C = 67

0 1 0 0 0 0 1 1 = 67 = 0x43

[Identifier] Operation [Operand(s)] [;Comment]

MOV AL, 43 ; put 0x43 in register AL

XOR AL, 20 ; AL  43 XOR 20

Faculty Of Information Technology, University Of Moratuwa 9


Example 4: Write a program to calculate the total of all integers from 1 to 10.

[Identifier] Operation [Operand(s)] [;Comment]

MOV AX, 0 ; AX  0x0

MOV CX, 0A ; AX  0x0A

L1: ADD AX, CX ; AX  AX + CX

DEC CX ; CX CX - 1

JNZ L1 ; Jump to L1 if CX is not zero

Final result will appear in AX.

For displaying content to screen or reading input you have to use Software Interrupts.

3.0 Real world coding using an assembler

You need an Assembler (Translating Program) like FASM (Flat Assembler) to translate the assembly
program into machine code.

1. FASM is available at http://flatassembler.net/download.php


A GUI IDE version is available for download here,
http://wareseeker.com/download/flat-assembler-1.67.26.rar/238499

2. The 8086 emulator is a good graphical emulator which can compile assembler code as well as
emulate the code execution.

This is available at http://www.emu8086.com/

The trial version is fully functional for two weeks.

Faculty Of Information Technology, University Of Moratuwa 10


Let’s see how we can write an assembly program in FASM.

Example 5: Write a program to display the term “Hello World” on screen using FASM (This is taken from
one of the examples in FASM);

The first two statements are the COM file start template to include for a Windows COM executable.

; fasm example of writing 16-bit COM program

org 100h ; code starts at offset 100h

use16 ; use 16-bit code

display_text = 9

mov dx,hello ;

mov ah,display_text ; moving 0x09 to AH says to print the content in DX (Data

Registry) bit-string to the screen.

int 21h ; Interrupt CPU

int 20h ; terminate COM program

hello db 'Hello world!',24h ; db stands for definite byte. In the data-segment it defines a
couple of bytes. These bytes contain the information between
the ‘ ‘. "hello" is a name to indentify this byte-string. It’s an
identifier. 24h is an interrupt saying to write string “Hello
World!” to screen.

Faculty Of Information Technology, University Of Moratuwa 11

You might also like