You are on page 1of 7

Rameshwar Saran Shakti Saran http://shaktisaran.tech.officelive.com/ProgrammingInC.

aspx

Assembly language is a low level programming language, learning it will help you understand how the underlying hardware operates, which will enable you to write efficient software. Answer this: If machines are getting exponentially more powerful and Moores law holds good then why software users aren't seeing similar increase in speed while running applications? This has two reasons: 1. Additional processing power has been employed to support new features (such as graphics) 2. Today's programmers don't take the time to consider how the underlying hardware shall operate for the code they write. Often, you'll hear old-time programmers make the comment that truly efficient software is written in assembly language. The real reason assembly language programs tend to be more efficient than programs written in other languages is because assembly language forces the programmer to consider how the underlying hardware operates with each machine instruction they write.

You need to get some knowledge about computer structure in order to understand assembly language.

CPU - the heart of the computer, most of computations occur inside CPU. RAM - programs are loaded here in order to be executed. SYSTEM BUS (shown in yellow) - connects the various components of a computer.

Main registers (General Purpose Registers) AH BH CH DH Index registers (General Purpose Registers) SI DI BP SP Status register 15 14 13 Segment register 12 11 O 10 D 9 I 8 T CS DS ES SS Instruction pointer IP 7 S 6 Z 5 4 A

AL BL CL DL

AX (primary accumulator) BX (base, accumulator) CX (counter, accumulator) DX (accumulator, other functions) Source Index Destination Index Base Pointer Stack Pointer 3 2 P 1 0 C (bit position) Flags Code Segment Data Segment ExtraSegment Stack Segment Instruction Pointer

General Purpose Registers: The 8086 had eight 16-bit registers including the stack pointer, but excluding the instruction pointer, flag register and segment registers. Four of them (AX,BX,CX,DX) could also be accessed as 8-bit registers (AH,AL,BH,BL, etc), the other four (BP,SI,DI,SP) were 16-bit only. Despite the name of a register, it's the programmer who determines the usage for each general purpose register. The main purpose of a register is to keep a number (variable) e.g. something like: 0011000000111001b in binary, or 12345 in decimal.

All internal registers as well as internal and external data buses were 16 bits wide, firmly establishing the "16-bit microprocessor" identity of the 8086.

A 20-bit external address bus gave an 1 MB (segmented) physical address space (220 = 1,048,576). The data bus was multiplexed with the address bus in order to fit a standard 40-pin dual inline package. (Multiplexing: Digital data streams are combined into one signal over a shared medium. The aim is to share an expensive resource. For example, in telephony, several phone calls may be transferred using one wire.) 16-bit I/O addresses meant 64 KB of separate I/O space (216 = 65,536). Because internal registers were only 16 bits wide the maximum linear address space was limited to 64 KB. Programming over 64 KB boundaries involved adjusting segment registers (and remained so until the 80386).

Segment Registers: They have a very special purpose - pointing at accessible blocks of memory (although it is possible to store any data in the segment registers, this is never a good idea). CS - points at the segment containing the current program. DS - generally points at segment where variables are defined. ES - extra segment register, it's up to a coder to define its usage. SS - points at the segment containing the stack. To access any memory location segment registers work together with general purpose register. For example, to access memory at the physical address 12345h , we set the DS = 1230h and SI = 0045h. This way we can access much more memory than with a single register that is limited to 16 bit values (64 KB). CPU makes a calculation of physical address by multiplying the segment register by 10h and adding general purpose register to it (1230h * 10h + 45h = 12345h):

The address formed with 2 registers is called an effective address. By default BX, SI and DI registers work with DS segment register; BP and SP work with SS segment register. Other general purpose registers cannot form an effective address! Also, although BX can form an effective address, BH and BL cannot.
IP (instruction pointer) register always works together with CS segment register and it points to currently executing instruction.

Segment the value in segment register (CS, DS, SS, ES) is called a segment Offset and the value in purpose register (BX, SI, DI, BP) is called an offset. When DS contains value 1234h and SI contains the value 7890h it can be also recorded as 1234:7890.

The physical address will be 1234h * 10h + 7890h = 19BD0h.


Note: If zero is added to a decimal number it is multiplied by 10. However 10h = 16, so if zero is added to a hex value, it is multiplied by 16. For example: 7h = 7 70h = 112

You might also like