मुद्रा बिनिमय दर

Thursday, November 25, 2010

THE ARCHITECTURE OF A MICROPROCESSOR

A typical Microprocessor  consists of the following interconnected functional units:

  • Registers
  • Arithmetic/Logic Unit (ALU)
  • Control Circuitry
 1. REGISTERS:
Registers are temporary storage units within the Processor. Some registers, such as the program counter and instruction register, have dedicated uses. Other registers, such as the accumulator, are for more general purpose use.

Accumulator:

The accumulator usually stores one of the operands to be manipulated by the ALU. A typical instruction might direct the ALU to add the contents of some other register to the contents of the accumulator and store the result in the accumulator itself. In general, the accumulator is both a source (operand) and a destination (result) register.

Often a CPU will include a number of additional general purpose registers that can be used to store operands or intermediate data. The availability of general purpose registers eliminates the need to "shuffle" intermediate results back and forth between memory and the accumulator, thus improving processing speed and efficiency.

Program Counter (Jumps, Subroutines and the Stack):

The instructions that make up a program are stored in the system's memory. The central processor references the contents of memory, in order to determine what action is appropriate. This means that the processor must know which location contains the next instruction.

Each of the locations in memory is numbered, to distinguish it from all other locations in memory. The number which identifies a memory location is called its Address.

The processor maintains a counter which contains the address of the next program instruction. This register is called the Program Counter. The processor updates the program counter by adding "1" to the counter each time it fetches an instruction, so that the program counter is always current (pointing to the next instruction)

The programmer therefore stores his instructions in numerically adjacent addresses, so that the lower addresses contain the first instructions to be executed and the higher addresses contain later instructions. The only time the programmer may violate this sequential rule is when an instruction in one section of memory is a Jump instruction to another section of memory.

A jump instruction contains the address of the instruction which is to follow it. The next instruction may be stored in any memory location, as long as the programmed jump specifies the correct address. During the execution of a jump instruction, the processor replaces the contents of its program counter with the address embodied in the Jump. Thus, the logical continuity of the program is maintained.

A special kind of program jump occurs when the stored program "Calls" a subroutine. In this kind of jump, the processor is required to "remember" the contents of the program counter at the time that the jump occurs. This enables the processor to resume execution of the main program when it is finished with the last instruction of the subroutine.

A Subroutine is a program within a program. Usually it is a general-purpose set of instructions that must be executed repeatedly in the course of a main program. Routines which calculate the square, the sine, or the logarithm of a program variable are good examples of functions often written as subroutines. Other examples might be programs designed for inputting or outputting data to a particular peripheral device.

The processor has a special way of handling subroutines, in order to insure an orderly return to the main program. When the processor receives a Call instruction, it increments the Program Counter and stores the counter's contents in a reserved memory area known as the Stack. The Stack thus saves the address of the instruction to be executed after the subroutine is completed. Then the processor loads the address specified in the Call into its Program Counter. The next instruction fetched will therefore be the first step of the subroutine.

The last instruction in any subroutine is a Return. Such an instruction need specify no address. When the processor fetches a Return instruction, it simply replaces the current contents of the Program Counter with the address on the top of the stack. This causes the processor to resume execution of the calling program at the point immediately following the original Call Instruction.

Subroutines are often Nested; that is, one subroutine will sometimes call a second subroutine. The second may call a third, and so on. This is perfectly acceptable, as long as the processor has enough capacity to store the necessary

return addresses, and the logical provision for doing so. In other words, the maximum depth of nesting is determined by the depth of the stack itself. If the stack has space for storing three return addresses, then three levels of subroutines may be accommodated.

Processors have different ways of maintaining stacks. Some have facilities for the storage of return addresses built into the processor itself. Other processors use a reserved area of external memory as the stack and simply maintain a Pointer register which contains the address of the most recent stack entry. The external stack allows virtually unlimited subroutine nesting. In addition, if the processor provides instructions that cause the contents of the accumulator and other general purpose registers to be "pushed" onto the stack or "popped" off the stack via the address stored in the stack pointer, multilevel interrupt processing (described later in this chapter) is possible. The status of the processor (i.e., the contents of all the registers) can be saved in the stack when an interrupt is accepted and then restored after the interrupt has been serviced. This ability to save the processor's status at any given time is possible even if an interrupt service routine, itself, is interrupted.

Instruction Register and Decoder:


Every computer has a Word Length that is characteristic of that machine. A computer's word length is usually determined by the size of its internal storage elements and interconnecting paths (referred to as Busses); for example, a computer whose registers and busses can store and transfer 8 bits of information has a characteristic word length of 8 bits and is referred to as an 8bit parallel processor. An eight-bit parallel processor generally finds it most efficient to deal with eight-bit binary fields, and the memory associated with such a processor is therefore organized to store eight bits in each addressable memory location. Data and instructions are stored in memory as eight-bit binary numbers, or as numbers that are integral multiples of eight bits: 16 bits, 24 bits, and so on. This characteristic eight-bit field is often referred to as a Byte.

Each operation that the processor can perform is identified by a unique byte of data known as an Instruction Code or Operation Code. An eight-bit word used as an instruction code can distinguish between 256 alternative actions, more than adequate for most processors.

The processor fetches an instruction in two distinct operations. First, the processor transmits the address in its Program Counter to the memory Then the memory returns the addressed byte to the processor. The CPU stores this instruction byte in a register known as the Instruction Register, and uses it to direct activities during the remainder of the instruction execution.

The mechanism by which the processor translates an instruction code into specific processing actions requires more elaboration than we can here afford. The concept, however, should be intuitively clear to any logic designer.

The eight bits stored in the instruction register can be decoded and used to selectively activate one of a number of output lines, in this case up to 256 lines. Each line represents a set of activities associated with execution of a particular instruction code. The enabled line can be combined with selected timing pulses, to develop electrical signals that can then be used to initiate specific actions. This translation of code into action is performed by the Instruction Decoder and by the associated control circuitry.

An eight-bit instruction code is often sufficient to specify a particular processing action. There are times, however, when execution of the instruction requires more information than eight bits can convey

One example of this is when the instruction references a memory location. The basic instruction code identifies the operation to be performed, but cannot specify the object address as well In a case like this, a two or three-byte instruction must be used. Successive instruction bytes are stored in sequentially adjacent memory locations, and the processor performs two or three fetches in succession to obtain the full instruction. The first byte retrieved from memory is placed in the processor's instruction register, and subsequent bytes are placed in temporary storage; the processor then proceeds with the execution phase. Such an instruction is referred to as Variable Length.

Address Register(s):

A CPU may use a register or register pair to hold the address of a memory location that is to be accessed for data If the address register is Programmable, (i e., if there are instructions that allow the programmer to alter the contents of the register) the program can "build" an address in the address register prior to executing a Memory Reference instruction (i.e., an instruction that reads data from memory, writes data to memory or operates on data stored in memory).

2. ARITHMETIC/LOGIC UNIT (ALU):

All processors contain an arithmetic/logic unit, which is often referred to simply as the ALU The ALU, as its name implies, is that portion of the CPU hardware which performs the arithmetic and logical operations on the binary data .

The ALU must contain an Adder which is capable of combining the contents of two registers in accordance with the logic of binary arithmetic. This provision permits the processor to perform arithmetic manipulations on the data it obtains from memory and from its other inputs.

Using only the basic adder a capable programmer can write routines which will subtract, multiply and divide, giving the machine complete arithmetic capabilities. In practice, however, most ALUs provide other built-in functions, including hardware subtraction, Boolean logic operations, and shift capabilities

The ALU contains Flag Bits which specify certain conditions that arise in the course of arithmetic and logical manipulations. Flags typically include Carry, Zero, Sign, and Parity. It is possible to program jumps which are conditionally dependent on the status of one or more flags. Thus, for example, the program may be designed to jump to a special routine if the carry bit is set following an addition instruction

3. CONTROL CIRCUITRY:

The control circuitry is the primary functional unit within a CPU. Using clock inputs, the control circuitry maintains the proper sequence of events required for any processing task After an instruction is fetched and decoded, the control circuitry issues the appropriate signals (to units both internal and external to the CPU) for initiating the proper processing action. Often the control circuitry will be capable of responding to external signals, such as an interrupt or wait request An Interrupt request will cause the control circuitry to temporarily interrupt main program execution, jump to a special routine to service the interrupting device, then automatically return to the main program. A Wait request is often issued by a memory or 1/0 element that operates slower than the CPU. The control circuitry will idle the CPU until the memory or 1/0 port is ready with the data.

COMPUTER OPERATIONS


There are certain operations that are basic to almost any computer A sound understanding of these basic operations is a necessary prerequisite to examining the specific operations of a particular computer.

Timing:

The activities of the central processor are cyclical. The processor fetches an instruction, performs the operations
required, fetches the next instruction, and so on. This orderly sequence of events requires precise timing, and the CPU therefore requires a free running oscillator clock which furnishes the reference for all processor actions The combined fetch and execution of a single instruction is referred to as an Instruction Cycle. The portion of a cycle identified with a clearly defined activity IS called a State. And the inter vat between pulses of the timing oscillator is referred to as a Clock Period. As a general rule, one or more clock periods are necessary for the completion of a state, and there are several states in a cycle.

Instruction Fetch:

The first state(s) of any instruction cycle will be dedicated to fetching the next instruction. The CPU issues a read signal and the contents of the program counter are sent to memory, which responds by returning the next instruc tion word. The first byte of the instruction is placed in the instruction register. If the instruction consists of more than one byte, additional states are required to fetch each byte of the instruction. When the entire instruction is present in the CPU, the program counter is incremented (in preparation for the next instruction fetch) and the instruction is decoded. The operation specified in the instruction will be executed in the remaining states of the instruction cycle. The instruction may call for a memory read or write, an input or output and/or an internal CPU operation, such as a register to register transfer or an add registers operation.

Memory Read:

An instruction fetch is merely a special memory read operation that brings the instruction to the CPU's instruction register. The instruction fetched may then call for data to be read from memory into the CPU. The CPU again issues a read signal and sends the proper memory address; memory responds by returning the requested word. The data received is placed in the accumulator or one of the other general purpose registers (not the instruction register).

Memory Write:

A memory write operation is similar to a read except for the direction of data flow. The CPU issues a write signal, sends the proper memory address, then sends the data word to be written into the addressed memory location.

Wait (memory synchronization):

As previously stated, the activities of the processor are timed by a master clock oscillator. The clock period determines the timing of all processing activity.

The speed of the processing cycle, however, is limited by the memory's Access Time. Once the processor has sent a read address to memory, it cannot proceed until the memory has had time to respond. Most memories are capable of responding much faster than the processing cycle requires. A few, however, cannot supply the addressed byte within the minimum time established by the processor's clock.

Therefore a processor should contain a synchronization provision, which permits the memory to request a Wait state. When the memory receives a read or write enable signal, it places a request signal on the processor's READY line, causing the CPU to idle temporarily. After the memory has had time to respond, it frees the processor's READY line, and the instruction cycle proceeds

Input/Output:

Input and Output operations are similar to memory read and write operations with the exception that a peripheral 1/0 device is addressed instead of a memory location. The CPU issues the appropriate input or output control signal, sends the proper device address and either receives the data being input or sends the data to be output.
Data can be input/output in either parallel or serial form. All data within a digital computer is represented in binary coded form. A binary data word consists of a group 5
of bits; each bit is either a one or a zero. Parallel 1/0 consists of transferring all bits in the word at the same time, one bit per line. Serial 1/0 consists of transferring one bit at a time on a single line. Naturally serial 1/0 is much slower, but it requires considerably less hardware than does parallel 1/0.

Interrupts:


Interrupt provisions are included on many central processors, as a means of improving the processor's efficiency. Consider the case of a computer that is processing a large volume of data, portions of which are to be output to a printer. The CPU can output a byte of data within a single machine cycle but it may take the printer the equivalent of many machine cycles to actually print the character specified by the data byte. The CPU could then remain idle waiting until the printer can accept the next data byte. If an interrupt capability is implemented on the computer, the CPU can output a data byte then return to data processing. When the printer is ready to accept the next data byte, it can request an interrupt. When the CPU acknowledges the interrupt, it suspends main program execution and automatically branches to a routine that will output the next data byte. After the byte is output, the CPU continues with main program execution. Note that this is, in principle, quite similar to a subroutine call, except that the jump is initiated externally rather than by the program.

More complex interrupt structures are possible, in which several interrupting devices share the same processor but have different priority levels. Interruptive processing is an important feature that enables maximum utilization of a processor's capacity for high system throughput.

Hold:

Another important feature that improves the throughput of a processor is the Hold. The hold provision enables Direct Memory Access (DMA) operations.

In ordinary input and output operations, the processor itself supervises the entire data transfer. Information to be placed in memory is transferred from the input device to the processor, and then from the processor to the designated memory location. In similar fashion, information that goes from memory to output devices goes by way of the processor.

Some peripheral devices, however, are capable of transferring information to and from memory much faster than the processor itself can accomplish the transfer. If any appreciable quantity of data must be transferred to or from such a device, then system throughput will be increased by having the device accomplish the transfer directly. The processor must temporarily suspend its operation during such a transfer, to prevent conflicts that would arise if processor and peripheral device attempted to access memory simultaneously. It is for this reason that a hold provision is included on some processors.

Tuesday, November 23, 2010

Some Examples - IQ

1) To write 8086 Assembly Language Program to Multiply two unsigned number.
-------------------------------------------------------------------------------
MODEL SMALL
.STACK 100
.DATA ; Data Segment to initialize the variables
A DW 0FF87H ; First signed number A = (-79H) = FF87H (2'Compliment form)
B DW 0FF84H ; Second signed number B = (-7CH) = FF84H (2'Compliment form)
C DW ? ; Variable C to store result

.CODE 
START: 
MOV AX,@DATA
MOV DS,AX ; Initialize data segment
MOV SI,0000H ; Initialize SI to 0000H
MOV AX,A ;Take first number A in AX register
MOV CX,B ;Take second number B in CX register
MUL CX ; Performs unsigned Multiplication DX:AX = AX × CX
MOV C[SI],DX ; Store higher 16-bit result
MOV C[SI+2],AX ; Store lower 16-bit result
INT 03H


END START

Input:
FF87 × FF84 = FF0B 3A9C H
A = 0FF87 H (2'compliment of -79H)
B = 0FF84 H (2'compliment of -7CH)
Output:
C = FF0B 3A9C H 
-------------------------------------------------------------------------------
2) To write 8086 Assembly Language Program to multiply two signed number.
-------------------------------------------------------------------------------
MODEL SMALL
.STACK 100
.DATA ; Data Segment to initialize the variables
A DW 0FF87H ; First signed number A = (-79H) = FF87H (2'Compliment form)
B DW 0FF84H ; Second signed number B = (-7CH) = FF84H (2'Compliment form)
C DW ? ; Variable C to store result


.CODE 
START: 
MOV AX,@DATA
MOV DS,AX ;Initialize data segment
MOV SI,0000H ;Initialize SI to 0000H
MOV AX,A ;Take first number A in AX register
MOV CX,B ;Take second number B in CX register
IMUL CX ; Performs signed Multiplication DX:AX = AX × CX
MOV C[SI],DX ;Store higher 16-bit result
MOV C[SI+2],AX ;Store lower 16-bit result
INT 03H


END START

Input:
-79 × -7C = 3A9C H
A = 0FF87 H (2'compliment of -79H)
B = 0FF84 H (2'compliment of -7CH)
Output:
C = 0000 3A9C H
-------------------------------------------------------------------------------
3) To write 8086 Assembly Language Program to multiply two 32-bit unsigned numbers.
-------------------------------------------------------------------------------
MODEL SMALL
.STACK 100
.DATA ; Data segment starts
A DW 5678H, 1234H, 5 DUP(0) ;A is 32bit number A=1234 5678
b DW 1111H, 1111H, 5 DUP(0) ;B is 32bit number B=1111 1111
C DW 4 DUP(?) ; Reserve 4 words of uninitialized data space to an offset C
.CODE
START:
MOV AX,@DATA ;Initialize DS
MOV DS,AX
MOV SI,OFFSET A ;Point to first number in A


MOV AX,WORD PTR A[SI] ;Take lower 16bits(5678) of A into AX
MUL WORD PTR B[BX+0] ;Multiply AX with lower 16bits of B(1111) and store in AX
MOV C[DI],AX ;Move the contents of AX to C[DI]
MOV CX,DX ;Move the value of DX to CX


MOV AX,WORD PTR A[SI+2] ;Take higher 16bits(1234) of A into AX
MUL WORD PTR B[BX+0] ;Multiply AX with lower 16bits of B(1111)and store in AX
ADD CX,AX ;CX=CX+AX
MOV C[DI+2],CX ;Move the contents of CX to C[DI+2]
MOV CX,DX ;Move contents of DX to CX


MOV AX,WORD PTR A[SI] ;Take lower 16bits(5678) of A in AX
MUL WORD PTR B[BX+2] ;Multiply contents of AX with higher 16bits of B(1111)
ADD WORD PTR C[DI+2],AX ;C[DI+2]=C[DI+2]+AX
ADC CX,DX ;CX=CX+DX+CF
MOV C[DI+4],AX ;Move contents of AX to C[DI+4]


MOV AX,WORD PTR A[SI+2] ;Take higher 16bits of A(1234) into AX
MUL WORD PTR B[BX+2] ;Multiply AX with higher 16bits of B(1111) and store in AX
ADD CX,AX ;CX=CX+AX
MOV WORD PTR C[DI+4],CX ;Move contents of CX to C[DI+4]
ADC DX,0000 ;DX=DX+0000+CF
MOV C[DI+6],DX ;Move the contents of DX to C[DI+6]


INT 03H ; Halt

END START 

INPUT 
A = 1234 5678 H 
B = 1111 1111 H
OUTPUT
C=0136 BO6E 652F B5F8 H
-------------------------------------------------------------------------------
4) To write 8086 Assembly Language Program to Division two unsigned number.
-------------------------------------------------------------------------------
MODEL SMALL
.STACK 100
.DATA ; Data Segment to initialize the variables
Dividend DW 1234H, 5678H ; Dividend = 1234 5678 H
Divisor DW 270FH ; Divisor = 207FH
Quotient DW ? ; Variable Quotient to store Quotient
Reminder DW ? ; Variable Reminder to store Reminder


.CODE 
START: 
MOV AX,@DATA
MOV DS,AX ;Initialize data segment
MOV SI,0000H ;Initialize SI to 0000H
MOV DX,Dividend[SI] ;Take higher 16-bit number which is to be divided in DX register
MOV AX,Dividend[SI+2] ;Take lower 16-bit number which is to be divided in AX register
MOV CX,Divisor ;Take divisor in CX register
DIV CX ; Performs unsigned Division DX:AX ÷CX
; AX = Quotient DX = Reminder
MOV Reminder,DX ;Store Reminder
MOV Quotient,AX ;Store Quotient
INT 03H
END START

Input:
12345678H ÷ 207FH = 7751H
Dividend = 1234 5678 H
Divisor = 207F H
Output:
Quotient = 7751 H
Reminder = 01B9 H
-------------------------------------------------------------------------------
5) To write 8086 Assembly Language Program to Division two signed number.
-------------------------------------------------------------------------------

MODEL SMALL
.STACK 100
.DATA ; Data Segment to initialize the variables
Dividend DW 0FFFFH, 0FF88H ; Dividend = (-78H) = FFFF FF88H (2'Compliment form)
Divisor DW 0006H ; Divisor = 0006H
Quotient DW ? ; Variable Quotient to store Quotient
Reminder DW ? ; Variable Reminder to store Reminder


.CODE 
START: 
MOV AX,@DATA
MOV DS,AX ;Initialize data segment
MOV SI,0000H ;Initialize SI to 0000H
MOV DX,Dividend[SI] ;Take higher 16-bit number which is to be divided in DX register
MOV AX,Dividend[SI+2] ;Take lower 16-bit number which is to be divided in AX register
MOV CX,Divisor ;Take divisor in CX register
IDIV CX ; Performs signed Division DX:AX ÷ CX
; AX = Quotient DX = Reminder
MOV Reminder,DX ;Store Reminder
MOV Quotient,AX ;Store Quotient
INT 03H


END START

Input:
-78H ÷ 06H = -14H
Dividend = FFFF FF87 H (32-bit 2'compliment of -78H)
Divisor = 0006 H
Output:
Quotient = FFEC H (2'compliment of -14H)
Reminder = 0000 H

Monday, November 22, 2010

8086 Assembly Tutorials

8086 Assembly Language

Structure of an assembly language program
  • Assembly language programs divide roughly into five sections
    • header
    • equates
    • data
    • body
    • closing
The Header
  • The header contains various directives which do not produce machine code
  • Sample header:
%TITLE "Sample Header"
.8086
.model small
.stack 256
Named Constants
  • Symbolic names associated with storage locations represent addresses
  • Named constants are symbols created to represent specific values determined by an expression
  • Named constants can be numeric or string
  • Some named constants can be redefined
  • No storage is allocated for these values
Equates
  • Constant values are known as equates
  • Sample equate section:
Count EQU 10
Element EQU 5
Size = Count * Element
MyString EQU "Maze of twisty passages"
Size = 0
  • = is used for numeric values only
  • Cannot change value of EQU symbol
  • EQUated symbols are not variables
  • EQU expressions are evaluated where used; = expressions are evaluated where defined
The Data Segment
  • Begins with the .data directive
  • Two kinds of variables, initialized and uninitialized.
  • Initialized variables take up space in the program's code file
  • Declare uninitialized variables after initialized ones so they do not take up space in the program's code file
Reserving space for variables
  • Sample DATA SEGMENT
.data
numRows    DB 25
numColumns DB ?
videoBase  DW 0800h
  • DB and DW are common directives (define byte) and (define word)
  • The symbols associated with variables are called labels
  • Strings may be declared using the DB directive:
aTOm DB "ABCDEFGHIJKLM"
Program Data and Storage
  • Pseudo-ops to define data or reserve storage
    • DB - byte(s)
    • DW - word(s)
    • DD - doubleword(s)
    • DQ - quadword(s)
    • DT - tenbyte(s)
  • These directives require one or more operands
    • define memory contents
    • specify amount of storage to reserve for run-time data
Defining Data
  • Numeric data values
    • 100 - decimal
    • 100b - binary
    • 100h - hexadecimal
    • '100' - ASCII
    • "100" - ASCII
  • Use the appropriate DEFINE directive (byte, word, etc.)
  • A list of values may be used - the following creates 4 consecutive words
DW 40Ch,10b,-13,0
  • A ? represents an uninitialized storage location
DB 255,?,-128,'X'
Naming Storage Locations
  • Names can be associated with storage locations
ANum DB -4
     DW 17
ONE
UNO  DW 1
X    DD ?
  • These names are called variables
  • ANum refers to a byte storage location, initialized to FCh
  • The next word has no associated name
  • ONE and UNO refer to the same word
  • X is an uninitialized doubleword
Arrays
  • Any consecutive storage locations of the same size can be called an array
X  DW  040Ch,10b,-13,0
Y  DB  'This is an array'
Z  DD  -109236, FFFFFFFFh, -1, 100b
  • Components of X are at X, X+2, X+4, X+6
  • Components of Y are at Y, Y+1, …, Y+15
  • Components of Z are at Z, Z+4, Z+8, Z+12
DUP
  • Allows a sequence of storage locations to be defined or reserved
  • Only used as an operand of a define directive
DB  40 DUP(?)
DW  10h DUP(0)
DB  3 DUP("ABC")
DB  4 DUP(3 DUP (0,1), 2 DUP('$'))
Word Storage
  • Word, doubleword, and quadword data are stored in reverse byte order (in memory)
Directive      Bytes in Storage
DW 256         00 01
DD 1234567h    67 45 23 01
DQ 10          0A 00 00 00 00 00 00 00
X DW 35DAh     DA 35
Low byte of X is at X, high byte of X is at X+1
The Program Body
  • Also known as the code segment
  • Divided into four columns: labels, mnemonics, operands, and comments
  • Labels refer to the positions of variables and instructions, represented by the mnemonics
  • Operands are required by most assembly language instructions
  • Comments aid in remembering the purpose of various instructions
An example
Label    Mnemonic   Operand     Comment
---------------------------------------------------------
         .data
exCode   DB         0          ;A byte variable
myWord   DW         ?          ;Uninitialized word var.
         .code
MAIN    PROC
         mov        ax,@data   ;Initialize DS to address
         mov        ds,ax      ; of data segment
         jmp        Exit       ;Jump to Exit label
         mov        cx,10      ;This line skipped!
Exit:    mov        ah,04Ch    ;DOS function: Exit prog
         mov        al, exCode ;Return exit code value
         int        21h        ;Call DOS. Terminate prog
MAIN     ENDP                  ;End Program
         END        MAIN       ; and specify entry point
The Label Field
  • Labels mark places in a program which other instructions and directives reference
  • Labels in the code segment always end with a colon
  • Labels in the data segment never end with a colon
  • Labels can be from 1 to 31 characters long and may consist of letters, digits, and the special characters ? . @ _ $ %
  • If a period is used, it must be the first character
  • Labels must not begin with a digit
  • The assembler is case insensitive
Legal and Illegal Labels
  • Examples of legal names
    • COUNTER1
    • @character
    • SUM_OF_DIGITS
    • $1000
    • DONE?
    • .TEST
  • Examples of illegal names
    • TWO WORDS contains a blank
    • 2abc begins with a digit
    • A45.28 . not first character
    • YOU&ME contains an illegal character
The Mnemonic Field
  • For an instruction, the operation field contains a symbolic operation code (opcode)
  • The assembler translates a symbolic opcode into a machine language opcode
  • Examples are: ADD, MOV, SUB
  • In an assembler directive, the operation field contains a directive (pseudo-op)
  • Pseudo-ops are not translated into machine code; they tell the assembler to do something
The Operand Field
  • For an instruction, the operand field specifies the data that are to be acted on by the instruction. May have zero, one, or two operands
NOP             ;no operands -- does nothing
INC AX          ;one operand -- adds 1 to the contents of AX
ADD WORD1,2     ;two operands -- adds 2 to the contents
                ; of memory word WORD1
  • In a two-operand instruction, the first operand is the destination operand. The second operand is the source operand.
  • For an assembler directive, the operand field usually contains more information about the directive.
The Comment Field
  • A semicolon marks the beginning of a comment field
  • The assembler ignores anything typed after the semicolon on that line
  • It is almost impossible to understand an assembly language program without good comments
  • Good programming practice dictates a comment on almost every line
Good and Bad Comments
  • Don't say something obvious, like
MOV CX,0 ;move 0 to CX
  • Instead, put the instruction into the context of the program
MOV CX,0 ;CX counts terms, initially 0
  • An entire line can be a comment, or be used to create visual space in a program
;
; Initialize registers
;
    MOV AX,0
    MOV BX,0
The Closing
  • The last lines of an assembly language program are the closing
  • Indicates to assembler that it has reached the end of the program and where the entry point is
MAIN  ENDP      ;End of program
      END MAIN  ; entry point for linker use
  • END is a pseudo-op; the single "operand" is the label specifying the beginning of execution, usually the first instruction after the .code pseudo-op
Assembling a Program
  • The source file of an assembly language program is usually named with an extension of .asm
edit myprog.asm
  • The source file is processed (assembled) by the assembler (TASM) to produce an object file (.obj)
tasm myprog produces myprog.obj
  • The object file must be linked by the linker (TLINK) to produce an executable file (.exe)
tlink myprog produces myprog.exe
Dealing with Errors
  • TASM will report the line number and give an error message for each error it finds
  • Sometimes it is helpful to have a listing file (.lst), created by using TASM with the -l option
  • The .lst file contains a complete listing of the program, along with line numbers, object code bytes, and the symbol table
Using the Debugger
  • Useful for logic errors that the assembler misses
  • See the text for a complete tutorial
  • You do not need to use the TDH386.SYS driver or the TD386.EXE debugger with the latest version of the assembler
  • To use the debugger on myprog.asm
tasm /zi myprog
tlink /v myprog
td myprog
.COM and .EXE files
  • The .COM code file format is a relic of the first version of MS-DOS
  • Not recommended for general purposes
  • All code, data, and the stack occupy one 64K segment (Borland's "tiny" model)
  • .EXE code files are more efficient in use of RAM
  • Data and code occupy separate segments
  • The programmer is responsible for setting up the data and code segments properly
Ending a Program
  • All programs, upon termination, must return control back to another program -- the operating system
  • Under MS-DOS, this is COMMAND.COM
  • This is done by doing a DOS system call
Data Transfer Instructions
  • MOV destination,source
    • reg, reg
    • mem, reg
    • reg, mem
    • mem, immed
    • reg, immed
  • Sizes of both operands must be the same
  • reg can be any non-segment register except IP cannot be the target register
  • MOV's between a segment register and memory or a 16-bit register are possible
Examples
  • mov ax, word1
    • "Move word1 to ax"
    • Contents of register ax are replaced by the contents of the memory location word1
  • xchg ah, bl
    • Swaps the contents of ah and bl
  • Illegal: mov word1, word2
    • can't have both operands be memory locations
Sample MOV Instructions
b    db  4Fh
w    dw  2048
mov bl,dh
mov ax,w
mov ch,b
mov al,255
mov w,-100
mov b,0
  • When a variable is created with a define directive, it is assigned a default size attribute (byte, word, etc)
  • You can assign a size attribute using LABEL
LoByte LABEL BYTE
aWord  DW    97F2h
Addresses with Displacements
b    db  4Fh, 20h, 3Ch
w    dw  2048, -100, 0
mov bx, w+2
mov b+1, ah
mov ah, b+5
mov dx, w-3
  • Type checking is still in effect
  • The assembler computes an address based on the expression
  • NOTE: These are address computations done at assembly time

MOV ax,b-1
will not subtract 1 from the value stored at b
eXCHanGe
  • XCHG destination,source
    • reg, reg
    • reg, mem
    • mem, reg
  • MOV and XCHG cannot perform memory to memory moves
  • This provides an efficient means to swap the operands
    • No temporary storage is needed
    • Sorting often requires this type of operation
    • This works only with the general registers
Arithmetic Instructions
ADD dest, source
SUB dest, source
INC dest
DEC dest
NEG dest
  • Operands must be of the same size
  • source can be a general register, memory location, or constant
  • dest can be a register or memory location
    • except operands cannot both be memory
ADD and INC
  • ADD is used to add the contents of
    • two registers
    • a register and a memory location
    • a register and a constant
  • INC is used to add 1 to the contents of a register or memory location
Examples
  • add ax, word1
    • "Add word1 to ax"
    • Contents of register ax and memory location word1 are added, and the sum is stored in ax
  • inc ah
    • Adds one to the contents of ah
  • Illegal: add word1, word2
    • can't have both operands be memory locations
SUB, DEC, and NEG
  • SUB is used to subtract the contents of
    • one register from another register
    • a register from a memory location, or vice versa
    • a constant from a register
  • DEC is used to subtract 1 from the contents of a register or memory location
  • NEG is used to negate the contents of a register or memory location
Examples
  • sub ax, word1
    • "Subtract word1 from ax"
    • Contents of memory location word1 is subtracted from the contents of register ax, and the sum is stored in ax
  • dec bx
    • Subtracts one from the contents of bx
  • Illegal: sub byte1, byte2
    • can't have both operands be memory locations
Type Agreement of Operands
  • The operands of two-operand instructions must be of the same type (byte or word)
    • mov ax, bh     ;illegal
    • mov ax, byte1  ;illegal
    • mov ah,'A'     ;legal -- moves 41h into ah
    • mov ax,'A'     ;legal -- moves 0041h into ax
Translation of HLL Instructions
  • B = A

mov ax,A
mov B,ax
    • memory-memory moves are illegal
  • A = B - 2*A

mov ax,B
sub ax,A
sub ax,A
mov A,ax
Program Segment Structure
  • Data Segments
    • Storage for variables
    • Variable addresses are computed as offsets from start of this segment
  • Code Segment
    • contains executable instructions
  • Stack Segment
    • used to set aside storage for the stack
    • Stack addresses are computed as offsets into this segment
  • Segment directives
.DATA
.CODE
.STACK size
Memory Models
  • .Model memory_model
    • tiny: code+data <= 64K (.com program)
    • small: code<=64K, data<=64K, one of each
    • medium: data<=64K, one data segment
    • compact: code<=64K, one code segment
    • large: multiple code and data segments
    • huge: allows individual arrays to exceed 64K
    • flat: no segments, 32-bit addresses, protected mode only (80386 and higher)
Program Skeleton
.MODEL small
.STACK 100h
.DATA
;declarations
.CODE
MAIN PROC
;main proc code
;return to DOS
ENDP MAIN
;other procs (if any) go here
end MAIN
 
·  Select a memory model
·  Define the stack size
·  Declare variables
·  Write code
·          organize into procedures
·  Mark the end of the source file
·          define the entry point
Input and Output Using 8086 Assembly Language
  • Most input and output is not done directly via the I/O ports, because
    • port addresses vary among computer models
    • it's much easier to program I/O with the service routines provided by the manufacturer
  • There are BIOS routines (which we'll look at later) and DOS routines for handling I/O (using interrupt number 21h)
Interrupts
  • The interrupt instruction is used to cause a software interrupt (system call)
    • An interrupt interrupts the current program and executes a subroutine, eventually returning control to the original program
    • Interrupts may be caused by hardware or software
  • int interrupt_number     ;software interrupt
Output to Monitor
  • DOS Interrupts : interrupt 21h
    • This interrupt invokes one of many support routines provided by DOS
    • The DOS function is selected via AH
    • Other registers may serve as arguments
  • AH = 2, DL = ASCII of character to output
    • Character is displayed at the current cursor position, the cursor is advanced, AL = DL
Output a String
  • Interrupt 21h, function 09h
    • DX = offset to the string (in data segment)
    • The string is terminated with the '$' character
  • To place the address of a variable in DX, use one of the following
    • lea   DX,theString        ;load effective address
    • mov   DX, offset theString ;immediate data
Print String Example
%TITLE "First Program -- HELLO.ASM"
        .8086
        .MODEL   small
        .STACK   256
        .DATA
msg     DB      "Hello, World!$"
        .CODE
MAIN    PROC
        mov     ax,@data     ;Initialize DS to address
        mov     ds,ax        ; of data segment
        lea     dx,msg       ;get message
        mov     ah,09h       ;display string function
        int     21h          ;display message
Exit:   mov     ah,4Ch       ;DOS function: Exit program
        mov     al,0         ;Return exit code value
        int     21h          ;Call DOS. Terminate program
MAIN    ENDP                 ;End of program
        END     MAIN         ; entry point
Input a Character
  • Interrupt 21h, function 01h
  • Filtered input with echo
    • This function returns the next character in the keyboard buffer (waiting if necessary)
    • The character is echoed to the screen
    • AL will contain the ASCII code of the non-control character
      • AL=0 if a control character was entered